python to livescript @pythoncon apac 2014

47
Python to LiveScript @hychen for PyCon APAC 2014

Upload: hsinyi-chen

Post on 14-May-2015

788 views

Category:

Technology


4 download

DESCRIPTION

introduction of LiveScript for Python programmer.

TRANSCRIPT

Page 1: Python To LiveScript @PythonCon APAC 2014

Python to LiveScript@hychen

for PyCon APAC 2014

Page 2: Python To LiveScript @PythonCon APAC 2014
Page 3: Python To LiveScript @PythonCon APAC 2014

Star to learn LiveScript after join a g0v.tw, a open civic hacking community.

Page 4: Python To LiveScript @PythonCon APAC 2014

劣即 是夯

JavaScript everywhere

Page 5: Python To LiveScript @PythonCon APAC 2014

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

Page 6: Python To LiveScript @PythonCon APAC 2014
Page 7: Python To LiveScript @PythonCon APAC 2014

JS

LS

use -> to replace anonymous function

Page 8: Python To LiveScript @PythonCon APAC 2014

LS

LS

use bound call to remove `that = this`

Page 9: Python To LiveScript @PythonCon APAC 2014

LS

LS

@ = this

Page 10: Python To LiveScript @PythonCon APAC 2014

LS

LS

func! === func()

Page 11: Python To LiveScript @PythonCon APAC 2014

LS

LS

‘foo’ === \foo

Page 12: Python To LiveScript @PythonCon APAC 2014

LS

LS

use back call

Page 13: Python To LiveScript @PythonCon APAC 2014

LS

LS

use bound back call

Page 14: Python To LiveScript @PythonCon APAC 2014

JS

LS

Page 15: Python To LiveScript @PythonCon APAC 2014

Package Manager

you can use any JavaScript modules in LiveScript.

NPM

PyPi

Page 16: Python To LiveScript @PythonCon APAC 2014

ImportImport os !require! os

PY

LS

PY

LS

from os import path !path = require os .path

Page 17: Python To LiveScript @PythonCon APAC 2014

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

Page 18: Python To LiveScript @PythonCon APAC 2014

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' ]

Page 19: Python To LiveScript @PythonCon APAC 2014

Help

PY

LS X

pydoc

Page 20: Python To LiveScript @PythonCon APAC 2014

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.

Page 21: Python To LiveScript @PythonCon APAC 2014

List construction[1, 2, [3, 4], [5,6]]PY & LS

1 spaceLS

Page 22: Python To LiveScript @PythonCon APAC 2014

List Property Access

[1, 2, [3,4]][0]PY & LS

LS

LS

[1, 2, [3,4]].0

[1, 2, [3,4]].2.0

Page 23: Python To LiveScript @PythonCon APAC 2014

Dict construction

PY & LS

LS

quotes of the keywords are optional

use object to simulate Python dict in LiveScript.

Page 24: Python To LiveScript @PythonCon APAC 2014

Dict construction(cont.)

PY & LS

LS

another better represent way

use object to simulate Python dict in LiveScript.

Page 25: Python To LiveScript @PythonCon APAC 2014

Dict Property Access

{‘a’:1, ‘b’}[‘a’]PY & LS

LS {‘a’:1, ‘b’}.a

Page 26: Python To LiveScript @PythonCon APAC 2014

unpacking function arguments

LS

PY

Page 27: Python To LiveScript @PythonCon APAC 2014

unpacking function keyword argument

LS

PY

Page 28: Python To LiveScript @PythonCon APAC 2014

packing function arguments

PY

LS

Page 29: Python To LiveScript @PythonCon APAC 2014

packing function keyword arguments

PY

LS

Page 30: Python To LiveScript @PythonCon APAC 2014

destructing assignment

PY & LS

LS

Page 31: Python To LiveScript @PythonCon APAC 2014

destructing assignment (cont.)

LS

Python does not support.

Page 32: Python To LiveScript @PythonCon APAC 2014

destructing function arguments

LS

Python does not support.

Page 33: Python To LiveScript @PythonCon APAC 2014

List Comprehension

PY

LS

result: [‘a1', 'a2', 'b1', 'b2']

Page 34: Python To LiveScript @PythonCon APAC 2014

Dict Comprehension

PY

LS

result: {‘a': 2, 'b': 4}

Page 35: Python To LiveScript @PythonCon APAC 2014

File Reading

⼤大家 !好 !我是 ! [Name]

file: memo.txt

PY

Page 36: Python To LiveScript @PythonCon APAC 2014

File Reading(cont.)

callback 2

callback 1

LS

Page 37: Python To LiveScript @PythonCon APAC 2014

Backcallcallback1

callback2

LS

Page 38: Python To LiveScript @PythonCon APAC 2014

Backcall(cont.)

LS

LS

Page 39: Python To LiveScript @PythonCon APAC 2014

Class

PY

Page 40: Python To LiveScript @PythonCon APAC 2014

Class(cont.)

LS

… means arguments of current function

Page 41: Python To LiveScript @PythonCon APAC 2014

Python Super

• super(type, obj) -> bound super object; requires isinstance(obj, type)

Page 42: Python To LiveScript @PythonCon APAC 2014

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.

Page 43: Python To LiveScript @PythonCon APAC 2014

LiveScript Mixing

You can extend only 1 parent but can implement many interface.

Page 44: Python To LiveScript @PythonCon APAC 2014

Many more…

LS Curried Function

Page 45: Python To LiveScript @PythonCon APAC 2014

Many more…

LS

Page 46: Python To LiveScript @PythonCon APAC 2014

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.

Page 47: Python To LiveScript @PythonCon APAC 2014

Questions?