Having a Python class decorator work with self

In a project, I need to benchmark some things and one of those thing is to time how long it takes to have an answer. In my code, this means measuring how long it takes between two different methods of an object are called. As I’m learning Python, I thought that a beautiful to do that would be with a class decorator.

If you don’t know anything about class decorator, I advise you read chapter 38.4 “Coding Class Decorators” from the book Learning Python by Mark Lutz and if you don’t even know anything about decorators, shame on you! No, I’m kidding, then just read chapter 38.2.

In a nutshell, a decorator makes it possible to replace a function (or a class) with another by just writing @myDecorator before the function or class. In my case, by writing @TwoMethodsTimer(“requestMedia”, “startPlayback”) before the class I want to benchmark, I will gather statistics on how long it takes to start the playback once I requested a media. Here is a first version of this decorator that I did after reading chapter 38.4 and some other blog posts and stack overflow threads:

To use it on any class is very simple, here is an example:

In this example, we measure how long it takes between calling methodA and then methodB. It works great and all. We’re happy, until we do something new:

Here we see that internally calling methodB (self.methodB()) does not trigger the stopTimer! That’s because it doesn’t seem to be calling __getattr__ when being already in the same object. I tried several variant by intercepting calls to __getattribute__ but it’s a mess and when it’s not doing infinite recursive calls, I just don’t see the calls to the method I’m interested in.

To solve this problem, instead of hijacking calls for the __getattr__ method and looking at the name of the method being called, I just modify the attributes of the object when being created. Here is the code:

Just after the instantiation of the object, we change the mapping of the two methods we’re interested in with our own methods and store the old ones somewhere else. This trick works for external and internal method calls.

Don’t migrate to the new Firefox Sync

In the last release, Firefox introduced a new Sync version, the feature with which you can synchronize your passwords, history, addons etc. For more information on Sync, go here: http://www.mozilla.org/en-US/firefox/sync/

However, when I migrated just today, I can’t tell you how disappointed I am given that:

  • You can NOT sync you passwords if you have a master password (which, in my standpoint, is necessary to have) (Link to the documentation stating this)
  • You can NOT roll back to the old Sync feature (on Firefox 29) (or not that I know of)

So here I am, stuck with the new Sync on one of my devices, with no way to sync my passwords except if I chose to store them insecurely on my computer. That’s just great, especially because synchronizing passwords was the number one reason I used Sync (as I have a unique password per website). My only advice: don’t migrate to the new Sync (yet).

Glassfish 2.x and OpenJDK

Here is a small article about Glassfish and OpenJDK as I’ve had an error and have not found the solution anywhere else.

When installing Glassfish 2.2 on Linux, I had this error message:

/opt/glassfish/setup-cluster.xml:160: Glassfish requires JDK 1.5 or higher, you have java version "1.7.0_03-icedtea"

This happens when I try to build the setup-cluster.xml file with ant:

sudo ant -f setup-cluster.xml

After too many hours of research, I’ve seen that the Java version was checked in a wrong way in the XML file. It needs Java >= 1.5 and I have Java 1.7, but the script only checks if the Java version is 1.6 or 1.5, so I’ve added the third line, so the test passes if the version is 1.7:

<condition property="java.version.acceptable"> 
    <contains string="${targeted.java.version}" substring="1.5"/>
    <contains string="${targeted.java.version}" substring="1.6"/>
    <contains string="${targeted.java.version}" substring="1.7"/>

I’ve tried this after reading http://www.ensode.net/java_fedora_8_icedtea.html and according to the author, glassfish-installer-v2-b58g doesn’t work with OpenJDK even with this trick, but it works fine with glassfish 2.1.1_b31g-1 (the current version in ArchLinux’s AUR repository).

PS: for those who have Arch, the ant command is located in /usr/share/java/apache-ant/bin/ which is not usually in the PATH.

Microsoft bluetooth keyboard

I know, microsoft is evil (trolling), but they’re really good for hardware. That’s why I bought this Microsoft Bluetooth Mobile Keyboard 5000!

Microsoft bluetooth mobile keyboard 5000Short summary of the french article: marvelous keyboard, easy to set up, comfortable, WIndows/Linux/iPad compatible. Available for only 40€ on Amazon France, and Microsoft is giving you a 20€ refund if you send them this coupon. So the real cost of the keyboard is only 20€, really good deal, sadly I don’t know if you can get the refund outside of France.

To conclude, it’s totally a keyboard to have if you plug your laptop on a bigger screen at home, really comfortable to type on. You should consider it too if you plan to build an HTPC, the keyboard is rather small and lightweight.