Python class attributes versus instance attributes

Today I finally found out the difference between class and instance attributes in Python. In C++, this is done by putting the static modifier in front of the declaration. Consider the following code:

#!/usr/bin/env python

class B:
b = 2

class C:
a = B()

def __init__(self):
self.a.b = 1

c = C()
c.a.b = 3
b = C()

print c.a.b, b.a.b

Here, a is a class attribute of class C. That is, there exists only one such attribute for all objects of kind C. So the output of the print statement will be “1 1”. This is the same as a static attribute in C++. But often I want an instance attribute. The correct way to do this would have been:

#!/usr/bin/env python

class B:
b = 2

class C:
def __init__(self):
self.a = B()
self.a.b = 1

c = C()
c.a.b = 3
b = C()

print c.a.b, b.a.b

Now the output is “3 1”, just as expected. I guess this all somehow makes sense in the Python world, but I tripped over this, and worst of all: Sometimes you don’t even notice. If the class attribute is a simple type, like int, the first solution would have worked. However, I have not yet understood why that is the case. One more Python semantic that eluded me so far.

Mobile blog

Finally. My blogs now also support mobile devices with rendering suitable for small screens. All thanks to Blogger, who have introduced a beta version of the mobile templates via their beta program. You can force the template on every blog by appending “?m=1” to the blog URL. On your own blog, you can just enable it in your blog’s mobile settings via draft.blogger.com. Here’s how my blog looks in the mobile version:

MapReduce on CUDA using Python (and a Discontinuous Galerkin solver!)

As volcore pointed out, there is a library called pycuda, which allows for CUDA programming in Python. It also comes with a nice ReductionKernel class, which allows one to rapidly develop custom MapReduce kernels.

Update: Even better, the same author has published a Discontinuous Galerkin solver, based on the same stuff. This can be used to solve partial differential equations, e.g. for fluid simulations, but also for EM simulations, using Maxwell’s equations.

Setting the sender of git post-receive hooks

There is a nice stackoverflow posting about how to set the sender of git post-receive hooks. I used this, slightly augmented:

#!/bin/sh

# Use the name and email address of the author of the last commit.
USER_EMAIL=$(git log -1 --format=format:%ae HEAD)
USER_NAME=$(git log -1 --format=format:%an HEAD)
. $(dirname $0)/post-receive-email

I then also changed the default post-receive-email script, to look like this in the send_mail() function:

send_mail()
{
if [ -n "$envelopesender" ]; then
/usr/sbin/sendmail -t -f "$envelopesender"
else
/usr/sbin/sendmail -t -F "$USER_NAME" -f "$USER_EMAIL"
fi
}
This will let the emails come from the last user in the git log. This is only a hack, and might break or not make sense under certain circumstances, but is good enough for most of my needs.

Pimping MobileMe #1: Expiration dates on folders

Well, MobileMe does not yet support the expiration of mails in folders. Especially for mailing lists, I want this feature. After let’s say 90 days the mails should be automatically deleted. I do not need to store mailing list contents for longer. So I took the nice tool imapfilter and wrote a config file that expires old mails from a selection of folders. I installed a cronjob on my web server that now automatically calls imapfilter every night. The config file (~/.imapfilter/config.lua) looks like this:

Myaccount = IMAP {
   server = ‘mail.me.com’,
   username = ‘your.name@me.com’,
   password = ‘yourpassword’,
   ssl = ‘tls1’
}


folders = {‘folder1’, ‘folder2’, ‘folder3’}


for k,folder in pairs(folders) do
   foldername = ‘Mailinglists/’ .. folder
   results = myaccount[foldername]:is_older(90)
   myaccount[foldername]:delete_messages(results)
end

My mailing lists are hosted under a sub-folder called “Mailinglists”, as you can notice in the config file above. You can tweak this to your liking. It might also be useful to set different expiration dates. It’s easy to augment the lua script to do so.

Migrating from GMX IMAP to MobileMe IMAP

I am currently migrating my >4GiB of mail data from GMX to MobileMe. I have been using offlineimap regularly to backup my mail data. So first step was to update the backup. Now the next step is to run imapsync, which can migrate whole IMAP accounts. The full command I am using for this is:

imapsync –sep1 ‘/’ –prefix1 ”  –ssl1 –ssl2 –authmech1 PLAIN –authmech2 PLAIN –host1 imap.gmx.net –user1 “yourname@gmx.de” –host2 mail.me.com –user2 “yourname@me.com”

I will report back once the sync has finished, this might take a while.

Update: Yup, it worked. My mail is now hosted on MobileMe. More disk space. What is missing at MobileMe is that it does not support automatically expiring mail folders. I.e. for mailing lists, I sort the mails into subfolders for each list. The lists are often high volume traffic, and not very important. So I’d like the mails to expire after, say, 30 days. With GMX this was not a problem, but MobileMe does not yet have such a feature. However, I found a nice tool called imapfilter. It allows you to do all kinds of stuff, besides other things it allows you to filter mails according to date and to delete them. This is what I will do. I’ll write another post, when I found out how to do that exactly.