print "Hello World"
Jey大的30分鐘系列
# class
CamleCase
# function
all_lower_underscore
# global
ALL_UPPER_UNDERSCORE
# public (variable, module, package)
all_lower_underscore
# protect
_var_
# private
__var
# avoid reserve variables
file_
# module internal used
_var
# not important(throwable var)
_
# python reserve
__var__
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)
# 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):
...
# attribute related
hasattr
getattr
setattr
# import related
__import__
# container, loop
iter
next
...
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
>>> 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
# 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
>>> for _k,_v in a.items(): print _k,_v
...
foo hello
bar world
>>> 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)]
>>> 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}
>>> 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)
>>> for _ in spam:
... if _ > 3:
... print "break"
... break
... else:
... print "loop finish"
break
>>> for _ in spam:
... pass
... else:
... print "loop finish"
loop finish
>>> 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
>>> 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
>>>
>>> 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
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.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)
>>> 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()
>>> 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 |