Python Quick Start

2/5 (Fri)

2/19 (Fri)

Introduction

print "Hello World"

Introduction

Introduction

Jey大的30分鐘系列

Introduction

#755 (Jey Tsai)

Outline

  1. Introduction

  2. Python Basic

  3. Python Advanced

  4. Test in Python

  5. Modules & Tool introduction

  6. Q & A

Resources

Resources

Resources

David Beazley youtube channel

Resources

Resources

Resources

Python Basic

Python Basic

  1. Python Coding Style (PEP8)

  2. Documents

  3. Builtin funtions

  4. Loop

  5. Exception

  6. Python tooler

Python Coding Style (PEP8)

# class
CamleCase

# function 
all_lower_underscore

# global
ALL_UPPER_UNDERSCORE

Python Coding Style (PEP8)

# public (variable, module, package) 
all_lower_underscore

# protect
_var_

# private
__var

Python Coding Style (PEP8)

# avoid reserve variables
file_

# module internal used
_var

# not important(throwable var)
_

# python reserve
__var__

Documents

def show_me_the_money(me, action):
    payroll_slip = ricky.sendmail()
    if payroll_slip.money < ACCEPT_VAL:
        me.do_B(action)
    else:
        me.do_G(action)

Documents

# function action?
wear_slipper_everyday()
wear_short_pants()
snitch_to_labor_affair()

# object action?
class BadAction(object):
    def wear_slipper_everyday(self):
      ...
    def wear_short_pants(self):
      ...
    def snitch_to_labor_affair(self):
      ...

Documents

  1. help()

  2. dir()

  3. repr()

  4. str()

  5. __doc__

Docstring Conventions

Demo Time

Builtin Functions

Builtin Functions

# attribute related
hasattr
getattr
setattr

# import related
__import__

# container, loop
iter
next
...

Loop - iteration

i = 0
while i < len(foo_list):
    v = my_list[i]
    print v
    i += 1
for i in range(len(foo_list)):
    print foo_list[i]
for _it in foo_list:
    print _it

Loop - get idx and val

>>> foo = [1,2,3,4,5]
>>> for _idx in range(len(foo)):
...     print _idx, foo[_idx]
... 
0 1
1 2
2 3
3 4
4 5
>>> for _idx, _val in enumerate(foo):
...   print _idx, _val
... 
0 1
1 2
2 3
3 4
4 5

Loop - iteration

# Get dictionary value
>>> a = {"foo": "hello", "bar": "world"}
>>> for key in a:
...     print a[key]
... 
hello
world
>>> for _v in a.itervalues(): print _v
... 
hello
world

Loop - iteration

>>> for _k,_v in a.items(): print _k,_v
... 
foo hello
bar world

Loop - iteration:zip

>>> a = [1,2,3,4,5]
>>> b = [1,2,3]
>>> for _ in zip(a,b):
...     print _
... 
(1, 1)
(2, 2)
(3, 3)
>>> c = [5,5,6,6]
>>> zip(a,b,c)
[(1, 1, 5), (2, 2, 5), (3, 3, 6)]

Loop - iteration

>>> a = [1,2,3,4,5]
>>> b = [_ for _ in a if _ > 2]
>>> b
[3, 4, 5]
>>> c = [_ if _ > 3 else None for _ in a]
>>> c
[None, None, None, 4, 5]
>>> d = {str(_): _ for _ in a}
>>> d
{'1': 1, '3': 3, '2': 2, '5': 5, '4': 4}

Loop - iteration:map

>>> def fib(n):
...     return 1 if n <=2 else fib(n-1)+fib(n-2)
... 
>>> fib(10)
55
>>> map(fib, a)
[1, 1, 2, 3, 5]
>>> [fib(_) for _ in a]
[1, 1, 2, 3, 5]
>>> map(spam, [(1,2),(3,4)])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: spam() takes exactly 2 arguments (1 given)

Loop - for ... else

>>> for _ in spam:
...     if _ > 3:
...         print "break"
...         break
... else:
...     print "loop finish"
break
>>> for _ in spam:
...     pass
... else:
...     print "loop finish"
loop finish

try... exception ... else

>>> try:
...     1/0
... except ZeroDivisionError:
...     raise
... else:
...     print "No exception"
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
ZeroDivisionError: integer division or modulo by zero
integer division or modulo by zero

>>> try:
...     1/2
... except ZeroDivisionError:
...     raise
... else:
...     print "No exception"
No exception

Python Tooler

  1. Decorator

  2. Iterator

  3. Generator

  4. Context manager

  5. Descriptor

@Decorator

  1. Function Patch

  2. Class Patch?

Iterator

>>> a = [5,5,6,6,]
>>> b = iter(a)
>>> b.next()
5
>>> b.next()
5
>>> b.next()
6
>>> b.next()
6
>>> b.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
>>> 

Iterator?

