Just some notes in my attempts to become more adept at python.
Python Package Basics
The waters here are a bit murky. What follows is my attempt to demurk them somewhat.
The first and basic package here is distutils. It comes with python. This is the basic standard package you use when you want to describe a particular package that you have written and you want to distribute to other people. At its core, it provides a way for you to describe your package so that other people may install it on their systems.
You just call
python setup.py install
and you're good to go.
Next we have setuptools, which provides the easy_install command.
Basically setuptools provides an extra layer on top of the plain vanilla distutils. You can think of distutils as providing metadata about the structure of your package, while setuptools provides extra information about dependencies and other things.
setuptools allows you to specify dependencies, and has a test hook which you can configure with the test_suite, tests_require, etc. You can then run tests like this:
python setup.py test
Note that this doesn't actually install any testing software, if any was was specified. The whole point is that it runs the tests outside of a python installation.
You can also declare "optional" fetures. All the extra metadata is used when you run easy_install, or when you run the setup.py script.
There is a package called Distribute which is a fork of setuptools.
Setuptools seems to be supplanted by pip. With pip, you just install a package like this on the command line:
pip install <package>
Or if you want a particular version:
pip install <package>==<version>
The setup.py file is read. It will download the package and its dependencies from the Python Package Index, though you can change that with the dependency_link option in the setup file.
See these guides:
Good info on the difference between the different python virtual env tools:
For python 2:
Use this to isolate different python environments.
cd <base directory for your virtualenvs> virtualenv --system-site-packages <virtualenv directory>
Or if you can choose not to give access to the global packages:
virtualenv --no-site-packages <virtualenv directory>
You switch into (activate) a virtualenv by running this:
Once you set everything up you can take a snapshot of the packages you have install like this:
pip freeze > requirements.txt
But do this after you have switched into/activated the virtual env.
This requirements file can be used with a new virtualenv to recreate the setup you had before:
pip install -r requirements.txt
You can also use virtualenvwrapper to manage difference virtualenvs. It give you the ability to switch between virtualenvs, create them, delete them, etc.
Several tools exist for checking python source code.
- pychecker. This one's pretty old, and it imports your code before doing anything. This makes it unsuitable for scripts with import side effects.
- pylint. Very comprehensive and chatty. Does not import your code. Developed from pychecker. There are ways to turn off some checks.
- pyflakes. Like pylint, but much less comprehensive. Doesn't catch very many errors. But fast, apparently.
- pep8. Style checker.
I'm thinking that pylint and pep8 might be the way to go here...
- all-in-one. One script which will run pylint, pep8, and unit tests for a file or package. Run this script via emacs on demand, in compilation mode.
- one-at-a-time. Like above, but there is a different way to run each phase of the check. In compilation mode, there is less of a reason for this, since you can list the errors from the different phases one after the other in the compilation buffer.
- on-the-fly. Using flymake, each phase would have to be executed one at a time.
Basically, a file ending in .py defines a module. A package is a collection of modules hanging off a common top-level directory
Python Web Stuff
This is just a page for jotting down notes on web related technologies in python. There's a boat load of web frameworks in Python: "X is an open source web framework written in Python" is something you see a lot of on Wikipedia.
Almost all of them provide some support for Web Server Gateway Interface (WSGI), a python centric interface between applications and web servers. The equivalent in Ruby is Rack.
Some python frameworks are fairly heavy weight. By that I mean that they include built-in support for DBs, ORMs, templates, etc. For example:
- Django is a full blown web framework. Has stuff for ORM and RDBMS. Not minimalist.
Grok, Zope2, Zope3, BlueBream are all Object Oriented Web Frameworks derived from the original Zope2 application. Uses a DB to store "Zope objects".
- Plone is a CMS built on top of Zope. Not what you'd call minimalist.
TurboGears fills the same niche as Django. Complete solution, not minimalistic.
- Uses Pylons uderneath, which seems a lot leaner
- Nagare is inspired by Seaside in Smalltalk, and uses continuations to provide a development experience much like the desktop.
Some are more lightweight:
- Flask. Brings together Jinga2 templating and the Werkzeug WSGI framework.
Python Argument Passing
I always need reminding about this one. From the programming "Python Book":
- Function calls
- func(value). Matched by position
- func(name=value). Matched by keyword.
- func(*sequence). This lets the caller pass in a sequence of arguments, and they will be unpacked into the functions positional arguments.
- func(**dict). This lets the caller pass in a dictionary of keyword arguments, and they will be unpacked into the functions keywords arguments.
- Function definitions
- def func(value). Positional argument.
- def func(name=value). Default argument.
- def func(*name). Collects calling positional arguments into a tuple.
- def func(**name). Collects calling keyword arguments into a dictionary.