jemnotesversion 2 / featuring python or see all/search

Jan 26
PiCloud has a clean and simple website, explaining their clean and simple way of adding python computing in the cloud. Here’s their example:
def func()
  # Do something.
  ...

import cloud
cloud.call(func)
Aug 5
Taking things to extremes when calculating the product of 1 through to 9.
# Python version.
prod = 1
for x in range(1, 10):
    prod *= x
prod
# Ruby version.
(1..9).inject :*
May 22
cProfile and friends work well for profiling larger programs where things are split into functions. For more detailed investigation, however, you need something like line_profiler, where you can see output like this (trimmed):
Line #      Hits         Time  Per Hit   + Time  Line Contents
==============================================================
...
202      1741        12946      7.4      0.0      for k in zip(K.I, K.J):
203      1740       222659    128.0      0.9          K[k] = randn()
...
Once installed, decorate any function worthy of analysis with @profile, run kernprof.py -l <scriptname.py> and then view results with python -m line_profiler <scriptname>.lprof. Works well.
May 21
I wrote down below about enumerate, which lets you access (index, item) pairs within a loop. I guess I thought it was a good idea, because I was about to write it here again. If you need both the index and item from a list, use enumerate:
for index, item in enumerate(items):
    pass
I haven’t used it in the intervening two months, but oh well.
May 1
This code will fail:
l = []
def app():
  l += (1,)
app()
It will fail with an UnboundLocalError. According to this website, this is because ‘If a value is assigned to a variable in a function body, the variable will be local, even if there is a global variable with the same name, and this global variable has been used before the assignment.’
This is quite different to the behaviour if you don’t try and perform an assignment, and so is a bit confusing. Of course, there are few cases where global variables are the correct tool, but sometimes they can be handy.
Apr 14
Could py.test be used for test-driven development in Python?
Mar 27
A few interesting things about Python that I had forgotten come from here. I’ve modified the examples.
First, you can use an instance’s namespace to give an interesting way to print strings. Sometimes this will be better:
# Basic version.
print 'My name is +s and I am +d.' + (p.name, p.age)
# Advanced version.
print 'My name is +(name)s and I am +(age)d.' + p.__dict__
There is also the enumerate function, which returns (index, item) pairs. For example,
{}{py}
for (index, item) in enumerate(items):
    print index, item
Dictionaries have a setdefault method. Consider these alternative pieces of code:
# Naive piece of code.
for (cat, amount) in data:
    if cat not in d:
        d[cat] = 0
    d[cat] += amount

# Simpler piece of code.
for (cat, amount) in data:
    d.setdefault(cat, 0)
    d[cat] += amount

# A cunning third option: setdefault also gets.
for (cat, amount) in data:
    d[cat] = d.setdefault(cat, 0) + amount
But, better than all of these optons is the new defaultdict in Python 2.5.
from collections import defaultdict
d = defaultdict(generator_function)

# Example like those above.
d = defaultdict(lambda: 0)
Finally, generator expressions: say we want to sum a large list that we generate on the fly (ie, we don’t care about the list):
sum(i**2 for i in xrange(1e5))
(I didn’t find much difference in evaluation time, though.)
Mar 17
The brief version first:
# fine.
assert expr1, expr2

# WRONG!
assert(expr1, expr2)
Be very careful with the syntax of assertions in Python! If you get the syntax wrong, the assertions will just always hold. This is particularly disastrous, as assertions are usually used to expose hidden bugs.
The exact syntax that you should use is assert expr1, expr2. This tests expr1 in the boolean sense, and raises an exception with value str(expr2) if expr1 is False. You can also just use assert expr1, which won’t give a special message. Note the total lack of brackets. If you insert parentheses, though, as in assert(thing1, thing2), what you are really doing is calling assert (thing1, thing2). That is, an assertion with expr1 being (thing1, thing2)! This will never evaluate to False, so you will never get an error.
Feb 11
This dictionary stores a limited number of entries, removing the oldest entries when it gets full. This could be used for a cache. Create a max-50-entry dictionary with f = FifoDict(50), for example.
class FifoDict(object):
    def __init__(self, maxentries=100):
        self.lastentries = []
        self.mydict = {}
        self.maxentries = maxentries

    def __getitem__(self, k):
        return self.mydict[k]

    def __setitem__(self, k, val):
        if len(self.lastentries) >= self.maxentries:
            del self.mydict[self.lastentries.pop()]
        self.mydict[k] = val
        self.lastentries.insert(0, k)

    def __str__(self):
        return str(self.mydict)

    def __repr__(self):
        return repr(self.mydict)
Nov 18
This page pointed out a bizarre but perfect way of fixing readline on mac. Apparently the GNU license doesn’t play nice for Apple so it doesn’t work by default. I did this:
sudo easy_install ipython
sudo easy_install -f http://ipython.scipy.org/dist/ readline
And suddenly readline worked in ipython on mac.
Sep 8
Aug 13
A quick way of finding out where a Python package lives:
>>> import pylab
>>> print pylab
<module 'pylab' from '/usr/lib/python2.5/site-packages/pylab.pyc'>
Aug 11
Run a Python script through a profiler, then interpret the output. I don’t think it’s worth the bother going through iPython; I couldn’t get %run -p to work properly.
At the shell:
> python -m cProfile -o outprof.tmp examples/bb30.py
Then, in iPython:
>>> import pstats
>>> p = pstats.Stats('outprof.tmp')
>>> p.strip_dirs().sort_stats('cumulative').print_stats(50)