Skip to content

Instantly share code, notes, and snippets.

@ldong
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save ldong/787e77ba6440a51d4265 to your computer and use it in GitHub Desktop.

Select an option

Save ldong/787e77ba6440a51d4265 to your computer and use it in GitHub Desktop.

Fast track

Day 3 Agenda

Speaker: Rick Copeland

Date: Tue Jul 15 19:02:58 EDT 2014

Generators

Uses yield, yield can be used as a function, along with the send() method

Iterator tools

Iterator will raise and except StopIteration.

It must implement __iter__

Loop comprehensions

[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:

zip

Context manager

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

virtualenv

Install packages

  1. python setup.py install or pip install .
  2. pip install package_name

Install virtualenv

  1. download virtualenv.py
  2. 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

Pack into packages

  1. Primitive: distutil
  2. Nicer: setuptools

setup.py commands

  1. sdist
  2. bdist_rpm, bdst_wininst, bdist_msi
  3. bdist_egg(setuptools only), alternative wheel
  4. register
  5. upload
  6. develop(setuptools only), python setup.py develop or pip install -e .

Create a release:

  1. python setup.py sdist

Testing

  1. unittest, import unittest keywords: try, except, else, raise, finally, docstring
  2. Using mock mocking complex objects for better unit testing
  3. Using nose to discover tests
  4. Using converage, uses for nose
  5. Another trivial testing tool to test docstring examples, doctest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment