Python-Guide by Kenneth
Python-Guide by Kenneth
Release 0.0.1
Kenneth Reitz
Contents
Getting Started
1.1 Picking an Interpreter . . . . .
1.2 Installing Python on Mac OS X
1.3 Installing Python on Windows .
1.4 Installing Python on Linux . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3
3
5
6
8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
11
11
17
26
27
29
33
35
Scenario Guide
3.1 Network Applications . . .
3.2 Web Applications . . . . .
3.3 HTML Scraping . . . . . .
3.4 Command-line Applications
3.5 GUI Applications . . . . . .
3.6 Databases . . . . . . . . . .
3.7 Networking . . . . . . . . .
3.8 Systems Administration . .
3.9 Continuous Integration . . .
3.10 Speed . . . . . . . . . . . .
3.11 Scientific Applications . . .
3.12 Image Manipulation . . . .
3.13 XML parsing . . . . . . . .
3.14 Cryptography . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
37
37
38
43
45
46
47
48
49
53
55
58
60
61
62
63
63
65
Development Environment
5.1 Your Development Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.2 Virtual Environments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
67
67
72
.
.
.
.
.
.
.
ii
Additional Notes
6.1 Introduction . . . . .
6.2 The Community . . .
6.3 Learning Python . . .
6.4 Documentation . . . .
6.5 News . . . . . . . . .
6.6 Contribute . . . . . .
6.7 License . . . . . . . .
6.8 The Guide Style Guide
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
75
75
76
77
81
82
82
84
84
Contents
Contents
CHAPTER 1
Getting Started
1.1.2 Recommendations
Ill be blunt:
Use Python 3 if...
You dont care.
You love Python 3.
You are indifferent towards 2 vs 3.
You dont know which one to use.
You embrace change.
Use Python 2 if...
You love Python 2 and are saddened by the future being Python 3.
The stability requirements of your software would be improved by a language and runtime that never changes.
Software that you depend on requires it.
1.1.3 So.... 3?
If youre choosing a Python interpreter to use, and arent opinionated, then I recommend you use the newest Python 3.x,
since every version brings new and improved standard library modules, security and bug fixes. Progress is progress.
Given such, only use Python 2 if you have a strong reason to, such as a Python 2 exclusive library which has no
adequate Python 3 ready alternative, or you (like me) absolutely love and are inspired by Python 2.
Check out Can I Use Python 3? to see if any software youre depending on will block your adoption of Python 3.
Further Reading
It is possible to write code that works on Python 2.6, 2.7, and 3.3. This ranges from trivial to hard depending upon the
kind of software you are writing; if youre a beginner there are far more important things to worry about.
1.1.4 Implementations
When people speak of Python they often mean not just the language but also the CPython implementation. Python is
actually a specification for a language that can be implemented in many different ways.
CPython
CPython is the reference implementation of Python, written in C. It compiles Python code to intermediate bytecode
which is then interpreted by a virtual machine. CPython provides the highest level of compatibility with Python
packages and C extension modules.
If you are writing open-source Python code and want to reach the widest possible audience, targeting CPython is best.
To use packages which rely on C extensions to function, CPython is your only implementation option.
All versions of the Python language are implemented in C because CPython is the reference implementation.
PyPy
PyPy is a Python interpreter implemented in a restricted statically-typed subset of the Python language called RPython.
The interpreter features a just-in-time compiler and supports multiple back-ends (C, CLI, JVM).
PyPy aims for maximum compatibility with the reference CPython implementation while improving performance.
If you are looking to increase performance of your Python code, its worth giving PyPy a try. On a suite of benchmarks,
its currently over 5 times faster than CPython.
PyPy supports Python 2.7. PyPy3 1 , released in beta, targets Python 3.
Jython
Jython is a Python implementation that compiles Python code to Java bytecode which is then executed by the JVM
(Java Virtual Machine). Additionally, it is able to import and use any Java class like a Python module.
If you need to interface with an existing Java codebase or have other reasons to need to write Python code for the JVM,
Jython is the best choice.
Jython currently supports up to Python 2.5.
1
2
http://pypy.org/compat.html
http://wiki.python.org/jython/JythonFaq/GeneralInfo#Is_Jython_the_same_language_as_Python.3F
IronPython
IronPython is an implementation of Python for the .NET framework. It can use both Python and .NET framework
libraries, and can also expose Python code to other languages in the .NET framework.
Python Tools for Visual Studio integrates IronPython directly into the Visual Studio development environment, making
it an ideal choice for Windows developers.
IronPython supports Python 2.7.
PythonNet
Python for .NET is a package which provides near seamless integration of a natively installed Python installation with
the .NET Common Language Runtime (CLR). This is the inverse approach to that taken by IronPython (see above),
to which it is more complementary than competing with.
In conjunction with Mono, PythonNet enables native Python installations on non-Windows operating systems, such as
OS X and Linux, to operate within the .NET framework. It can be run in addition to IronPython without conflict.
PythonNet supports from Python 2.3 up to Python 2.7.
The script will explain what changes it will make and prompt you before the installation begins. Once
youve installed Homebrew, insert the Homebrew directory at the top of your PATH environment variable.
You can do this by adding the following line at the bottom of your ~/.profile file
3
4
http://ironpython.codeplex.com/releases/view/81726
http://pythonnet.github.io/readme.html
export PATH=/usr/local/bin:/usr/local/sbin:$PATH
1.2.3 Virtualenv
After Setuptools & Pip, the next development tool that you should install is virtualenv. Use pip
$ pip install virtualenv
The virtualenv kit provides the ability to create virtual Python environments that do not interfere with
either each other, or the main Python installation. If you install virtualenv before you begin coding then
you can get into the habit of using it to create completely clean Python environments for each project.
This is particularly important for Web development, where each framework and application will have
many dependencies.
To set up a new Python environment, move into the directory where you would like to store the environment, and use the virtualenv utility to create the new environment.
$ virtualenv venv
To use an environment, run source venv/bin/activate. Your command prompt will change to
show the active environment. Once you have finished working in the current virtual environment, run
deactivate to restore your settings to normal.
Each new environment automatically includes a copy of pip, so that you can setup the third-party libraries
and tools that you want to use in that environment. Put your own code within a subdirectory of the
environment, however you wish. When you no longer need a particular environment, simply copy your
code out of it, and then delete the main directory for the environment.
A useful set of extensions to virtualenv is available in virtualenvwrapper, RTFD to find out more.
This page is a remixed version of another guide, which is available under the same license.
The Windows version is provided as an MSI package. To install it manually, just double-click the file. The
MSI package format allows Windows administrators to automate installation with their standard tools.
By design, Python installs to a directory with the version number embedded, e.g. Python version 2.7 will
install at C:\Python27\, so that you can have multiple versions of Python on the same system without
conflicts. Of course, only one interpreter can be the default application for Python file types. It also does
not automatically modify the PATH environment variable, so that you always have control over which
copy of Python is run.
Typing the full path name for a Python interpreter each time quickly gets tedious, so add the directories for
your default Python version to the PATH. Assuming that your Python installation is in C:\Python27\,
add this to your PATH:
C:\Python27\;C:\Python27\Scripts\
The second (Scripts) directory receives command files when certain packages are installed, so it is a
very useful addition. You do not need to install or configure anything else to use Python. Having said
that, I would strongly recommend that you install the tools and libraries described in the next section
before you start building Python applications for real-world use. In particular, you should always install
Setuptools, as it makes it much easier for you to use other third-party Python libraries.
1.3.2 Virtualenv
After Setuptools & Pip, the next development tool that you should install is virtualenv. Use pip
> pip install virtualenv
The virtualenv kit provides the ability to create virtual Python environments that do not interfere with
either each other, or the main Python installation. If you install virtualenv before you begin coding then
you can get into the habit of using it to create completely clean Python environments for each project.
This is particularly important for Web development, where each framework and application will have
many dependencies.
To set up a new Python environment, change the working directory to wherever you want to store the
environment, and run the virtualenv utility in your projects directory
To use an environment, run the activate.bat batch file in the Scripts subdirectory of that environment. Your command prompt will change to show the active environment. Once you have finished
working in the current virtual environment, run the deactivate.bat batch file to restore your settings
to normal.
Each new environment automatically includes a copy of pip in the Scripts subdirectory, so that you
can setup the third-party libraries and tools that you want to use in that environment. Put your own code
within a subdirectory of the environment, however you wish. When you no longer need a particular
environment, simply copy your code out of it, and then delete the main directory for the environment.
This page is a remixed version of another guide, which is available under the same license.
1.4.2 Virtualenv
After Setuptools & Pip, the next development tool that you should install is virtualenv. Use pip
The virtualenv kit provides the ability to create virtual Python environments that do not interfere with
either each other, or the main Python installation. If you install virtualenv before you begin coding then
you can get into the habit of using it to create completely clean Python environments for each project.
This is particularly important for Web development, where each framework and application will have
many dependencies.
To set up a new Python environment, change the working directory to where ever you want to store the
environment, and run the virtualenv utility in your projects directory
$ virtualenv venv
To use an environment, run source venv/bin/activate. Your command prompt will change to
show the active environment. Once you have finished working in the current virtual environment, run
deactivate to restore your settings to normal.
Each new environment automatically includes a copy of pip, so that you can setup the third-party libraries
and tools that you want to use in that environment. Put your own code within a subdirectory of the
environment, however you wish. When you no longer need a particular environment, simply copy your
code out of it, and then delete the main directory for the environment.
This page is a remixed version of another guide, which is available under the same license.
10
CHAPTER 2
This part of the guide focuses on best practices for writing Python code.
Spaghetti code: multiple pages of nested if clauses and for loops with a lot of copy-pasted procedural code
and no proper segmentation are known as spaghetti code. Pythons meaningful indentation (one of its most
controversial features) make it very hard to maintain this kind of code. So the good news is that you might not
see too much of it.
Ravioli code is more likely in Python: it consists of hundreds of similar little pieces of logic, often classes or
objects, without proper structure. If you never can remember if you have to use FurnitureTable, AssetTable or
Table, or even TableNew for your task at hand, you might be swimming in ravioli code.
2.1.2 Modules
Python modules are one of the main abstraction layers available and probably the most natural one. Abstraction layers
allow separating code into parts holding related data and functionality.
For example, a layer of a project can handle interfacing with user actions, while another would handle low-level
manipulation of data. The most natural way to separate these two layers is to regroup all interfacing functionality in
one file, and all low-level operations in another file. In this case, the interface file needs to import the low-level file.
This is done with the import and from ... import statements.
As soon as you use import statements you use modules. These can be either built-in modules such as os and sys,
third-party modules you have installed in your environment, or your projects internal modules.
To keep in line with the style guide, keep module names short, lowercase, and be sure to avoid using special symbols
like the dot (.) or question mark (?). So a file name like my.spam.py is one you should avoid! Naming this way will
interfere with the way Python looks for modules.
In the case of my.spam.py Python expects to find a spam.py file in a folder named my which is not the case. There is
an example of how the dot notation should be used in the Python docs.
If youd like you could name your module my_spam.py, but even our friend the underscore should not be seen often
in module names.
Aside from some naming restrictions, nothing special is required for a Python file to be a module, but you need to
understand the import mechanism in order to use this concept properly and avoid some issues.
Concretely, the import modu statement will look for the proper file, which is modu.py in the same directory as
the caller if it exists. If it is not found, the Python interpreter will search for modu.py in the path recursively and
raise an ImportError exception if it is not found.
Once modu.py is found, the Python interpreter will execute the module in an isolated scope. Any top-level statement
in modu.py will be executed, including other imports if any. Function and class definitions are stored in the modules
dictionary.
Then, the modules variables, functions, and classes will be available to the caller through the modules namespace, a
central concept in programming that is particularly helpful and powerful in Python.
In many languages, an include file directive is used by the preprocessor to take all code found in the file and
copy it into the callers code. It is different in Python: the included code is isolated in a module namespace, which
means that you generally dont have to worry that the included code could have unwanted effects, e.g. override an
existing function with the same name.
It is possible to simulate the more standard behavior by using a special syntax of the import statement: from modu
import *. This is generally considered bad practice. Using import * makes code harder to read and makes
dependencies less compartmentalized.
Using from modu import func is a way to pinpoint the function you want to import and put it in the global
namespace. While much less harmful than import * because it shows explicitly what is imported in the global
namespace, its only advantage over a simpler import modu is that it will save a little typing.
Very bad
12
[...]
from modu import *
[...]
x = sqrt(4) # Is sqrt part of modu? A builtin? Defined above?
Better
from modu import sqrt
[...]
x = sqrt(4) # sqrt may be part of modu, if not redefined in between
Best
import modu
[...]
x = modu.sqrt(4)
As mentioned in the Code Style section, readability is one of the main features of Python. Readability means to avoid
useless boilerplate text and clutter, therefore some efforts are spent trying to achieve a certain level of brevity. But
terseness and obscurity are the limits where brevity should stop. Being able to tell immediately where a class or
function comes from, as in the modu.func idiom, greatly improves code readability and understandability in all but
the simplest single file projects.
2.1.3 Packages
Python provides a very straightforward packaging system, which is simply an extension of the module mechanism to
a directory.
Any directory with an __init__.py file is considered a Python package. The different modules in the package are
imported in a similar manner as plain modules, but with a special behavior for the __init__.py file, which is used
to gather all package-wide definitions.
A file modu.py in the directory pack/ is imported with the statement import pack.modu. This statement will
look for an __init__.py file in pack, execute all of its top-level statements. Then it will look for a file named
pack/modu.py and execute all of its top-level statements. After these operations, any variable, function, or class
defined in modu.py is available in the pack.modu namespace.
A commonly seen issue is to add too much code to __init__.py files. When the project complexity grows, there
may be sub-packages and sub-sub-packages in a deep directory structure. In this case, importing a single item from a
sub-sub-package will require executing all __init__.py files met while traversing the tree.
Leaving an __init__.py file empty is considered normal and even a good practice, if the packages modules and
sub-packages do not need to share any code.
Lastly, a convenient syntax is available for importing deeply nested packages: import very.deep.module as
mod. This allows you to use mod in place of the verbose repetition of very.deep.module.
13
However, unlike Java, Python does not impose object-oriented programming as the main programming paradigm. It
is perfectly viable for a Python project to not be object-oriented, i.e. to use no or very few class definitions, class
inheritance, or any other mechanisms that are specific to object-oriented programming.
Moreover, as seen in the modules section, the way Python handles modules and namespaces gives the developer a
natural way to ensure the encapsulation and separation of abstraction layers, both being the most common reasons to
use object-orientation. Therefore, Python programmers have more latitude to not use object-orientation, when it is not
required by the business model.
There are some reasons to avoid unnecessary object-orientation. Defining custom classes is useful when we want to
glue together some state and some functionality. The problem, as pointed out by the discussions about functional
programming, comes from the state part of the equation.
In some architectures, typically web applications, multiple instances of Python processes are spawned to respond
to external requests that can happen at the same time. In this case, holding some state into instantiated objects,
which means keeping some static information about the world, is prone to concurrency problems or race-conditions.
Sometimes, between the initialization of the state of an object (usually done with the __init__() method) and the
actual use of the object state through one of its methods, the world may have changed, and the retained state may be
outdated. For example, a request may load an item in memory and mark it as read by a user. If another request requires
the deletion of this item at the same time, it may happen that the deletion actually occurs after the first process loaded
the item, and then we have to mark as read a deleted object.
This and other issues led to the idea that using stateless functions is a better programming paradigm.
Another way to say the same thing is to suggest using functions and procedures with as few implicit contexts and sideeffects as possible. A functions implicit context is made up of any of the global variables or items in the persistence
layer that are accessed from within the function. Side-effects are the changes that a function makes to its implicit
context. If a function saves or deletes data in a global variable or in the persistence layer, it is said to have a side-effect.
Carefully isolating functions with context and side-effects from functions with logic (called pure functions) allow the
following benefits:
Pure functions are deterministic: given a fixed input, the output will always be the same.
Pure functions are much easier to change or replace if they need to be refactored or optimized.
Pure functions are easier to test with unit-tests: There is less need for complex context setup and data cleaning
afterwards.
Pure functions are easier to manipulate, decorate, and pass-around.
In summary, pure functions, without any context or side-effects, are more efficient building blocks than classes and
objects for some architectures.
Obviously, object-orientation is useful and even necessary in many cases, for example when developing graphical
desktop applications or games, where the things that are manipulated (windows, buttons, avatars, vehicles) have a
relatively long life of their own in the computers memory.
2.1.5 Decorators
The Python language provides a simple yet powerful syntax called decorators. A decorator is a function or a class that
wraps (or decorates) a function or a method. The decorated function or method will replace the original undecorated
function or method. Because functions are first-class objects in Python, this can be done manually, but using the
@decorator syntax is clearer and thus preferred.
def foo():
# do something
def decorator(func):
# manipulate func
14
return func
foo = decorator(foo)
# Manually decorate
@decorator
def bar():
# Do something
# bar() is decorated
This mechanism is useful for separating concerns and avoiding external un-related logic polluting the core logic
of the function or method. A good example of a piece of functionality that is better handled with decoration is
memoization or caching: you want to store the results of an expensive function in a table and use them directly instead
of recomputing them when they have already been computed. This is clearly not part of the function logic.
Good
count = 1
msg = a string
def func():
pass # Do something
Using short functions or methods helps reduce the risk of using the same name for two unrelated things.
It is better to use different names even for things that are related, when they have a different type:
Bad
items = a b c d # This is a string...
items = items.split( ) # ...becoming a list
items = set(items) # ...and then a set
There is no efficiency gain when reusing names: the assignments will have to create new objects anyway. However,
when the complexity grows and each assignment is separated by other lines of code, including if branches and loops,
it becomes harder to ascertain what a given variables type is.
Some coding practices, like functional programming, recommend never reassigning a variable. In Java this is done
with the final keyword. Python does not have a final keyword and it would be against its philosophy anyway. However,
2.1. Structuring Your Project
15
it may be a good discipline to avoid assigning to a variable more than once, and it helps in grasping the concept of
mutable and immutable types.
One consequence of this difference in behavior is that mutable types are not stable, and therefore cannot be used as
dictionary keys.
Using properly mutable types for things that are mutable in nature and immutable types for things that are fixed in
nature helps to clarify the intent of the code.
For example, the immutable equivalent of a list is the tuple, created with (1, 2). This tuple is a pair that cannot be
changed in-place, and can be used as a key for a dictionary.
One peculiarity of Python that can surprise beginners is that strings are immutable. This means that when constructing
a string from its parts, it is much more efficient to accumulate the parts in a list, which is mutable, and then glue (join)
the parts together when the full string is needed. One thing to notice, however, is that list comprehensions are better
and faster than constructing a list in a loop with calls to append().
Bad
# create a concatenated string from 0 to 19 (e.g. "012..1819")
nums = ""
for n in range(20):
nums += str(n)
# slow and inefficient
print nums
Good
# create a concatenated string from 0 to 19 (e.g. "012..1819")
nums = []
for n in range(20):
nums.append(str(n))
print "".join(nums) # much more efficient
Best
# create a concatenated string from 0 to 19 (e.g. "012..1819")
nums = [str(n) for n in range(20)]
print "".join(nums)
One final thing to mention about strings is that using join() is not always best. In the instances where you are
creating a new string from a pre-determined number of strings, using the addition operator is actually faster, but in
16
cases like above or in cases where you are adding to an existing string, using join() should be your preferred
method.
foo = foo
bar = bar
foobar = foo + bar # This is good
foo += ooo # This is bad, instead you should do:
foo = .join([foo, ooo])
Note: You can also use the % formatting operator to concatenate a pre-determined number of strings besides
str.join() and +. However, according to PEP 3101, the % operator became deprecated in Python 3.1 and will be
replaced by the str.format() method in the later versions.
foo = foo
bar = bar
foobar = %s%s % (foo, bar) # It is OK
foobar = {0}{1}.format(foo, bar) # It is better
foobar = {foo}{bar}.format(foo=foo, bar=bar) # It is best
17
Bad
def make_complex(*args):
x, y = args
return dict(**locals())
Good
def make_complex(x, y):
return {x: x, y: y}
In the good code above, x and y are explicitly received from the caller, and an explicit dictionary is returned. The
developer using this function knows exactly what to do by reading the first and last lines, which is not the case with
the bad example.
One statement per line
While some compound statements such as list comprehensions are allowed and appreciated for their brevity and their
expressiveness, it is bad practice to have two disjoint statements on the same line of code.
Bad
print one; print two
if x == 1: print one
if <complex comparison> and <other complex comparison>:
# do something
Good
print one
print two
if x == 1:
print one
cond1 = <complex comparison>
cond2 = <other complex comparison>
if cond1 and cond2:
# do something
Function arguments
Arguments can be passed to functions in four different ways.
1. Positional arguments are mandatory and have no default values. They are the simplest form of arguments and
they can be used for the few function arguments that are fully part of the functions meaning and their order is natural.
For instance, in send(message, recipient) or point(x, y) the user of the function has no difficulty
remembering that those two functions require two arguments, and in which order.
In those two cases, it is possible to use argument names when calling the functions and, doing so, it is possible to
switch the order of arguments, calling for instance send(recipient=World, message=Hello) and
point(y=2, x=1) but this reduces readability and is unnecessarily verbose, compared to the more straightforward
calls to send(Hello, World) and point(1, 2).
2. Keyword arguments are not mandatory and have default values. They are often used for optional parameters sent
to the function. When a function has more than two or three positional parameters, its signature is more difficult to
18
remember and using keyword argument with default values is helpful. For instance, a more complete send function could be defined as send(message, to, cc=None, bcc=None). Here cc and bcc are optional, and
evaluate to None when they are not passed another value.
Calling a function with keyword arguments can be done in multiple ways in Python, for example it is possible to
follow the order of arguments in the definition without explicitly naming the arguments, like in send(Hello,
World, Cthulhu, God), sending a blind carbon copy to God. It would also be possible to name arguments in another order, like in send(Hello again, World, bcc=God, cc=Cthulhu). Those
two possibilities are better avoided without any strong reason to not follow the syntax that is the closest to the function
definition: send(Hello, World, cc=Cthulhu, bcc=God).
As a side note, following YAGNI principle, it is often harder to remove an optional argument (and its logic inside the
function) that was added just in case and is seemingly never used, than to add a new optional argument and its logic
when needed.
3. The arbitrary argument list is the third way to pass arguments to a function. If the function intention is better expressed by a signature with an extensible number of positional arguments, it can be defined with the *args constructs.
In the function body, args will be a tuple of all the remaining positional arguments. For example, send(message,
*args) can be called with each recipient as an argument: send(Hello, God, Mom, Cthulhu),
and in the function body args will be equal to (God, Mom, Cthulhu).
However, this construct has some drawbacks and should be used with caution. If a function receives a list of arguments
of the same nature, it is often more clear to define it as a function of one argument, that argument being a list or any sequence. Here, if send has multiple recipients, it is better to define it explicitly: send(message, recipients)
and call it with send(Hello, [God, Mom, Cthulhu]). This way, the user of the function can
manipulate the recipient list as a list beforehand, and it opens the possibility to pass any sequence, including iterators,
that cannot be unpacked as other sequences.
4. The arbitrary keyword argument dictionary is the last way to pass arguments to functions. If the function
requires an undetermined series of named arguments, it is possible to use the **kwargs construct. In the function
body, kwargs will be a dictionary of all the passed named arguments that have not been caught by other keyword
arguments in the function signature.
The same caution as in the case of arbitrary argument list is necessary, for similar reasons: these powerful techniques
are to be used when there is a proven necessity to use them, and they should not be used if the simpler and clearer
construct is sufficient to express the functions intention.
It is up to the programmer writing the function to determine which arguments are positional arguments and which are
optional keyword arguments, and to decide whether to use the advanced techniques of arbitrary argument passing. If
the advice above is followed wisely, it is possible and enjoyable to write Python functions that are:
easy to read (the name and arguments need no explanations)
easy to change (adding a new keyword argument does not break other parts of the code)
Avoid the magical wand
A powerful tool for hackers, Python comes with a very rich set of hooks and tools allowing to do almost any kind of
tricky tricks. For instance, it is possible to do each of the following:
change how objects are created and instantiated
change how the Python interpreter imports modules
it is even possible (and recommended if needed) to embed C routines in Python.
However, all these options have many drawbacks and it is always better to use the most straightforward way to achieve
your goal. The main drawback is that readability suffers greatly when using these constructs. Many code analysis
tools, such as pylint or pyflakes, will be unable to parse this magic code.
19
We consider that a Python developer should know about these nearly infinite possibilities, because it instills confidence
that no impassable problem will be on the way. However, knowing how and particularly when not to use them is very
important.
Like a kung fu master, a Pythonista knows how to kill with a single finger, and never to actually do it.
We are all consenting adults
As seen above, Python allows many tricks, and some of them are potentially dangerous. A good example is that any
client code can override an objects properties and methods: there is no private keyword in Python. This philosophy,
very different from highly defensive languages like Java, which give a lot of mechanisms to prevent any misuse, is
expressed by the saying: We are all consenting adults.
This doesnt mean that, for example, no properties are considered private, and that no proper encapsulation is possible
in Python. Rather, instead of relying on concrete walls erected by the developers between their code and others,
the Python community prefers to rely on a set of conventions indicating that these elements should not be accessed
directly.
The main convention for private properties and implementation details is to prefix all internals with an underscore.
If the client code breaks this rule and accesses these marked elements, any misbehavior or problems encountered if
the code is modified is the responsibility of the client code.
Using this convention generously is encouraged: any method or property that is not intended to be used by client code
should be prefixed with an underscore. This will guarantee a better separation of duties and easier modification of
existing code; it will always be possible to publicize a private property, while privatising a public property might be a
much harder operation.
Returning values
When a function grows in complexity it is not uncommon to use multiple return statements inside the functions
body. However, in order to keep a clear intent and a sustainable readability level, it is preferable to avoid returning
meaningful values from many output points in the body.
There are two main cases for returning values in a function: the result of the function return when it has been processed
normally, and the error cases that indicate a wrong input parameter or any other reason for the function to not be able
to complete its computation or task.
If you do not wish to raise exceptions for the second case, then returning a value, such as None or False, indicating
that the function could not perform correctly might be needed. In this case, it is better to return as early as the incorrect
context has been detected. It will help to flatten the structure of the function: all the code after the return-because-oferror statement can assume the condition is met to further compute the functions main result. Having multiple such
return statements is often necessary.
However, when a function has multiple main exit points for its normal course, it becomes difficult to debug the returned
result, so it may be preferable to keep a single exit point. This will also help factoring out some code paths, and the
multiple exit points are a probable indication that such a refactoring is needed.
def complex_function(a, b, c):
if not a:
return None # Raising an
if not b:
return None # Raising an
# Some complex code trying to
# Resist temptation to return
if not x:
# Some Plan-B computation
20
return x
# One single exit point for the returned value x will help
# when maintaining the code.
2.2.2 Idioms
A programming idiom, put simply, is a way to write code. The notion of programming idioms is discussed amply at
c2 and at Stack Overflow.
Idiomatic Python code is often referred to as being Pythonic.
Although there usually is one and preferably only one obvious way to do it; the way to write idiomatic Python
code can be non-obvious to Python beginners. So, good idioms must be consciously acquired.
Some common Python idioms follow:
Unpacking
If you know the length of a list or tuple, you can assign names to its elements with unpacking. For example, since
enumerate() will provide a tuple of two elements for each item in list:
for index, item in enumerate(some_list):
# do something with index and item
Note: Many Python style guides recommend the use of a single underscore _ for throwaway variables rather
than the double underscore __ recommended here. The issue is that _ is commonly used as an alias for the
gettext() function, and is also used at the interactive prompt to hold the value of the last operation. Using a
double underscore instead is just as clear and almost as convenient, and eliminates the risk of accidentally interfering
with either of these other use cases.
21
This will set the value of the variable word to spam. This idiom can be applied to lists and tuples.
Sometimes we need to search through a collection of things. Lets look at two options: lists and dictionaries.
Take the following code for example:
d = {s: [], p: [], a: [], m: []}
l = [s, p, a, m]
def lookup_dict(d):
return s in d
def lookup_list(l):
return s in l
Even though both functions look identical, because lookup_dict is utilizing the fact that dictionaries in Python are
hashtables, the lookup performance between the two is very different. Python will have to go through each item in the
list to find a matching case, which is time consuming. By analysing the hash of the dictionary, finding keys in the dict
can be done very quickly. For more information see this StackOverflow page.
22
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless youre Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, its a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- lets do more of those!
For some examples of good Python style, see this Stack Overflow question or these slides from a Python user group.
2.2.4 PEP 8
PEP 8 is the de-facto code style guide for Python.
Conforming your Python code to PEP 8 is generally a good idea and helps make code more consistent when working
on projects with other developers. There is a command-line program, pep8, that can check your code for conformance.
Install it by running the following command in your Terminal:
$ pip install pep8
2.2.5 Conventions
Here are some conventions you should follow to make your code easier to read.
Check if variable equals a constant
You dont need to explicitly compare a value to True, or None, or 0 - you can just add it to the if statement. See Truth
Value Testing for a list of what is considered false.
Bad:
if attr == True:
print True!
if attr == None:
print attr is None!
Good:
# Just check the value
if attr:
print attr is truthy!
# or check for the opposite
23
if not attr:
print attr is falsey!
# or, since None is considered false, explicitly check for it
if attr is None:
print attr is None!
Bad:
d = {hello: world}
if d.has_key(hello):
print d[hello]
# prints world
else:
print default_value
Good:
d = {hello: world}
print d.get(hello, default_value) # prints world
print d.get(thingy, default_value) # prints default_value
# Or:
if hello in d:
print d[hello]
Good:
a
b
#
b
= [3, 4, 5]
= [i for i in a if i > 4]
Or:
= filter(lambda x: x > 4, a)
Bad:
# Add three to all list members.
a = [3, 4, 5]
for i in range(len(a)):
a[i] += 3
24
Good:
a
a
#
a
= [3, 4, 5]
= [i + 3 for i in a]
Or:
= map(lambda i: i + 3, a)
The enumerate() function has better readability than handling a counter manually. Moreover, it is better optimized
for iterators.
Read From a File
Use the with open syntax to read from files. This will automatically close files for you.
Bad:
f = open(file.txt)
a = f.read()
print a
f.close()
Good:
with open(file.txt) as f:
for line in f:
print line
The with statement is better because it will ensure you always close the file, even if an exception is raised inside the
with block.
Line Continuations
When a logical line of code is longer than the accepted limit, you need to split it over multiple physical lines. The
Python interpreter will join consecutive lines if the last character of the line is a backslash. This is helpful in some
cases, but should usually be avoided because of its fragility: a white space added to the end of the line, after the
backslash, will break the code and may have unexpected results.
A better solution is to use parentheses around your elements. Left with an unclosed parenthesis on an end-of-line the
Python interpreter will join the next line until the parentheses are closed. The same behavior holds for curly and square
braces.
Bad:
my_very_big_string = """For a long time I used to go to bed early. Sometimes, \
when I had put out my candle, my eyes would close so quickly that I had not even \
time to say Im going to sleep."""
from some.deep.module.inside.a.module import a_nice_function, another_nice_function, \
yet_another_nice_function
25
Good:
my_very_big_string = (
"For a long time I used to go to bed early. Sometimes, "
"when I had put out my candle, my eyes would close so quickly "
"that I had not even time to say Im going to sleep."
)
from some.deep.module.inside.a.module import (
a_nice_function, another_nice_function, yet_another_nice_function)
However, more often than not having to split long logical line is a sign that you are trying to do too many things at the
same time, which may hinder readability.
26
2.4 Documentation
Readability is a primary focus for Python developers, in both project and code documentation. Following some simple
best practices can save both you and others a lot of time.
2.4. Documentation
27
reStructuredText
Most Python documentation is written with reStructuredText. Its like Markdown with all the optional extensions built
in.
The reStructuredText Primer and the reStructuredText Quick Reference should help you familiarize yourself with its
syntax.
In general, follow the comment section of PEP 8 (the Python Style Guide).
Commenting Sections of Code
Do not use triple-quote strings to comment code. This is not a good practice, because line-oriented command-line tools
such as grep will not be aware that the commented code is inactive. It is better to add hashes at the proper indentation
level for every commented line. Your editor probably has the ability to do this easily, and it is worth learning the
comment/uncomment toggle.
Docstrings and Magic
Some tools use docstrings to embed more-than-documentation behavior, such as unit test logic. Those can be nice, but
you wont ever go wrong with vanilla heres what this does.
Docstrings versus Block comments
These arent interchangeable. For a function or class, the leading comment block is a programmers note. The
docstring describes the operation of the function or class:
# This function slows down program execution for some reason.
def square_and_rooter(x):
"""Returns the square root of self times self."""
...
See also:
Further reading on docstrings: PEP 257
29
As of Python 2.7 unittest also includes its own test discovery mechanisms.
unittest in the standard library documentation
Doctest
The doctest module searches for pieces of text that look like interactive Python sessions in docstrings, and then
executes those sessions to verify that they work exactly as shown.
Doctests have a different use case than proper unit tests: they are usually less detailed and dont catch special cases or
obscure regression bugs. They are useful as an expressive documentation of the main use cases of a module and its
components. However, doctests should run automatically each time the full test suite runs.
A simple doctest in a function:
def square(x):
"""Squares x.
>>> square(2)
4
>>> square(-2)
4
"""
return x * x
if __name__ == __main__:
import doctest
doctest.testmod()
When running this module from the command line as in python module.py, the doctests will run and complain
if anything is not behaving as described in the docstrings.
2.5.2 Tools
py.test
py.test is a no-boilerplate alternative to Pythons standard unittest module.
30
Despite being a fully-featured and extensible test tool, it boasts a simple syntax. Creating a test suite is as easy as
writing a module with a couple of functions:
# content of test_sample.py
def func(x):
return x + 1
def test_answer():
assert func(3) == 5
>
E
E
def test_answer():
assert func(3) == 5
assert 4 == 5
+ where 4 = func(3)
test_sample.py:5: AssertionError
========================= 1 failed in 0.02 seconds =========================
is far less work than would be required for the equivalent functionality with the unittest module!
py.test
Nose
nose extends unittest to make testing easier.
$ pip install nose
nose provides automatic test discovery to save you the hassle of manually creating test suites. It also provides numerous
plugins for features such as xUnit-compatible test output, coverage reporting, and test selection.
nose
tox
tox is a tool for automating test environment management and testing against multiple interpreter configurations
$ pip install tox
tox allows you to configure complicated multi-parameter test matrices via a simple ini-style configuration file.
tox
31
Unittest2
unittest2 is a backport of Python 2.7s unittest module which has an improved API and better assertions over the one
available in previous versions of Python.
If youre using Python 2.6 or below, you can install it with pip
$ pip install unittest2
You may want to import the module under the name unittest to make porting code to newer versions of the module
easier in the future
import unittest2 as unittest
class MyTest(unittest.TestCase):
...
This way if you ever switch to a newer Python version and no longer need the unittest2 module, you can simply change
the import in your test module without the need to change any other code.
unittest2
mock
unittest.mock is a library for testing in Python. As of Python 3.3, it is available in the standard library.
For older versions of Python:
$ pip install mock
It allows you to replace parts of your system under test with mock objects and make assertions about how they have
been used.
For example, you can monkey-patch a method:
from mock import MagicMock
thing = ProductionClass()
thing.method = MagicMock(return_value=3)
thing.method(3, 4, 5, key=value)
thing.method.assert_called_with(3, 4, 5, key=value)
To mock classes or objects in a module under test, use the patch decorator. In the example below, an external search
system is replaced with a mock that always returns the same result (but only for the duration of the test).
def mock_search(self):
class MockSearchQuerySet(SearchQuerySet):
def __iter__(self):
return iter(["foo", "bar", "baz"])
return MockSearchQuerySet()
# SearchForm here refers to the imported class reference in myapp,
# not where the SearchForm class itself is imported from
@mock.patch(myapp.SearchForm.search, mock_search)
def test_new_watchlist_activities(self):
# get_search_results runs a search and iterates over the result
self.assertEqual(len(myapp.get_search_results(q="fish")), 3)
Mock has many other ways you can configure it and control its behavior.
mock
32
A new list is created each time the function is called if a second argument isnt provided, so that the output is:
[12]
[42]
A new list is created once when the function is defined, and the same list is used in each successive call.
Pythons default arguments are evaluated once when the function is defined, not each time the function is called (like
it is in say, Ruby). This means that if you use a mutable default argument and mutate it, you will and have mutated
that object for all future calls to the function as well.
What You Should Do Instead
Create a new object each time the function is called, by using a default arg to signal that no argument was provided
(None is often a good choice).
33
A list containing five functions that each have their own closed-over i variable that multiplies their argument, producing:
0
2
4
6
8
34
Whats particularly nasty about this gotcha is the seemingly prevalent misinformation that this has something to do
with lambdas in Python. Functions created with a lambda expression are in no way special, and in fact the same
exact behavior is exhibited by just using an ordinary def:
def create_multipliers():
multipliers = []
for i in range(5):
def multiplier(x):
return i * x
multipliers.append(multiplier)
return multipliers
35
To help you choose one for your project, theres a license chooser, use it.
More Permissive
PSFL (Python Software Foundation License) for contributing to Python itself
MIT / BSD / ISC
MIT (X11)
New BSD
ISC
Apache
Less Permissive:
LGPL
GPL
GPLv2
GPLv3
A good overview of licenses with explanations of what one can, cannot, and must do using a particular software can
be found at tl;drLegal.
36
CHAPTER 3
Scenario Guide
This part of the guide focuses on tool and module advice based on different scenarios.
37
RabbitMQ
RabbitMQ is an open source message broker software that implements the Advanced Message Queuing Protocol
(AMQP). The RabbitMQ server is written in the Erlang programming language and is built on the Open Telecom
Platform framework for clustering and failover. Client libraries to interface with the broker are available for all major
programming languages.
Homepage
GitHub Organization
3.2.1 Context
WSGI
The Web Server Gateway Interface (or WSGI for short) is a standard interface between web servers and Python
web application frameworks. By standardizing behavior and communication between web servers and Python web
frameworks, WSGI makes it possible to write portable Python web code that can be deployed in any WSGI-compliant
web server. WSGI is documented in PEP 3333.
3.2.2 Frameworks
Broadly speaking, a web framework consists of a set of libraries and a main handler within which you can build custom
code to implement a web application (i.e. an interactive web site). Most web frameworks include patterns and utilities
to accomplish at least the following:
URL Routing Matches an incoming HTTP request to a particular piece of Python code to be invoked
Request and Response Objects Encapsulate the information received from or sent to a users browser
Template Engine Allows for separating Python code implementing an applications logic from the HTML (or other)
output that it produces
Development Web Server Runs an HTTP server on development machines to enable rapid development; often automatically reloads server-side code when files are updated
Django
Django is a batteries included web application framework. By providing many utilities and patterns out of the box,
Django aims to make it possible to build complex, database-backed web applications quickly, while encouraging best
practices in code written using it.
Django has a large and active community, and many pre-built re-usable modules that can be incorporated into a new
project as-is, or customized to fit your needs.
There are annual Django conferences in the United States and in Europe.
38
Flask
Flask is a microframework for Python. Rather than aiming to provide everything you could possibly need, Flask
implements the most commonly-used core components of a web application framework, like URL routing, request and
response objects, and templates. As a user of Flask, it is therefore up to you to choose and integrate other components
you may need, such as database access or form generation and validation. For many popular modules, Extensions may
already exist to suit your needs.
Support for flask can best be found in its mailing list. Just shoot an email to flask@librelist.com and reply to the
confirmation email.
Werkzeug
Werkzeug is not actually a real framework, but rather a very powerful set of tools for building web applications. It
provides URL routing utilities, request and response objects and a basic development server. It is mostly used where
users need bigger flexibility for their application that is not commonly found in other web frameworks.
Support can be found on its mailing list.
Tornado
Tornado is a scalable, non-blocking web server and web application framework with a relative simple usage. Tornado
is known for its high performance. It was initially developed for friendfeed , a real time chat and blog system.
In the Jinja2 template engine example it is used to serve the rendered pages.
Pyramid
Pyramid lies somewhere between a big framework like Django and the microframeworks: It comes with a lot of
libraries and functionality and can thus not be considered lightweight. On the other hand, it does not provide all the
functionality Django does. Instead Pyramid brings basic support for most regular tasks and provides a great deal of
extensibility. Additionally, Pyramid has a huge focus on complete documentation. As a little extra it comes with the
Werkzeug Debugger which allows you to debug a running web application in the browser.
Support can also be found in the documentation.
39
Gunicorn
Gunicorn (Green Unicorn) is a WSGI server used to serve Python applications. It is a Python interpretation of the
Ruby Unicorn server. Unicorn is designed to be lightweight, easy to use, and uses many UNIX idioms. Gunicorn is
not designed to face the internet it was designed to run behind Nginx which buffers slow requests and takes care of
other important considerations. A sample setup for Nginx + Gunicorn can be found in the Gunicorn help.
uWSGI
uWSGI is a full stack for building hosting services. In addition to process management, process monitoring, and
other functionality, uWSGI acts as an application server for various programming languages and protocols - including
Python and WSGI. uWSGI can either be run as a stand-alone web router, or be run behind a full web server (such as
Nginx or Apache). In the latter case, a web server can configure uWSGI and an applications operation over the uwsgi
protocol. uWSGIs web server support allows for dynamically configuring Python, passing environment variables and
further tuning. For full details, see uWSGI magic variables.
3.2.6 Hosting
Platform-as-a-Service
Platform-as-a-Service (PaaS) is a type of cloud computing infrastructure which abstracts and manages infrastructure,
routing, and scaling of web applications. When using PaaS, application developers can focus on writing application
code rather than needing to be concerned with deployment details.
Most PaaS services offer a command-line interface that developers can use to set up and interrogate configuration, and
to deploy new releases of an application to the service.
PaaS services and their partners offer add-on functionality which is well integrated into the platform, such as database
hosting, email services, logging, scheduled and background tasks, billing and payment, etc.
Heroku
Herokus Cedar stack offers first class support for Python 2.7 applications.
Heroku allows you to run as many Python web applications as you like, 24/7 and free of charge. Heroku is best
described as a horizontal scaling platform. They start to charge you once you scale your application to run on more
than one Dyno (abstracted servers) at a time.
Heroku maintains articles on using Python with Heroku as well as step-by-step instructions on how to set up your first
application.
40
DotCloud
DotCloud supports WSGI applications and background/worker tasks natively on their platform. Web applications run
Python version 2.6, use nginx and uWSGI, and allow custom configuration of both for advanced users.
DotCloud uses a custom command-line API client which can work with applications managed in git repositories or
any other version control system.
DotCloud has a free plan with limited database size, and without extra services (caching. . . ).
See the DotCloud documentation on Python for more information and help getting started.
Gondor
Gondor is a PaaS specialized for deploying Django and Pinax applications. Gondor recommends Django version 1.6
and supports any WSGI application on Python version 2.7. Gondor can automatically configure your Django site if
you use local_settings.py for site-specific configuration information.
Gondor has a guide on deploying Django projects.
3.2.7 Templating
Most WSGI applications are responding to HTTP requests to serve content in HTML or other markup languages.
Instead of generating directly textual content from Python, the concept of separation of concerns advises us to use
templates. A template engine manages a suite of template files, with a system of hierarchy and inclusion to avoid
unnecessary repetition, and is in charge of rendering (generating) the actual content, filling the static content of the
templates with the dynamic content generated by the application.
As template files are sometimes written by designers or front-end developers, it can be difficult to handle increasing
complexity.
Some general good practices apply to the part of the application passing dynamic content to the template engine, and
to the templates themselves.
Template files should be passed only the dynamic content that is needed for rendering the template. Avoid the
temptation to pass additional content just in case: it is easier to add some missing variable when needed than
to remove a likely unused variable later.
Many template engines allow for complex statements or assignments in the template itself, and many allow some
Python code to be evaluated in the templates. This convenience can lead to uncontrolled increase in complexity,
and often make it harder to find bugs.
It is often necessary to mix JavaScript templates with HTML templates. A sane approach to this design is to
isolate the parts where the HTML template passes some variable content to the JavaScript code.
Jinja2
Jinja2 is a template engine which is similar to the Django template system with some extra features. It is a text-based
template language and thus can be used to generate any markup. It allows customization of filters, tags, tests and
globals, and unlike the template system implemented in the Django Framework, also allows calling functions. Jinja2
is released under the BSD license.
Here some important html tags in Jinja2:
41
{# This is a comment #}
{# The next tag is a variable output: #}
{{title}}
{# Tag for a block, can be replaced through inheritance with other html code #}
{% block head %}
<h1>This is the head!</h1>
{% endblock %}
{# Output of an array as an iteration #}
{% for item in list %}
<li>{{ item }}</li>
{% endfor %}
The next listings is an example of a web site in combination with the tornado web server. Tornado is not very
complicate to use.
# import Jinja2
from jinja2 import Environment, FileSystemLoader
# import Tornado
import tornado.ioloop
import tornado.web
# Load template file templates/site.html
TEMPLATE_FILE = "site.html"
templateLoader = FileSystemLoader( searchpath="templates/" )
templateEnv = Environment( loader=templateLoader )
template = templateEnv.get_template(TEMPLATE_FILE)
# List for famous movie rendering
movie_list = [[1,"The Hitchhikers Guide to the Galaxy"],[2,"Back to future"],[3,"Matrix"]]
# template.render() returns a string which contains the rendered html
html_output = template.render(list=movie_list,
title="Here is my favorite movie list")
# Handler for main page
class MainHandler(tornado.web.RequestHandler):
def get(self):
# Returns rendered template string to the browser request
self.write(html_output)
# Assign handler to the server root (127.0.0.1:PORT/)
application = tornado.web.Application([
(r"/", MainHandler),
])
PORT=8884
if __name__ == "__main__":
# Setup the server
application.listen(PORT)
tornado.ioloop.IOLoop.instance().start()
The base.html file can be used as base for all site pages which are for example implemented in the content block.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<html xmlns="http://www.w3.org/1999/xhtml">
42
<head>
<link rel="stylesheet" href="style.css" />
<title>{{title}} - My Webpage</title>
</head>
<body>
<div id="content">
{# In the next line the content from the site.html template will be added #}
{% block content %}{% endblock %}
</div>
<div id="footer">
{% block footer %}
© Copyright 2013 by <a href="http://domain.invalid/">you</a>.
{% endblock %}
</div>
</body>
The next listing is our site page (site.html) loaded in the Python app which extends base.html. The content
block is automatically set into the corresponding block in the base.html page.
<!{% extends "base.html" %}
{% block content %}
<p class="important">
<div id="content">
<h2>{{title}}</h2>
<p>{{ list_title }}</p>
<ul>
{% for item in list %}
<li>{{ item[0]}} : {{ item[1]}}</li>
{% endfor %}
</ul>
</div>
</p>
{% endblock %}
References
43
Next we will use requests.get to retrieve the web page with our data, parse it using the html module and save
the results in tree:
page = requests.get(http://econpy.pythonanywhere.com/ex/001.html)
tree = html.fromstring(page.text)
tree now contains the whole HTML file in a nice tree structure which we can go over two different ways: XPath and
CSSSelect. In this example, we will focus on the former.
XPath is a way of locating information in structured documents such as HTML or XML documents. A good introduction to XPath is on W3Schools .
There are also various tools for obtaining the XPath of elements such as FireBug for Firefox or the Chrome Inspector.
If youre using Chrome, you can right click an element, choose Inspect element, highlight the code, right click again
and choose Copy XPath.
After a quick analysis, we see that in our page the data is contained in two elements - one is a div with title buyername and the other is a span with class item-price:
<div title="buyer-name">Carson Busses</div>
<span class="item-price">$29.95</span>
Knowing this we can create the correct XPath query and use the lxml xpath function like this:
#This will create a list of buyers:
buyers = tree.xpath(//div[@title="buyer-name"]/text())
#This will create a list of prices
prices = tree.xpath(//span[@class="item-price"]/text())
Congratulations! We have successfully scraped all the data we wanted from a web page using lxml and Requests. We
have it stored in memory as two lists. Now we can do all sorts of cool stuff with it: we can analyze it using Python or
we can save it to a file and share it with the world.
Some more cool ideas to think about are modifying this script to iterate through the rest of the pages of this example
dataset, or rewriting this application to use threads for improved speed.
44
3.4.1 Clint
clint is a Python module which is filled with very useful tools for developing command-line applications. It supports
features such as; CLI colors and indents, simple and powerful column printer, iterator based progress bars and implicit
argument handling.
3.4.2 Click
click is an upcoming Python package for creating command-line interfaces in a composable way with as little code as
possible. This Command-line Interface Creation Kit is highly configurable but comes with good defaults out of the
box.
3.4.3 docopt
docopt is a lightweight, highly Pythonic package that allows creating command-line interfaces easily and intuitively,
by parsing POSIX-style usage instructions.
3.4.4 Plac
Plac is a simple wrapper over the Python standard library argparse, which hides most of its complexity by using a
declarative interface: the argument parser is inferred rather than written down by imperatively. This module targets
especially unsophisticated users, programmers, sys-admins, scientists and in general people writing throw-away scripts
for themselves, who choose to create a command-line interface because it is quick and simple.
3.4.5 Cliff
Cliff is a framework for building command-line programs. It uses setuptools entry points to provide subcommands,
output formatters, and other extensions. The framework is meant to be used to create multi-level commands such as
subversion and git, where the main program handles some basic argument parsing and then invokes a sub-command
to do the work.
45
3.5.1 Camelot
Camelot provides components for building applications on top of Python, SQLAlchemy and Qt. It is inspired by the
Django admin interface.
The main resource for information is the website: http://www.python-camelot.com and the mailing list
https://groups.google.com/forum/#!forum/project-camelot
3.5.2 Cocoa
Note: The Cocoa framework is only available on OS X. Dont pick this if youre writing a cross-platform application!
3.5.3 GTk
PyGTK provides Python bindings for the GTK+ toolkit. Like the GTK+ library itself, it is currently licensed under the
GNU LGPL. It is worth noting that PyGTK only currently supports the Gtk-2.X API (NOT Gtk-3.0). It is currently
recommended that PyGTK not be used for new projects and that existing applications be ported from PyGTK to
PyGObject.
3.5.4 Kivy
Kivy is a Python library for development of multi-touch enabled media rich applications. The aim is to allow for quick
and easy interaction design and rapid prototyping, while making your code reusable and deployable.
Kivy is written in Python, based on OpenGL and supports different input devices such as: Mouse, Dual Mouse, TUIO,
WiiMote, WM_TOUCH, HIDtouch, Apples products and so on.
Kivy is actively being developed by a community and free to use. It operates on all major platforms (Linux, OSX,
Windows, Android).
The main resource for information is the website: http://kivy.org
PyObjC
Note: Only available on OS X. Dont pick this if youre writing a cross-platform application.
PySide
PySide is a Python binding of the cross-platform GUI toolkit Qt.
http://developer.qt.nokia.com/wiki/PySideDownloads/
46
PyQt
Note: If your software does not fully comply with the GPL you will need a commercial license!
PyQt provides Python bindings for the Qt Framework (see below).
http://www.riverbankcomputing.co.uk/software/pyqt/download
3.5.6 Qt
Qt is a cross-platform application framework that is widely used for developing software with a GUI but can also be
used for non-GUI applications.
3.5.7 Tk
Tkinter is a thin object-oriented layer on top of Tcl/Tk. It has the advantage of being included with the Python
standard library, making it the most convenient and compatible toolkit to program with.
Both Tk and Tkinter are available on most Unix platforms, as well as on Windows and Macintosh systems. Starting
with the 8.0 release, Tk offers native look and feel on all platforms.
Theres a good multi-language Tk tutorial with Python examples at TkDocs. Theres more information available on
the Python Wiki.
3.5.8 wxPython
wxPython is a GUI toolkit for the Python programming language. It allows Python programmers to create programs
with a robust, highly functional graphical user interface, simply and easily. It is implemented as a Python extension
module (native code) that wraps the popular wxWidgets cross platform GUI library, which is written in C++.
Install (Stable) wxPython go to http://www.wxpython.org/download.php#stable and download the appropriate package for your OS.
3.6 Databases
3.6.1 DB-API
The Python Database API (DB-API) defines a standard interface for Python database access modules. Its documented
in PEP 249. Nearly all Python database modules such as sqlite3, psycopg and mysql-python conform to this interface.
Tutorials that explain how to work with modules that conform to this interface can be found here and here.
3.6. Databases
47
3.6.2 SQLAlchemy
SQLAlchemy is a commonly used database toolkit. Unlike many database libraries it not only provides an ORM layer
but also a generalized API for writing database-agnostic code without SQL.
$ pip install sqlalchemy
3.7 Networking
3.7.1 Twisted
Twisted is an event-driven networking engine. It can be used to build applications around many different networking protocols, including http servers and clients, applications using SMTP, POP3, IMAP or SSH protocols, instant
messaging and much more.
3.7.2 PyZMQ
PyZMQ is the Python binding for ZeroMQ, which is a high-performance asynchronous messaging library. One great
advantage of ZeroMQ is that it can be used for message queuing without a message broker. The basic patterns for this
are:
request-reply: connects a set of clients to a set of services. This is a remote procedure call and task distribution
pattern.
publish-subscribe: connects a set of publishers to a set of subscribers. This is a data distribution pattern.
push-pull (or pipeline): connects nodes in a fan-out / fan-in pattern that can have multiple steps, and loops. This
is a parallel task distribution and collection pattern.
For a quick start, read the ZeroMQ guide.
3.7.3 gevent
gevent is a coroutine-based Python networking library that uses greenlets to provide a high-level synchronous API on
top of the libev event loop.
48
The following code will create two tasks that we can use: memory_usage and deploy. The former will output the
memory usage on each machine. The latter will ssh into each server, cd to our project directory, activate the virtual
environment, pull the newest codebase, and restart the application server.
from fabric.api import cd, env, prefix, run, task
env.hosts = [my_server1, my_server2]
@task
def memory_usage():
run(free -m)
@task
def deploy():
with cd(/var/www/project-env/project):
with prefix(. ../bin/activate):
run(git pull)
run(touch app.wsgi)
With the previous code saved in a file named fabfile.py, we can check memory usage with:
$ fab memory_usage
[my_server1] Executing task memory
[my_server1] run: free -m
[my_server1] out:
total
[my_server1] out: Mem:
6964
[my_server1] out: -/+ buffers/cache:
[my_server1] out: Swap:
0
used
1897
1509
0
free
5067
5455
0
shared
0
buffers
166
cached
222
[my_server2]
[my_server2]
[my_server2]
[my_server2]
[my_server2]
[my_server2]
used
902
148
1
free
764
1517
894
shared
0
buffers
180
cached
572
Additional features include parallel execution, interaction with remote programs, and host grouping.
Fabric Documentation
3.8.2 Salt
Salt is an open source infrastructure management tool. It supports remote command execution from a central point
3.8. Systems Administration
49
(master host) to multiple hosts (minions). It also supports system states which can be used to configure multiple servers
using simple template files.
Salt supports Python versions 2.6 and 2.7 and can be installed via pip:
$ pip install salt
After configuring a master server and any number of minion hosts, we can run arbitrary shell commands or use prebuilt modules of complex commands on our minions.
The following command lists all available minion hosts, using the ping module.
$ salt * test.ping
The host filtering is accomplished by matching the minion id, or using the grains system. The grains system uses static
host information like the operating system version or the CPU architecture to provide a host taxonomy for the Salt
modules.
The following command lists all available minions running CentOS using the grains system:
$ salt -G os:CentOS test.ping
Salt also provides a state system. States can be used to configure the minion hosts.
For example, when a minion host is ordered to read the following state file, it will install and start the Apache server:
apache:
pkg:
- installed
service:
- running
- enable: True
- require:
- pkg: apache
State files can be written using YAML, the Jinja2 template system or pure Python.
Salt Documentation
3.8.3 Psutil
Psutil is an interface to different system information (e.g. CPU, memory, disks, network, users and processes).
Here is an example to be aware of some server overload. If any of the tests (net, CPU) fail, it will send an email.
# Functions to get system values:
from psutil import cpu_percent, net_io_counters
# Functions to take a break:
from time import sleep
# Package for email services:
import smtplib
import string
MAX_NET_USAGE = 400000
MAX_ATTACKS = 4
attack = 0
counter = 0
while attack <= MAX_ATTACKS:
sleep(4)
counter = counter + 1
# Check the cpu usage
50
A full terminal application like a widely extended top which is based on psutil and with the ability of a client-server
monitoring is glance.
3.8.4 Ansible
Ansible is an open source system automation tool. The biggest advantage over Puppet or Chef is it does not require an
agent on the client machine. Playbooks are Ansibles configuration, deployment, and orchestration language and are
written in in YAML with Jinja2 for templating.
Ansible supports Python versions 2.6 and 2.7 and can be installed via pip:
$ pip install ansible
Ansible requires an inventory file that describes the hosts to which it has access. Below is an example of a host and
playbook that will ping all the hosts in the inventory file.
Here is an example inventory file: hosts.yml
[server_name]
127.0.0.1
51
The Ansible playbook will ping all of the servers in the hosts.yml file. You can also select groups of servers using
Ansible. For more information about Ansible, read the Ansible Docs.
3.8.5 Chef
Todo
Write about Chef
Chef Documentation
3.8.6 Puppet
Puppet is an IT Automation and configuration management software from Puppet Labs that allows System Administrators to define the state of their IT Infrastructure, thereby providing an elegant way to manage their fleet of physical
and virtual machines.
Puppet is available both as an Open Source and an Enterprise variant. Modules are small,shareable units of code
written to automate or define the state of a system. Puppet Forge is a repository for modules written by the community
for Open Source and Enterprise Puppet.
Puppet Agents are installed on nodes whose state needs to be monitored or changed. A desginated server known as
the Puppet Master is responsible for orchastrating the agent nodes.
Agent nodes send basic facts about the system such as to the operating system, kernel, architecture, ip address, hostname etc. to the Puppet Master. The Puppet Master then compiles a catalog with information provided by the agents
on how each node should be configured and sends it to the agent. The agent enforces the change as prescribed in the
catalog and sends a report back to the Puppet Master.
Facter is an interesting tool that ships with Puppet that pulls basic facts about the System. These facts can be referenced
as a variable while writing your Puppet modules.
$ facter kernel
Linux
$ facter operatingsystem
Ubuntu
Writing Modules in Puppet is pretty straight forward. Puppet Manifests together form Puppet Modules. Puppet
manifest end with an extension of .pp. Here is an example of Hello World in Puppet.
notify { This message is getting logged into the agent node:
#As nothing is specified in the body the resource title
#the notification message by default.
}
Here is another example with system based logic. Note how the operatingsystem fact is being used as a variable
prepended with the $ sign. Similarly, this holds true for other facts such as hostname which can be referenced by
$hostname
notify{ Mac Warning:
message => $operatingsystem ? {
Darwin => This seems to be a Mac.,
default => I am a PC.,
},
}
52
There are several resource types for Puppet but the package-file-service paradigm is all you need for undertaking
majority of the configuration management. The following Puppet code makes sure that the OpenSSH-Server package
is installed in a system and the sshd service is notified to restart everytime the sshd configuration file is changed.
package { openssh-server:
ensure => installed,
}
file { /etc/ssh/sshd_config:
source
=> puppet:///modules/sshd/sshd_config,
owner
=> root,
group
=> root,
mode
=> 640,
notify
=> Service[sshd], # sshd will restart
# whenever you edit this
# file
require => Package[openssh-server],
}
service { sshd:
ensure
=> running,
enable
=> true,
hasstatus => true,
hasrestart=> true,
}
3.8.7 Blueprint
Todo
Write about Blueprint
3.8.8 Buildout
Todo
Write about Buildout
Buildout Website
53
integration is verified by an automated build (including test) to detect integration errors as quickly as
possible. Many teams find that this approach leads to significantly reduced integration problems and
allows a team to develop cohesive software more rapidly.
3.9.2 Jenkins
Jenkins CI is an extensible continuous integration engine. Use it.
3.9.3 Buildbot
Buildbot is a Python system to automate the compile/test cycle to validate code changes.
3.9.4 Mule
Mule is a lightweight integration platform that enables you to connect anything, anywhere. You can use Mule to
intelligently manage message routing, data mapping, orchestration, reliability, security and scalability between nodes.
Plug other systems and applications into Mule and let it handle all the communication between systems, enabling you
to track and monitor everything that happens.
3.9.5 Tox
tox is an automation tool providing packaging, testing and deployment of Python software right from the console or
CI server. It is a generic virtualenv management and test command line tool which provides the following features:
Checking that packages install correctly with different Python versions and interpreters
Running tests in each of the environments, configuring your test tool of choice
Acting as a front-end to Continuous Integration servers, reducing boilerplate and merging CI and shell-based
testing.
3.9.6 Travis-CI
Travis-CI is a distributed CI server which builds tests for open source projects for free. It provides multiple workers
to run Python tests on and seamlessly integrates with GitHub. You can even have it comment on your Pull Requests
whether this particular changeset breaks the build or not. So if you are hosting your code on GitHub, travis-ci is a
great and easy way to get started with Continuous Integration.
In order to get started, add a .travis.yml file to your repository with this example content:
language: python
python:
- "2.6"
- "2.7"
- "3.2"
- "3.3"
# command to install dependencies
script: python tests/test_all_of_the_units.py
branches:
only:
- master
54
This will get your project tested on all the listed Python versions by running the given script, and will only build the
master branch. There are a lot more options you can enable, like notifications, before and after steps and much more.
The travis-ci docs explain all of these options, and are very thorough.
In order to activate testing for your project, go to the travis-ci site and login with your GitHub account. Then activate
your project in your profile settings and youre ready to go. From now on, your projects tests will be run on every
push to GitHub.
3.10 Speed
CPython, the most commonly used implementation of Python, is slow for CPU bound tasks. PyPy is fast.
Using a slightly modified version of David Beazleys CPU bound test code (added loop for multiple tests), you can see
the difference between CPython and PyPys processing.
# PyPy
$ ./pypy -V
Python 2.7.1 (7773f8fc4223, Nov 18 2011, 18:47:10)
[PyPy 1.7.0 with GCC 4.4.3]
$ ./pypy measure2.py
0.0683999061584
0.0483210086823
0.0388588905334
0.0440690517426
0.0695300102234
# CPython
$ ./python -V
Python 2.7.1
$ ./python measure2.py
1.06774401665
1.45412397385
1.51485204697
1.54693889618
1.60109114647
3.10.1 Context
The GIL
The GIL (Global Interpreter Lock) is how Python allows multiple threads to operate at the same time. Pythons
memory management isnt entirely thread-safe, so the GIL is required to prevent multiple threads from running the
same Python code at once.
David Beazley has a great guide on how the GIL operates. He also covers the new GIL in Python 3.2. His results show
that maximizing performance in a Python application requires a strong understanding of the GIL, how it affects your
specific application, how many cores you have, and where your application bottlenecks are.
C Extensions
The GIL
Special care must be taken when writing C extensions to make sure you register your threads with the interpreter.
3.10. Speed
55
3.10.2 C Extensions
Cython
Cython implements a superset of the Python language with which you are able to write C and C++ modules for Python.
Cython also allows you to call functions from compiled C libraries. Using Cython allows you to take advantage of
Pythons strong typing of variables and operations.
Heres an example of strong typing with Cython:
def primes(int kmax):
"""Calculation of prime numbers with additional
Cython keywords"""
cdef int n, k, i
cdef int p[1000]
result = []
if kmax > 1000:
kmax = 1000
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result
This implementation of an algorithm to find prime numbers has some additional keywords compared to the next one,
which is implemented in pure Python:
def primes(kmax):
"""Calculation of prime numbers in standard Python syntax"""
p= range(1000)
result = []
if kmax > 1000:
kmax = 1000
k = 0
n = 2
while k < kmax:
i = 0
while i < k and n % p[i] != 0:
i = i + 1
if i == k:
p[k] = n
k = k + 1
result.append(n)
n = n + 1
return result
Notice that in the Cython version you declare integers and integer arrays to be compiled into C types while also
creating a Python list:
56
What is the difference? In the upper Cython version you can see the declaration of the variable types and the integer
array in a similar way as in standard C. For example cdef int n,k,i in line 3. This additional type declaration (i.e. integer)
allows the Cython compiler to generate more efficient C code from the second version. While standard Python code
is saved in *.py files, Cython code is saved in *.pyx files.
Whats the difference in speed? Lets try it!
import time
#activate pyx compiler
import pyximport
pyximport.install()
#primes implemented with Cython
import primesCy
#primes implemented with Python
import primes
print "Cython:"
t1= time.time()
print primesCy.primes(500)
t2= time.time()
print "Cython time: %s" %(t2-t1)
print ""
print "Python"
t1= time.time()
print primes.primes(500)
t2= time.time()
print "Python time: %s" %(t2-t1)
The pyximport module allows you to import *.pyx files (e.g., primesCy.pyx) with the Cython-compiled version
of the primes function. The pyximport.install() command allows the Python interpreter to start the Cython compiler
directly to generate C-code, which is automatically compiled to a *.so C-library. Cython is then able to import this
library for you in your Python code, easily and efficiently. With the time.time() function you are able to compare the
time between these 2 different calls to find 500 prime numbers. On a standard notebook (dual core AMD E-450 1.6
GHz), the measured values are:
Cython time: 0.0054 seconds
Python time: 0.0566 seconds
3.10. Speed
57
Pyrex
Shedskin?
Numba
Todo
Write about Numba and the autojit compiler for NumPy
3.10.3 Threading
Threading
Spawning Processes
Multiprocessing
3.11.2 Tools
IPython
IPython is an enhanced version of Python interpreter, which provides features of great interest to scientists. The inline
mode allow graphics and plots to be displayed in the terminal (Qt based version). Moreover, the notebook mode supports literate programming and reproducible science generating a web-based Python notebook. This notebook allows
you to store chunks of Python code along side the results and additional comments (HTML, LaTeX, Markdown). The
notebook can then be shared and exported in various file formats.
58
3.11.3 Libraries
NumPy
NumPy is a low level library written in C (and FORTRAN) for high level mathematical functions. NumPy cleverly
overcomes the problem of running slower algorithms on Python by using multidimensional arrays and functions that
operate on arrays. Any algorithm can then be expressed as a function on arrays, allowing the algorithms to be run
quickly.
NumPy is part of the SciPy project, and is released as a separate library so people who only need the basic requirements
can use it without installing the rest of SciPy.
NumPy is compatible with Python versions 2.4 through to 2.7.2 and 3.1+.
Numba
Numba is an Numpy aware Python compiler (just-in-time (JIT) specializing compiler) which compiles annotated
Python (and Numpy) code to LLVM (Low Level Virtual Machine) through special decorators. Briefly, Numba uses a
system that compiles Python code with LLVM to code which can be natively executed at runtime.
SciPy
SciPy is a library that uses NumPy for more mathematical functions. SciPy uses NumPy arrays as the basic data structure, and comes with modules for various commonly used tasks in scientific programming, including linear algebra,
integration (calculus), ordinary differential equation solving and signal processing.
Matplotlib
Matplotlib is a flexible plotting library for creating interactive 2D and 3D plots that can also be saved as manuscriptquality figures. The API in many ways reflects that of MATLAB, easing transition of MATLAB users to Python.
Many examples, along with the source code to re-create them, are available in the matplotlib gallery.
Pandas
Pandas is data manipulation library based on Numpy which provides many useful functions for accessing, indexing,
merging and grouping data easily. The main data structure (DataFrame) is close to what could be found in the R
statistical package; that is, heterogeneous data tables with name indexing, time series operations and auto-alignment
of data.
Rpy2
Rpy2 is a Python binding for the R statistical package allowing the execution of R functions from Python and passing
data back and forth between the two environments. Rpy2 is the object oriented implementation of the Rpy bindings.
PsychoPy
PsychoPy is a library for cognitive scientists allowing the creation of cognitive psychology and neuroscience experiments. The library handles presentation of stimuli, scripting of experimental design and data collection.
59
3.11.4 Resources
Installation of scientific Python packages can be troublesome, as many of these packages are implemented as Python
C extensions which need to be compiled. This section lists various so-called scientific Python distributions which
provide precompiled and easy-to-install collections of scientific Python packages.
Unofficial Windows Binaries for Python Extension Packages
Many people who do scientific computing are on Windows, yet many of the scientific computing packages are notoriously difficult to build and install on this platform. Christoph Gohlke however, has compiled a list of Windows
binaries for many useful Python packages. The list of packages has grown from a mainly scientific Python resource to
a more general list. If youre on Windows, you may want to check it out.
Anaconda
Continuum Analytics offers the Anaconda Python Distribution which includes all the common scientific Python packages as well as many packages related to data analytics and big data. Anaconda itself is free, and Continuum sells a
number of proprietary add-ons. Free licenses for the add-ons are available for academics and researchers.
Canopy
Canopy is another scientific Python distribution, produced by Enthought. A limited Canopy Express variant is
available for free, but Enthought charges for the full distribution. Free licenses are available for academics.
60
and then you can get the child elements name like this:
obj.root.child[name]
3.13.2 xmltodict
xmltodict is another simple library that aims at making XML feel like working with JSON.
An XML file like this:
<mydocument has="an attribute">
<and>
<many>elements</many>
<many>more elements</many>
</and>
<plus a="complex">
element as well
</plus>
</mydocument>
and then you can access elements, attributes and values like this:
doc[mydocument][@has] # == uan attribute
doc[mydocument][and][many] # == [uelements, umore elements]
doc[mydocument][plus][@a] # == ucomplex
doc[mydocument][plus][#text] # == uelement as well
xmltodict also lets you roundtrip back to XML with the unparse function, has a streaming mode suitable for handling
files that dont fit in memory and supports namespaces.
61
3.14 Cryptography
3.14.1 Cryptography
Cryptography is an actively developed library that provides cryptographic recipes and primitives. It supports Python
2.6-2.7, Python 3.2+ and PyPy.
Cryptography is divided into two layers of recipes and hazardous materials (hazmat). The recipes layer provides
simple API for proper symmetric encryption and the hazmat layer provides low-level cryptographic primitives.
Installation
$ pip install cryptography
Example
Example code using high level symmetric encryption recipe:
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"A really secret message. Not for prying eyes.")
plain_text = cipher_suite.decrypt(cipher_text)
3.14.2 PyCrypto
PyCrypto is another library, which provides secure hash functions and various encryption algorithms. It supports
Python version 2.1 through 3.3.
Installation
$ pip install pycrypto
Example
from Crypto.Cipher import AES
# Encryption
encryption_suite = AES.new(This is a key123, AES.MODE_CBC, This is an IV456)
cipher_text = encryption_suite.encrypt("A really secret message. Not for prying eyes.")
# Decryption
decryption_suite = AES.new(This is a key123, AES.MODE_CBC, This is an IV456)
plain_text = decryption_suite.decrypt(cipher_text)
62
CHAPTER 4
63
This runs a simple http server running on port 9000 and will list all packages (like MyPackage). Now you can install
MyPackage using any Python package installer. Using Pip, you would do it like:
$ pip install --extra-index-url=http://127.0.0.1:9000/ MyPackage
Having a folder with the same name as the package name is crucial here. I got fooled by that, one time. But if you
feel that creating a folder called MyPackage and keeping MyPackage.tar.gz inside that, is redundant, you can
still install MyPackage using:
$ pip install
http://127.0.0.1:9000/MyPackage.tar.gz
pypiserver
Pypiserver is a minimal PyPI compatible server. It can be used to serve a set of packages to easy_install or pip.
It includes helpful features like an administrative command (-U ) which will update all its packages to their latest
versions found on PyPI.
S3-Hosted PyPi
One simple option for a personal PyPi server is to use Amazon S3. A prerequisite for this is that you have an Amazon
AWS account with an S3 bucket.
1. Install all your requirements from PyPi or another source
2. Install pip2pi
pip install git+https://github.com/wolever/pip2pi.git
3. Follow pip2pi README for pip2tgz and dir2pi commands
pip2tgz packages/ YourPackage (or pip2tgz packages/ -r requirements.txt)
dir2pi packages/
4. Upload the new files
Use a client like Cyberduck to sync the entire packages folder to your s3 bucket
Make sure you upload packages/simple/index.html as well as all new files and directories
5. Fix new file permissions
By default, when you upload new files to the S3 bucket, they will have the wrong permissions set.
Use the Amazon web console to set the READ permission of the files to EVERYONE.
If you get HTTP 403 when trying to install a package, make sure youve set the permissions correctly.
6. All done
You can now install your package with pip install --index-url=http://your-s3-bucket/packages/simple
YourPackage
64
Arch
Useful Tools
fpm
alien
4.2.1 Comparison
Solutions and platforms/features supported:
SoluWintion
dows
bbFreeze yes
py2exe
yes
pyInyes
staller
cx_Freeze yes
py2app no
Linux OS
X
yes
yes
no
no
yes
yes
Python
3
no
yes
no
License
MIT
MIT
GPL
One-file
mode
no
yes
yes
Zipfile
import
yes
yes
no
Eggs pkg_resources
support
yes
yes
no
no
yes
no
yes
no
yes
yes
PSF
MIT
no
no
yes
yes
yes
yes
yes
yes
no
yes
Note: Freezing Python code on Linux into a Windows executable was only once supported in PyInstaller and later
dropped..
Note: All solutions need MS Visual C++ dll to be installed on target machine, except py2app. Only Pyinstaller makes
self-executable exe that bundles the dll when passing --onefile to Configure.py.
4.2.2 Windows
bbFreeze
Prerequisite is to install Python, Setuptools and pywin32 dependency on Windows.
Todo
Write steps for most basic .exe
65
py2exe
Prerequisite is to install Python on Windows.
1. Download and install http://sourceforge.net/projects/py2exe/files/py2exe/
2. Write setup.py (List of configuration options):
from distutils.core import setup
import py2exe
setup(
windows=[{script: foobar.py}],
)
6. Provide the Microsoft Visual C runtime DLL. Two options: globally install dll on target machine or distribute
dll alongside with .exe.
PyInstaller
Prerequisite is to have installed Python, Setuptools and pywin32 dependency on Windows.
Most basic tutorial
Manual
4.2.3 OS X
py2app
PyInstaller
4.2.4 Linux
bbFreeze
PyInstaller
66
CHAPTER 5
Development Environment
textwidth=79
shiftwidth=4
tabstop=4
expandtab
softtabstop=4
shiftround
autoindent
"
"
"
"
"
"
"
With these settings, newlines are inserted after 79 characters and indentation is set to 4 spaces per tab. If you also use
Vim for other languages, there is a handy plugin called indent, which handles indentation settings for Python source
files.
There is also a handy syntax plugin called syntax featuring some improvements over the syntax file included in Vim
6.1.
These plugins supply you with a basic environment for developing in Python. To get the most out of Vim, you should
continually check your code for syntax errors and PEP8 compliance. Luckily PEP8 and Pyflakes will do this for you.
If your Vim is compiled with +python you can also utilize some very handy plugins to do these checks from within
the editor.
For PEP8 checking, install the vim-pep8 plugin, and for pyflakes you can install vim-pyflakes. Now you can map the
functions Pep8() or Pyflakes() to any hotkey or action you want in Vim. Both plugins will display errors at the
bottom of the screen, and provide an easy way to jump to the corresponding line. Its very handy to call these functions
whenever you save a file. In order to do this, add the following lines to your .vimrc:
67
If you are already using syntastic, you can set it to run Pyflakes on write and show errors and warnings in the quickfix
window. An example configuration to do that which also shows status and warning messages in the statusbar would
be:
set
set
set
let
let
statusline+=%#warningmsg#
statusline+=%{SyntasticStatuslineFlag()}
statusline+=%*
g:syntastic_auto_loc_list=1
g:syntastic_loc_list_height=5
Python-mode
Python-mode is a complex solution for working with Python code in Vim. It has:
Asynchronous Python code checking (pylint, pyflakes, pep8, mccabe) in any combination
Code refactoring and autocompletion with Rope
Fast Python folding
Virtualenv support
Search through Python documentation and run Python code
Auto PEP8 error fixes
And more.
SuperTab
SuperTab is a small Vim plugin that makes code completion more convenient by using <Tab> key or any other
customized keys.
Emacs
Emacs is another powerful text editor. It is fully programmable (lisp), but it can be some work to wire up correctly. A
good start if youre already an Emacs user is Python Programming in Emacs at EmacsWiki.
1. Emacs itself comes with a Python mode.
2. Python ships with an alternate version: python-mode.el
3. Fabin Ezequiel Gallinas python.el provides nice functionality and behavior out of the box
TextMate
TextMate brings Apples approach to operating systems into the world of text editors. By bridging UNIX
underpinnings and GUI, TextMate cherry-picks the best of both worlds to the benefit of expert scripters
and novice users alike.
68
Sublime Text
Sublime Text is a sophisticated text editor for code, markup and prose. Youll love the slick user interface,
extraordinary features and amazing performance.
Sublime Text has excellent support for editing Python code and uses Python for its plugin API. It also has a diverse
variety of plugins, some of which allow for in-editor PEP8 checking and code linting.
Atom
Atom is a hackable text editor for the 21st century, built on atom-shell, and based on everything we love
about our favorite editors.
Atom is web native (HTML, CSS, JS), focusing on modular design and easy plugin development. It comes with
native package control and plethora of packages. Recommended for Python development is Linter combined with
linter-flake8.
5.1.2 IDEs
PyCharm / IntelliJ IDEA
PyCharm is developed by JetBrains, also known for IntelliJ IDEA. Both share the same code base and most of PyCharms features can be brought to IntelliJ with the free Python Plug-In. There are two versions of PyCharm: Professional Edition (Free 30-day trial) and Community Edition(Apache 2.0 License) with less features.
Eclipse
The most popular Eclipse plugin for Python development is Aptanas PyDev.
Komodo IDE
Komodo IDE is developed by ActiveState and is a commercial IDE for Windows, Mac, and Linux. KomodoEdit is the
open source alternative.
Spyder
Spyder is an IDE specifically geared toward working with scientific Python libraries (namely Scipy). It includes
integration with pyflakes, pylint and rope.
Spyder is open-source (free), offers code completion, syntax highlighting, a class and function browser, and object
inspection.
WingIDE
WingIDE is a Python specific IDE. It runs on Linux, Windows and Mac (as an X11 application, which frustrates some
Mac users).
WingIDE offers code completion, syntax highlighting, source browser, graphical debugger and support for version
control systems.
69
NINJA-IDE
NINJA-IDE (from the recursive acronym: Ninja-IDE Is Not Just Another IDE) is a cross-platform IDE, specially
designed to build Python applications, and runs on Linux/X11, Mac OS X and Windows desktop operating systems.
Installers for these platforms can be downloaded from the website.
NINJA-IDE is open-source software (GPLv3 licence) and is developed in Python and Qt. The source files can be
downloaded from GitHub.
Eric (The Eric Python IDE)
Eric is a full featured Python IDE offering sourcecode autocompletion, syntax highlighting, support for version control
systems, python 3 support, integrated web browser, python shell, integrated debugger and a flexible plug-in system.
Written in python, it is based on the Qt gui toolkit, integrating the Scintilla editor control. Eric is an open-source
software (GPLv3 licence) with more than ten years of active development.
virtualenv venv will create a folder in the current directory which will contain the Python executable files, and
a copy of the pip library which you can use to install other packages. The name of the virtual environment (in this
case, it was venv) can be anything; omitting the name will place the files in the current directory instead.
To start using the virtual environment, run:
$ source venv/bin/activate
The name of the current virtual environment will now appear on the left of the prompt (e.g.
(venv)Your-Computer:your_project UserName$) to let you know that its active. From now on,
any package that you install using pip will be placed in the venv folder, isolated from the global Python installation.
Install packages as usual:
$ pip install requests
To stop using an environment, simply type deactivate. To remove the environment, just remove the directory it
was installed into. (In this case, it would be rm -rf venv.)
70
Other Notes
Running virtualenv with the option --no-site-packages will not include the packages that are installed
globally. This can be useful for keeping the package list clean in case it needs to be accessed later. [This is the default
behavior for virtualenv 1.7 and later.]
In order to keep your environment consistent, its a good idea to freeze the current state of the environment packages.
To do this, run
$ pip freeze > requirements.txt
This will create a requirements.txt file, which contains a simple list of all the packages in the current environment, and their respective versions. Later it will be easier for a different developer (or you, if you need to re-create the
environment) to install the same packages using the same versions:
$ pip install -r requirements.txt
This can help ensure consistency across installations, across deployments, and across developers.
Lastly, remember to exclude the virtual environment folder from source control by adding it to the ignore list.
virtualenvwrapper
Virtualenvwrapper makes virtualenv a pleasure to use by wrapping the command line API with a nicer CLI.
$ pip install virtualenvwrapper
This will prevent your virtualenvs from relying on your (global) site packages directory, so that they are completely
separate. [Note: This is the default behavior for virtualenv 1.7 and later]
71
BPython
bpython is an alternative interface to the Python interpreter for Unix-like operating systems. It has the following
features:
In-line syntax highlighting.
Readline-like autocomplete with suggestions displayed as you type.
Expected parameter list for any Python function.
Rewind function to pop the last line of code from memory and re-evaluate.
Send entered code off to a pastebin.
Save entered code to a file.
Auto-indentation.
Python 3 support.
$ pip install bpython
5.2.1 virtualenv
virtualenv is a tool to create isolated Python environments.
Install it via pip:
$ pip install virtualenv
Basic Usage
1. Create a virtual environment:
$ virtualenv venv
This creates a copy of Python in whichever directory you ran the command in, placing it in a folder named venv.
You can also use a Python interpreter of your choice.
72
You can then begin installing any new modules without affecting the system default Python or other virtual environments.
3. If you are done working in the virtual environment for the moment, you can deactivate it:
$ deactivate
This puts you back to the systems default Python interpreter with all its installed libraries.
To delete a virtual environment, just delete its folder.
After a while, though, you might end up with a lot of virtual environments littered across your system, and its possible
youll forget their names or where they were placed.
5.2.2 virtualenvwrapper
virtualenvwrapper provides a set of commands which makes working with virtual environments much more pleasant.
It also places all your virtual environments in one place.
To install (make sure virtualenv is already installed):
$ pip install virtualenvwrapper
$ export WORKON_HOME=~/Envs
$ source /usr/local/bin/virtualenvwrapper.sh
Basic Usage
1. Create a virtual environment:
$ mkvirtualenv venv
virtualenvwrapper provides tab-completion on environment names. It really helps when you have a lot of environments and have trouble remembering their names.
workon also deactivates whatever environment you are currently in, so you can quickly switch between environments.
73
4. To delete:
$ rmvirtualenv venv
5.2.3 autoenv
When you cd into a directory containing a .env, autoenv automagically activates the environment.
Install it on Mac OS X using brew:
$ brew install autoenv
And on Linux:
$ git clone git://github.com/kennethreitz/autoenv.git ~/.autoenv
$ echo source ~/.autoenv/activate.sh >> ~/.bashrc
74
CHAPTER 6
Additional Notes
This part of the guide, which is mostly prose, begins with some background information about Python, then focuses
on next steps.
6.1 Introduction
From the official Python website:
Python is a general-purpose, high-level programming language similar to Tcl, Perl, Ruby, Scheme, or Java. Some of
its main key features include:
very clear, readable syntax
Pythons philosophy focuses on readability, from code blocks delineated with significant whitespace to intuitive
keywords in place of inscrutable punctuation.
extensive standard libraries and third party modules for virtually any task
Python is sometimes described with the words batteries included because of its extensive standard library,
which includes modules for regular expressions, file IO, fraction handling, object serialization, and much more.
Additionally, the Python Package Index is available for users to submit their packages for widespread use,
similar to Perls CPAN. There is a thriving community of very powerful Python frameworks and tools like the
Django web framework and the NumPy set of math routines.
integration with other systems
Python can integrate with Java libraries, enabling it to be used with the rich Java environment that corporate
programmers are used to. It can also be extended by C or C++ modules when speed is of the essence.
ubiquity on computers
Python is available on Windows, *nix, and Mac. It runs wherever the Java virtual machine runs, and the reference
implementation CPython can help bring Python to wherever there is a working C compiler.
friendly community
Python has a vibrant and large community which maintains wikis, conferences, countless repositories, mailing
lists, IRC channels, and so much more. Heck, the Python community is even helping to write this guide!
75
6.2.3 PEPs
PEPs are Python Enhancement Proposals. They describe changes to Python itself, or the standards around it.
There are three different types of PEPs (as defined by PEP 1):
Standards Describes a new feature or implementation.
Informational Describes a design issue, general guidelines, or information to the community.
Process Describes a process related to Python.
76
Notable PEPs
There are a few PEPs that could be considered required reading:
PEP 8: The Python Style Guide. Read this. All of it. Follow it.
PEP 20: The Zen of Python. A list of 19 statements that briefly explain the philosophy behind Python.
PEP 257: Docstring Conventions. Gives guidelines for semantics and conventions associated with Python
docstrings.
You can read more at The PEP Index.
Submitting a PEP
PEPs are peer-reviewed and accepted/rejected after much discussion. Anyone can write and submit a PEP
for review.
Heres an overview of the PEP acceptance workflow:
77
78
Python Koans
Python Koans is a port of Edgecases Ruby Koans. It uses a test-driven approach, q.v. TEST DRIVEN DESIGN
SECTION to provide an interactive tutorial teaching basic Python concepts. By fixing assertion statements that fail in
a test script, this provides sequential steps to learning Python.
For those used to languages and figuring out puzzles on their own, this can be a fun, attractive option. For those new
to Python and programming, having an additional resource or reference will be helpful.
Python Koans
More information about test driven development can be found at these resources:
Test Driven Development
A Byte of Python
A free introductory book that teaches Python at the beginner level, it assumes no previous programming experience.
A Byte of Python for Python 2.x A Byte of Python for Python 3.x
6.3.2 Advanced
Pro Python
This book is for intermediate to advanced Python programmers who are looking to understand how and why Python
works the way it does and how they can take their code to the next level.
Pro Python
Expert Python Programming
Expert Python Programming deals with best practices in programming Python and is focused on the more advanced
crowd.
It starts with topics like decorators (with caching, proxy, and context manager case-studies), method resolution order,
using super() and meta-programming, and general PEP 8 best practices.
It has a detailed, multi-chapter case study on writing and releasing a package and eventually an application, including a
chapter on using zc.buildout. Later chapters detail best practices with writing documentation, test-driven development,
version control, and optimization/profiling.
Expert Python Programming
A Guide to Pythons Magic Methods
This is a collection of blog posts by Rafe Kettler which explain magic methods in Python. Magic methods are
surrounded by double underscores (i.e. __init__) and can make classes and objects behave in different and magical
ways.
A Guide to Pythons Magic Methods
79
6.3.5 References
Python in a Nutshell
Python in a Nutshell, written by Alex Martelli, covers most cross-platform Pythons usage, from its syntax to built-in
libraries to advanced topics such as writing C extensions.
Python in a Nutshell
The Python Language Reference
This is Pythons reference manual, it covers the syntax and the core semantics of the language.
The Python Language Reference
80
6.4 Documentation
6.4.1 Official Documentation
The official Python Language and Library documentation can be found here:
Python 2.x
Python 3.x
6.4.3 pydoc
pydoc is a utlity that is installed when you install Python. It allows you to quickly retrieve and search for documentation from your shell. For example, if you needed a quick refresher on the time module, pulling up documentation
would be as simple as
$ pydoc time
The above command is essentially equivalent to opening the Python REPL and running
6.4. Documentation
81
>>> help(time)
6.5 News
6.5.1 Planet Python
This is an aggregate of Python news from a growing number of developers.
Planet Python
6.5.2 /r/python
/r/python is the Reddit Python community where users contribute and vote on Python-related news.
/r/python
Contribution notes and legal information are here (for those interested).
6.6 Contribute
Python-guide is under active development, and contributors are welcome.
If you have a feature request, suggestion, or bug report, please open a new issue on GitHub. To submit patches,
please send a pull request on GitHub. Once your changes get merged back in, youll automatically be added to the
Contributors List.
in
/var/build/user_builds/python-
in
/var/build/user_builds/python-
in
/var/build/user_builds/python-
in
/var/build/user_builds/python-
Todo
Write about Numba and the autojit compiler for NumPy
(The
original
entry
is
located
guide/checkouts/latest/docs/scenarios/speed.rst, line 227.)
Todo
Fill in Freezing Your Code stub
(The
original
entry
is
located
guide/checkouts/latest/docs/shipping/freezing.rst, line 13.)
Todo
Write steps for most basic .exe
(The
original
entry
is
located
guide/checkouts/latest/docs/shipping/freezing.rst, line 50.)
Todo
Embed
and
explain
YouTube
video
showing
python
code
reading:
http://www.youtube.com/watch?v=Jc8M9-LoEuo
This
may
require
installing
a
Sphinx
plugin.
https://bitbucket.org/birkenfeld/sphinxcontrib/src/a09f29fc16970f34350ca36ac7f229e00b1b1674/youtube?at=default
(The
original
entry
is
located
guide/checkouts/latest/docs/writing/reading.rst, line 41.)
in
/var/build/user_builds/python-
Todo
Include code examples of exemplary code from each of the projects listed. Explain why it is excellent
code. Use complex examples.
(The
original
entry
is
located
guide/checkouts/latest/docs/writing/reading.rst, line 43.)
in
/var/build/user_builds/python-
Todo
Explain techniques to rapidly identify data structures, algorithms and determine what the code is doing.
6.6. Contribute
83
(The
original
entry
is
located
guide/checkouts/latest/docs/writing/reading.rst, line 45.)
in
/var/build/user_builds/python-
in
/var/build/user_builds/python-
in
/var/build/user_builds/python-
in
/var/build/user_builds/python-
Todo
Write about Chef
Chef Documentation
(The
original
entry
is
located
guide/checkouts/latest/docs/scenarios/admin.rst, line 235.)
Todo
Write about Blueprint
(The
original
entry
is
located
guide/checkouts/latest/docs/scenarios/admin.rst, line 336.)
Todo
Write about Buildout
Buildout Website
(The
original
entry
is
located
guide/checkouts/latest/docs/scenarios/admin.rst, line 341.)
6.7 License
The Guide is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported license.
6.8.1 Relevancy
Strive to keep any contributions relevant to the purpose of The Guide.
Avoid including too much information on subjects that dont directly relate to Python development.
Prefer to link to other sources if the information is already out there. Be sure to describe what and why you are
linking.
84
6.8.2 Headings
Use the following styles for headings.
Chapter title:
#########
Chapter 1
#########
Page title:
===================
Time is an Illusion
===================
Section headings:
Lunchtime Doubly So
-------------------
6.8.3 Prose
Wrap text lines at 78 characters. Where necessary, lines may exceed 78 characters, especially if wrapping would make
the source text more difficult to read.
85
Python examples:
Descriptive title::
.. code-block:: python
def get_answer():
return 42
Prefer to use descriptive labels with inline links instead of leaving bare links:
Read the Sphinx Tutorial <http://sphinx.pocoo.org/tutorial.html>_
Avoid using labels such as click here, this, etc. preferring descriptive labels (SEO worthy) instead.
Warnings:
.. warning:: DONT PANIC
6.8.8 TODOs
Please mark any incomplete areas of The Guide with a todo directive. To avoid cluttering the Todo List, use a single
todo for stub documents or large incomplete sections.
86
.. todo::
Learn the Ultimate Answer to the Ultimate Question
of Life, The Universe, and Everything
87
88
Index
E
environment variable
PATH, 5, 7
P
PATH, 5, 7
Python Enhancement Proposals
PEP 1, 76
PEP 20, 22, 77
PEP 249, 47
PEP 257, 28, 77
PEP 3101, 17
PEP 3132, 21
PEP 3333, 38
PEP 8, 23, 67, 77, 79
PEP 8#comments, 28
89