Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Little known and/or useful Python features for fun and profit

Larry Weya
January 23, 2014

Little known and/or useful Python features for fun and profit

Larry Weya

January 23, 2014
Tweet

Other Decks in Programming

Transcript

  1. Little known and/or useful Python features for fun & profit

    by Larry Weya - Co-founder and Software Engineer at Ona Labs Kenya - Video Game Development Hobbyist
  2. Why I love Python Productivity, think less do more. How?

    • REPL (Read-Eval-Print loop)/Interactive shell • Rapid prototyping • Mature package management • Inbuilt testing framework • Pretty code == readable code == maintainable code
  3. 1. (virtualenv) Isolated working copy of Python. Why? Run different

    python installations on the same system, each with its own set of packages. Invaluable in any python dev machine. virtualenv - http://www.virtualenv.org/en/latest/ virtualenvwrapper - http://virtualenvwrapper.readthedocs.org/en/latest/index. html Make using virtualenv even easier. • Central location of all virtual environments • switch virtual environments with the `workon` command, includes TAB completion
  4. 2. setuptools Packaging for Python. Build, distribute and install python

    packages. • Create distributable python .egg • Create distributable Python sources • Upload packages to PyPi • Automatically find and download your packages dependencies on installation (by yourself or 3rd parties) • … http://pythonhosted.org/setuptools/setuptools.html Python package skeleton: $> easy_install PasteScript $> paster create basic_package
  5. setuptools Development Mode Make the python source directories available to

    a python installation. Changes made to your sources are immediately available in the Python installation. ProjectB (depends on ProjectA) (virtualenv) for Project B ProjectA
  6. setuptools Wrapper script generation Generate command line tools from you

    python functions. mymodule/utils.py import sys def setup_database(argv=sys.argv): create_and_populate_database() return 0 def upgrade_database(argv=sys.argv): run_db_migrations() return 0 setup.py from setuptools import setup, find_packages setup( … entry_points = { 'console_scripts': [ 'setup_db = mymodule.utils: setup_database' ] } … # creates <python_dir>/bin/setup_db $> setup_db
  7. IPython “Command shell for interactive computing” - Wikipedia • TAB

    completion (on imports, function names …) • Command history across sessions • Object introspection - func_name? • Magic methods ◦ %timeit ◦ %run ◦ %cd ◦ … ◦ %debug - after an exception ◦ %pdb - toggle dropping into debugger on exceptions
  8. try..except..else Language Features try: o = get_db_object(pk) parent_object = get_object(o.parent_id)

    except ObjectNotFound: log(“Object with pk: {} not found”.format(pk)) try: db_object = get_db_object(pk) except ObjectNotFound: log(“Object with pk: {} not found”.format(pk)) else: parent_object = get_object(o.parent_id) • Don’t catch the wrong exceptions • Readability - “do this only when the exception is not raised” vs
  9. Language Features List Comprehensions kids = [{‘name’: ‘Billy’, ‘age’: 2},

    {‘name’: ‘Bob’, ‘age’: 5}] ages = [] for kid in kids: ages.append(kid[‘age’]) do_something(ages) do_something([kid['age'] for kid in kids]) • Clear and concise syntax when working with lists (and other iterables) • Readability? Not when it gets too complex
  10. Language Features Generators x = [n for n in range(1000000000)]

    x = (n for n in range(1000000000)) Pros • No extra storage is created • You can then iterate over only part of the list without the extra memory overhead • Cons: • You can only iterate over the result once vs
  11. Language Features List Stepping values = range(10) third = []

    for idx, val in enumerate(values): if val % 3 == 0 third.append(val) values = range(10) # get every 3rd item result = values[::3] • READABILITY vs
  12. Language Features Debugging Regular Expressions (Experimental) import re re.compile(r"^hell(o)", re.DEBUG)

    at at_beginning literal 104 literal 101 literal 108 literal 108 subpattern 1 literal 111
  13. Language Features mymodule/utils.py def failing_function(): import ipdb; ipdb.set_trace() result =

    call_some_other_function() # s to step into result += 1 # n to step over return result Debugging pdb/ipdb Python Debugger Cheatsheet - http://nblock.org/2011/11/15/pdb-cheatsheet/
  14. Language Features >>> func_is_even = lambda n: n % 2

    == 0 >>> func_is_even(3) False >>> filter(func_is_even, range(10)) [0, 2, 4, 6, 8] Lambdas/Anonymous Functions
  15. Additional References • Ona Data Platform - https://github.com/onaio/onadata, http://ona.io/ •

    PyCharm IDE - http://www.jetbrains.com/pycharm/ • Blender 3D - http://www.blender.org/