python to livescript @pythoncon apac 2014
DESCRIPTION
introduction of LiveScript for Python programmer.TRANSCRIPT
Python to LiveScript@hychen
for PyCon APAC 2014
Star to learn LiveScript after join a g0v.tw, a open civic hacking community.
劣即 是夯
JavaScript everywhere
LiveScript• concise syntax.
• say bye to bad parts of JavaScript.
• many improvements for OOP.
• many features of FP.
• http://livescript.net/
• go there and try compiling
JS
LS
use -> to replace anonymous function
LS
LS
use bound call to remove `that = this`
LS
LS
@ = this
LS
LS
func! === func()
LS
LS
‘foo’ === \foo
LS
LS
use back call
LS
LS
use bound back call
JS
LS
Package Manager
you can use any JavaScript modules in LiveScript.
NPM
PyPi
ImportImport os !require! os
PY
LS
PY
LS
from os import path !path = require os .path
REPL
user@host: python Python 2.7.6 (default, Nov 17 2013, 16:03:54) [GCC 4.2.1 Compatible Apple LLVM 5.0 (clang-500.2.79)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>>
user@host: lsc LiveScript 1.2.0 - use 'lsc --help' for more information ls>
Python
LiveScript
Inspect attributes of an object
Object.keys
dir>>> dir(path) ['__all__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '_joinrealpath', '_unicode', '_varprog', 'abspath', 'altsep', 'basename', 'commonprefix', 'curdir', 'defpath', 'devnull', 'dirname', 'exists', 'expanduser', 'expandvars', 'extsep', 'genericpath', 'getatime', 'getctime', 'getmtime', 'getsize', 'isabs', 'isdir', 'isfile', 'islink', ‘ismount', ….
ls> Object.keys path [ 'resolve', 'normalize', 'join','relative','sep','delimiter', 'dirname', 'basename','extname','exists', 'existsSync', '_makeLong' ]
Help
PY
LS X
pydoc
Object Model
Please check the slide “Python New-Style Object v.s. JavaScript prototype, COSCUP 2012”
The basic object model of LiveScript should be the same as JavaScript.
List construction[1, 2, [3, 4], [5,6]]PY & LS
1 spaceLS
List Property Access
[1, 2, [3,4]][0]PY & LS
LS
LS
[1, 2, [3,4]].0
[1, 2, [3,4]].2.0
Dict construction
PY & LS
LS
quotes of the keywords are optional
use object to simulate Python dict in LiveScript.
Dict construction(cont.)
PY & LS
LS
another better represent way
use object to simulate Python dict in LiveScript.
Dict Property Access
{‘a’:1, ‘b’}[‘a’]PY & LS
LS {‘a’:1, ‘b’}.a
unpacking function arguments
LS
PY
unpacking function keyword argument
LS
PY
packing function arguments
PY
LS
packing function keyword arguments
PY
LS
destructing assignment
PY & LS
LS
destructing assignment (cont.)
LS
Python does not support.
destructing function arguments
LS
Python does not support.
List Comprehension
PY
LS
result: [‘a1', 'a2', 'b1', 'b2']
Dict Comprehension
PY
LS
result: {‘a': 2, 'b': 4}
File Reading
⼤大家 !好 !我是 ! [Name]
file: memo.txt
PY
File Reading(cont.)
callback 2
callback 1
LS
Backcallcallback1
callback2
LS
Backcall(cont.)
LS
LS
Class
PY
Class(cont.)
LS
… means arguments of current function
Python Super
• super(type, obj) -> bound super object; requires isinstance(obj, type)
LiveScript Super
• is a reference to the appropriate function.
• If you want to call it with all arguments, use super …
• … implies calling it with the arguments of the current function.
LiveScript Mixing
You can extend only 1 parent but can implement many interface.
Many more…
LS Curried Function
Many more…
LS
Conclusion• change mind to code with callbacks.
• functional style programming with prelude.ls
• use class rarely after use LiveScript.
• LiveScript does not have debugger now, testing is highly important!
• You may need to check compiled JavaScript even your LiveScript code are correctly rarely.
Questions?