Day 3 Agenda
Speaker: Rick Copeland
Date: Tue Jul 15 19:02:58 EDT 2014
Uses yield, yield can be used as a function, along with the send() method
Iterator will raise and except StopIteration.
It must implement __iter__
[x*2 for x in range(4)] or map(lambda x: x*2, range(4))
[(x,y) for x in range(4) for y in range(4)]
[x for x in range(10) if x%2 == 0]
Side note:
keyword: with
For example:
with open('PATH') as fp_i, open('PATH', 'w') as fp_o:
fp_o.write(fp_i.read())
Must have __enter__(self) and __exit__(self)
Side note:
Another useful library logger
difference between Context manager and decorator
Using build-in library import contextlib to create context manager
for example:
def MyClass():
def close():
pass
with contextlib.closing(MyClass()):
pass
Install packages
python setup.py installorpip install .pip install package_name
Install virtualenv
- download virtualenv.py
- download via pip,
pip install virtualenv
Behind the sence:
virtualenv changes shell path and inserts current path at the beginning of PATH
for example, install packages of the following:
pip install nose
pip install mock
pip install converage
- Primitive:
distutil - Nicer:
setuptools
setup.py commands
- sdist
- bdist_rpm, bdst_wininst, bdist_msi
- bdist_egg(setuptools only), alternative wheel
- register
- upload
- develop(setuptools only),
python setup.py developorpip install -e .
Create a release:
python setup.py sdist
- unittest,
import unittestkeywords: try, except, else, raise, finally, docstring - Using mock mocking complex objects for better unit testing
- Using nose to discover tests
- Using converage, uses for nose
- Another trivial testing tool to test docstring examples, doctest