I keep getting bitten on the bum with Python and ‘proper’ programming things. Today it’s this:


In [253]: 8**14
Out[253]: 4398046511104L

In [254]: 8**(numpy.int32(14))
Out[254]: 0

I would feel guilty about this if I had to specify the type of numbers I was using, but this is a dynamic programming language! How the hell am I supposed to know that raising an int by a numpy.int32 will force the result to also be an numpy.int32 (and therefore (silently) 0 because int32 can’t hold my number) whereas raising an int by an int will result in a long? Why didn’t the int32 become a long also? Or an int64 or something that could hold my number?

Gah!

Advertisements

Nasty Python Things

March 27, 2009

So I seem to keep writing commands that look like this:

delta[t][q] = max(
    [delta[tau][j] +
        pylab.log(
            pylab.array([
                output_dist(Q=q,L=(t-tau),Y=Y[tau+1:t]),
                duration_dist(Q=q,L=(t-tau)),
                transition_dist[q,j]]).prod())            
    for j in self.state_range])

Is this bad? The above is the max of a list. The list is made up using a list comprehension, where each element is the log of a product of a 1D array plus a bit. Each element of each array is a call to a function associated with my model. The trouble is, if I break it down into some for loops, then I start having to invent temporary names for my variables, which seems clunky.

Any opinions?

M

I came across the Microcosmographia Academia in one of Niranjan‘s slides (which are an interesting view into the world of academic politics and bureaucracy) and then found it in full at the webpage of Dr Utting of Kent University. It’s a lovely satire on academic politics from 1908 in the form of a short pamphlet. Just short enough, in fact, that I’ve wasted a good hour typesetting it [pdf,tex]. It starts with this small ‘advertisement’:

If you are young, do not read this book; it is not fit for you;
If you are old, throw it away; you have nothing to learn from it;
If you are unambitious, light the fire with it; you do not need its guidance.

But, if you are neither less than twenty-five years old, nor more than thirty;
And if you are ambitious withal, and your spirit hankers after academic politics;
Read, and may your soul (if you have a soul) find mercy!