Saving matplotlib plots as PDF

I recently started using matplotlib together with PyQt, and I love it. It’s awesome and has many more features compared to PyQWT. However, I needed to save the plots to a PDF file. Here is how you do that:

import matplotlib.backends.backend_pdf
...
@QtCore.pyqtSlot()
def printPlots(self):
filename,_ = QtGui.QFileDialog.getSaveFileName(self, "Save plots as PDF file", "", "Portable Document File (*.pdf)")
if filename == "":
return
pp = matplotlib.backends.backend_pdf.PdfPages(filename)
pp.savefig(self.plotFigure)
pp.close()

Assuming that your figure is called self.plotFigure. You can connect the above slot to a QAction and map it to some nice menu item or shortcut.

Is “not None” maybe “Something”?

I just had a funny thought. In Python you can write:

if not someObject is None:
someObject.doSomething()
else:
print "someObject is None!"

This reads a bit strange. So what if you could alias “not … is None” to “… is Something”?

if someObject is Something:
someObject.doSomething()
else:
print "someObject is None!"

It seems that this idea was thought of almost eight years ago already. This lead to some PEP 0326, which got rejected. If Python were a macro or functional language, you could probably hack something up to do the same thing, but it does not work like that:

>>> Something = not None
>>> Something
True
>>> A = [1,2,3]
>>> if A is Something:
... print "This is something"
... else:
... print "This is nothing"
...
This is nothing
>>>

The problem here being, that “non None” is immediately evaluated to “True”, since “None” can be implicitly converted to “False” in a boolean sense. Was a funny thought, though.

Update: Turns out you can at least write “if someObject is not None:”, which is more readable.

Alignment of double values on ARM architectures

Consider the following code:

class C {
...
uint8_t *data;
size_t size;
...

void writeDouble(double v)
{
...
reinterpret_cast<double*>(this->data)[this->size] = v;
this->size += sizeof(double);
...
}

};

Looks harmless enough, doesn’t it? But here is the problem: It’s not necessarily portable or well behaving code. After debugging for about three hours, I found out something interesting. Namely that on ARM platforms, such as the iPhone, double values on the stack need to be stored at 8 byte aligned memory addresses. This was some existing code I was using, so it took me a while to get to this function and to its problem. Single precision float values have no such restriction, by the way. There are two workarounds in the above case. 1) Write a small for-loop that casts the double value to a uint8_t * and copy the double value byte-wise, or 2) use malloc to copy the value. I did the for-loop, since I thought that maybe the malloc call has too much overhead. I guess you could also cast the double to a uint32_t * pointing to two 32 bit words. Anyway, take care when doing such drastic casts. Some platforms might want alignment for some datatypes!

Valgrind checking of iOS programs

Well, this is awesome news. First, valgrind has been available for OS X for some time now. And second, you can use it to check your iOS programs on the simulator with it.

The idea here is to let your program spawn valgrind itself. Because you cannot tell the simulator to run the program through valgrind. Well, maybe you could build a funky bundle, but I think this works just fine. So here is the  code, taken from the above link:

#define VALGRIND "/usr/local/valgrind/bin/valgrind"

int main(int argc, char *argv[]) {
#ifdef VALGRIND_REXEC
/* Using the valgrind build config, rexec ourself
* in valgrind */
if (argc < 2 || (argc >= 2 && strcmp(argv[1], "-valgrind") != 0)) {
execl(VALGRIND, VALGRIND, "--leak-check=full", "--dsymutil=yes", argv[0], "-valgrind",
NULL);
}
#endif

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, @"PeepsAppDelegate");
[pool release];
return retVal;
}

You will obviously want to define the VALGRIND_REXEC macro, if you need valgrind output. You can also pass different command line arguments to valgrind. E.g. you can switch to different valgrind tools this way, or pipe everything to a log-file.
Update: I finally got around to trying out this method. One problem here is that valgrind will fail to run, since it tries to open /dev/random, which I guess is not allowed for sandboxed applications. But one can fix this by patching and recompiling valgrind, which is not too hard. Especially when using MacPorts. Furthermore, I needed to add –dsymutil=yes to the valgrind options, or else the program would just crash.

Conditionally rewriting mail subjects using exim4

Since MobileMe sadly does not support filtering using RegExps, and I get a lot of heterogenous system mails from our computer systems here, I implemented a workaround. I added some text like [System] to the subject, which can be filtered fine by MobileMe.

All of our machines here send their mail to a central smarthost running exim4. All the system mails are directed to “root” at some of our machines. What I did was first adding a file /etc/exim4/exim.filter containing this:

# Exim filter
if "$h_to:" matches Nroot@.*.your.domain.deN then
headers add "New-Subject: [System] $h_subject:"
headers remove subject
headers add "Subject: $h_new-subject:"
headers remove new-subject
endif
Since we are running Debian, running exim4 in split configuration, I also added the file /etc/exim4/conf.d/main/80_exim4-config_system_filter, containing this:

system_filter = /etc/exim4/exim.filter

After that I had to run update-exim4.conf and /etc/init.d/exim4 reload. Then I was all set up.