Table of Contents
Decorator
http://stackoverflow.com/questions/739654/how-can-i-make-a-chain-of-function-decorators-in-python
Closure
http://stackoverflow.com/questions/4020419/closures-in-python
Yield
http://stackoverflow.com/questions/231767/what-does-the-yield-keyword-do-in-python
Use travis ci in github for your python projects
- https://github.com/lepture/safe
- http://docs.travis-ci.com/user/getting-started/
- http://docs.travis-ci.com/user/build-configuration/
- http://docs.travis-ci.com/user/languages/python/
Higher order functions
filter, map, reduce, sum, any, all.
integers = range(10) even = filter(lambda x: x % 2 ==0, integers) even = [x for x in integers if x % 2 == 0] map(lambda x: x * x, integers) reduce(lambda x, y: x * y, integers) sum(integers) any([False, True, False]) all([False, True, False])
super() in python
- Understanding Python super() with __init__() methods
- How to use 'super' in Python?
- super(type[, object-or-type])
- Python’s super() considered super!
String.format
一般的:
"{}, {}".format("Hello", "world")
可以指定顺序,或者重复某个:
"{2}{0}{1}".format('1', '2', '3') "{1}{0}{1}".format('1', '2', '3')
使用关键字:
"{first}{second}".format(first='Hello', second='world')
使用tuple, dict
list_number = ('1', '2', '3') "{}{}{}".format(*list_number) dictory_number = {'first': '1', 'second': '2'} "{first}{secong}".format(**dictory_number)
格式化:
[[fill]align][sign][#][0][minimumwidth][.precision][type] align flags: '<' - left-aligned, fill with space. '>' - right-aligned '=' - valid only for numeric types, force the padding to be placed - after the sign but before the digits. '^' - centered sign(only valid for numeric types): '+' - a sign should be used for both positive as well as negative numbers. '-' - a sign only for negative numbers. ' ' - a leading space should be used on positive numbers. '#': integers use the 'alternate form' which means taht binary, octal and hexadecimal output will be prefixed with 0b, 0o, 0x.
Inside the {}, we can't put executable python code except: (1):retrive
attibute with '.' notation.(2):retrive a single item with '[]' notation.
Reference:
standard library
re
groups group
unicode
sys.stdout = codes.getwriter('utf-8')(sys.stdout) text = u'' pattern = ur'\w+' re.search(pattern, text)
\num or (?P=name): 自引用表达式
re.split()
pydoc
$ pydoc platform # Get help information about platform module Or in the interpreter >> help('platform') >> help('platform.system') Or browse in the browser $ pydoc -p PORT
timeit
Class: Timer(stmt='pass', setup='pass', timer=
functions:
- repeat(repeat=3, number=1000000)
- timeit(number=1000000)
t = timeit.Timer("print 'Hello, world.'") t.timeit(2) r.repeat(3, 2)
from terminal
$ python -m timeit "for i in range(1000)" " d[str(i)=i" $ pthon -m timeit "import hello; hello.say_hi()"
the difference between __str__ and __repr__
object.__repr__(self)
Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an “informal” string representation of instances of that class is required. This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.
object.__str__(self)
Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object. This differs from __repr__() in that it does not have to be a valid Python expression: a more convenient or concise representation may be used instead. The return value must be a string object.
Exception
relavited blog posts:
- The most underrated feature in Python 3
- Write Cleaner Python: Use Exceptions
- Python Exception Handling Techniques
- Re-raising Exceptions
StackOverflow relavited questions: