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
Slide 2
Slide 2 text
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
Slide 3
Slide 3 text
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
Slide 4
Slide 4 text
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
Slide 5
Slide 5 text
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
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
Slide 8
Slide 8 text
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
Slide 9
Slide 9 text
the ternary operator
Language Features
Javascript:
var val = is_valid?1:0;
Python
val = 1 if is_valid else 0
Slide 10
Slide 10 text
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
Slide 11
Slide 11 text
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
Slide 12
Slide 12 text
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
Slide 13
Slide 13 text
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
Slide 14
Slide 14 text
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/
Slide 15
Slide 15 text
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
Slide 16
Slide 16 text
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/