If you want to profile a Python applictation, you can get some basic functionality with the cProfile and pstats modules, as described in the Python Profilers documentation. After some curious experimenting, I discovered that the pstats module provides a handy interactive mode. To my surprising, I didn't found any substantial documentation about this time saver, so let me entertain you with a dump of my findings.
python
Python: hiding attribute getters and setters behind standard attribute access
Look at this small snippet of Python code:
x = TooMuchAlcohol() x.value = 10 print x.value x.value = 'foo' print x.value x.value = [1,2,3] print x.value
Seems like pretty boring stuff, but look what it spits out:
20 foofoo [1, 2, 3, 1, 2, 3]
Oh my god, I see everythin' double!
Jython and PYTHONPATH
For some small tools or jobs I use Jython so I can (re)use some existing Java code, while still writing in Python. I also have various custom Python modules, of which I put the paths in my PYTHONPATH environment variable, so those modules are easily available in Python scripts and interactive sessions.
However, Jython does not automatically pick up the PYTHONPATH information, for reasons that seem to be discussed in this Jython development mailing list thread.
Jython 2.5 introduced the JYTHONPATH environmental variable as Jython-equivalent of PYTHONPATH, so setting both to the same value should do the trick for most use cases (unless you're working in a setup with incompatible Python an Jython versions).
For Jython versions prior to 2.5 (like 2.2.1 in my case), there is an easy "workaround", by invoking Jython with the appropriate -Dpython.path=foo/path:bar/path option. For example as follows:
Bar plots and legends in pylab/matplotlib
I like working with Matplotlib aka Pylab for my plotting needs in Python, but today I stumbled on a weird issue when creating a mixed bar + line plot with legend. Take the following snippet:
pylab.bar(x, yr, color='#88aa33', align='center', label='histogram') pylab.plot(x, yc, 'bo-', label='cumulative') pylab.legend()
This generates something like (note the entry overload in the legend):

[Update: this issue seems to be solved. The issue described here occurred with Matplotlib version 0.91.2. With Matplotlib 0.99.0 I don't have this problem anymore.]
Date tick control in pylab/matplotlib
Today I struggled a bit with pylab's plot_date function and overlapping date tick labels:

Script for easier adding subversion ignore rules
It's possible to make Subversion ignore unversioned files, so they don't show up in the status overview and you get a better signal to noise ratio. The command line interface to add ignore rules this is however a bit clunky. First, you need to remember following command:
svn propedit svn:ignore .
(which can be quite confusing, especially with the slightly similar looking but different propset command).
Then you get a file editor where you have to add the rule (make sure you remembered the file name or pattern you want to ignore), save the file and exit the editor.
A bit too much hoops to jump through for something that could be just one command like svn-ignore filename.
Parsing the subversion Id keyword with python
Another one for the category 'remainder-to-self' or 'write-it-on-a-sticky-note-somewhere'.
Parsing a subversion Id keyword is just a matter of some regular expression magic:
# use the built in regular expression library import re # the subversion Id keyword svnid = '$Id: svnidparse.py 1234 2008-02-19 09:59:27Z joske $' # bow for the mighty regular expression svnidrep = r'^\$Id: (?P<filename>.+) (?P<revision>\d+) (?P<date>\d{4}-\d{2}-\d{1,2}) (?P<time>\d{2}:\d{2}:\d{2})Z (?P<user>\w+) \$$' # parse the svn Id mo = re.match(svnidrep, svnid) # use it, like for example: print 'this is %s - revision %s (%s)' % mo.group('filename', 'revision', 'date')
hmm, calling it "black magic" would be better, probably.
Get name of current function and caller with Python
With the python module inspect, one can inspect (no kidding) the run-time python stack. Among other things, this makes it possible to get the name of the current function or callers. Handy for logging or debugging purposes. A simple script to illustrate:
import inspect # functions def whoami(): return inspect.stack()[1][3] def whosdaddy(): return inspect.stack()[2][3] def foo(): print "hello, I'm %s, daddy is %s" % (whoami(), whosdaddy()) bar() def bar(): print "hello, I'm %s, daddy is %s" % (whoami(), whosdaddy()) johny = bar # call them! foo() bar() johny()
output:
hello, I'm foo, daddy is ? hello, I'm bar, daddy is foo hello, I'm bar, daddy is ? hello, I'm bar, daddy is ?
get working directory in python
This is mainly a remainder to myself, because I always forget it and spend to much time to find it again. It's so simple/basic that it is almost embarrassing.
To get the current working directory of in a python script, like you have pwd (print working directory) on a Linux/Unix command line:
import os print os.getcwd()
a clash between locale settings, C extension Python modules and pylab (aka matplotlib)
This weekend I encountered a weird problem during programming C extension modules for Python. For some obscure reason floats from my C extension modules were formatted with a comma as separater (e.g. 123,456) instead of with the more familiar point (e.g. 123.456). Obviously some locale related problem. Most of my desktop and applications are set up for Dutch (my native language), but when I'm programming/working I use English and scientific conventions (e.g. a point as decimal separator). After isolating the problem I found out it was related to importing the pylab (aka Matplotlib) module (which I started using for plotting graphs and figures from Python). The following situation illustrates the problem.