>>> for _ in a: print _,
9 5 2 7
>>> for _ in a.__iter__(): print _,
9 5 2 7
>>> for _ in iter(a): print _,
9 5 2 7

Interpreter translate iterable  object -> Iterator

Generator

yield

Generator

  1. Generate a sequence

  2. Generator can implement scheduler

  3. Python3 enhance generator

  4. Python3 add "generator deletate"

  5. Python3 implement Coroutine via Generator

Context Manager

with

Context Manager

  1. Resource Manager

  2. with scope

  3. __enter__(self)

  4. __exit__(self, exc_ty, exc_val, tb)

  5. contextlib

Descriptor

Protocol

  1. __get__

  2. __set__

  3. __delete__

object attribute with “binding behavior”

Descriptor

object attribute with “binding behavior”

@property

Other useful protocols

  1. __getitem__(self, key)

  2. __setitem__(self, key, val)

  3. __iter__(self)

  4. __contains__(self, value)

  5. __getattribute__(self, key)

  6. __getattr__(self, key)

Python Advanced

  1. os.path, os, sys, shutil, subprocess 

  2. File Operation

  3. threading

  4. multiprocessing

  5. socket

os.path

os.path.abspath
os.path.basename
os.path.dirname
os.path.join
os.path.exists
os.path.isfile
os.path.isdir
os.path.islink
os.path.realpath
os.path.walk

OS

os.mkdir
os.chdir
os.geteuid
os.seteuid
os.getgid
os.getpid
os.fork
os.dup2
os.open
os.read
os.write
os.lseek
os.fstat
os.uname
os.popen(not recommand used)

File Operations

  1. open

  2. close

  3. read & write

  4. json

  5. PyYAML

  6. ConfigureParser

  7. tarfile

  8. zipfile

threading

Fake Thread

threading

threading

Multiprocessing

multiprocessing.Pool Fail?! Python Bugs??

>>> for _ in range(2):
...     task = multiprocessing.Process(target=fib, args=(30,))
...     tasks.append(task)  

>>> tasks  
[<Process(Process-15, initial)>, <Process(Process-16, initial)>]

>>> for _ in range(2):
...     task = tasks[_]
...     task.start()
... for _ in range(2):
...     task = tasks[_]
...     task.join()

Demo Time

Mutliprocessing

VS

threading

CPU bound

VS

IO bound

Socket

  1. TCP

  2. UDP

  3. Domain Socket

  4. socket + threading

  5. socket +select

Demo Time

Test In Python

PyCon 2014

doctest

Test Your Documents are right

Unit Test?

PyUnit

PyUnit

  1. Ref JUnit

  2. Python 2.1 include

  3. Python stdardard library: unittest

  4. Python3 include "Mock" in unittest

unittest:glossary 

  1. Test Fixture: setUp & tearDown

  2. Test Case: smallest unit of testing

  3. Test Suite: collection of Tests

  4. Test Runner: run & report

Definition: Python Document

unittest == PyUnit

  1. Basic unittest

  2. setUp & tearDown hierarchy

  3. skip test

  4. combine test

Mocking

@mock.patch(real, fake)

Demo Time

ctypes

ctypes

>>> from ctypes import *
>>> sdk = CDLL('libsynosdk.so.6') 
>>> sdk.SLIBServiceIsEnabled('atalk')
1
Pros Cons
Pure Python code struct need rewrite
C++ need C wrapper
Need to learn

Standard Library

Modules & Tool introduction

3rd party

Modules & Tool introduction

  1. flask - web backend micro framework

  2. jinja2 - HTML template engine

  3. django - full stack web framework

  4. requests - better than urllib

  5. scrapy - analysis packet

  6. paramiko - ssh related operation

  7. sqlalchemy - database ORM operations

Modules & Tool introduction

  1. pytest - test framework

  2. Pyro4 - Python remote object

  3. virtualenv - Python dev enviroment

  4. flake8 - Python coding style check

  5. yapf - Fix your coding style

  6. coverage - Python code coverage

  7. gunicorn - WSGI webserver

Modules & Tool introduction

  1. gevent - event base concurrency lib

  2. netifaces - get NICs infomation

  3. scipy - computation

  4. pyyaml - Python yaml parser

  5. python-jenkins - interact with jenkins

  6. py2app - Build Mac OSX .app

  7. py2exe - Build Windows .exe

Modules & Tool introduction

  1. lockfile - Python file lock

  2. psutil - Process utils

  3. OpenCV - opencv support

  4. python-zeroconf - pure python mDNS

  5. Avahi - Avahi support python binding

  6. DBus - DBus support

Modules & Tool introduction

  1. ipython - Better Python shell

  2. bpython - Better Python shell

  3. ptpython - Great Python shell

  4. ipython-notebooks - Past your code

Q & A

Python

By chang-ning tsai

Python

  • 474