May 3, 2011
"Because JavaScript is pretty free-form, consistent coding style and unit tests are critical to give it some structure and keep it readable.

We propose that all GNOME usage of JavaScript conform to the style guide in doc/Style_Guide.txt to help keep things sane."

From README of GNOME Spidermonkey Javascript binding.

I think this README file sums up my opinion about Javascript in Gnome 3: I can’t understand why Javascript is used in the shell and not a more suitable language (such as Python). It was a very good opportunity to open the desktop to new developers, but… Javascript?

April 30, 2011
PIL Error Saving a JPEG Image

This bug has been very difficult to identify and workaround, so I’m writing this post for the record.

When I tried to save a JPEG encoded image using optimize option, I was getting the following error with some images:

Traceback (most recent call last):

  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 100, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

  File "/home/motivator/../motivator/public/views.py", line 227, in creator_done_view
    img.copy().resize((480, 480*h/w), Image.ANTIALIAS).save(fd, 'JPEG', optimize=True)

  File "/usr/lib/python2.6/dist-packages/PIL/Image.py", line 1439, in save
    save_handler(self, fp, filename)

  File "/usr/lib/python2.6/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)])

  File "/usr/lib/python2.6/dist-packages/PIL/ImageFile.py", line 501, in _save
    raise IOError("encoder error %d when writing image file" % s)

IOError: encoder error -2 when writing image file

The weird thing is that it was happening with a couple of images, and I couldn’t find a pattern (although the image size seemed to be relevant).

The problem looks related to libjpeg (the library PIL is using) and the way PIL is encoding the JPEG, and after trying blindly to tweak the saving process, I managed to get it to work setting ImageFile.MAXBLOCK to a big value (by default it’s using 64KB).

 # before saving, increase MAXBLOCK
from PIL import ImageFile
    ImageFile.MAXBLOCK = 1024*1024

I don’t know what block size works best, because I certainly don’t know what kind of image is making PIL to crash, but 1MB it’s been working fine for me so far.

When I was looking for information about this bug I’ve found that there’s some people not very happy with PIL because of the JPEG support. It’s working OK for me now, but I’m not 100% happy with the workaround.

February 17, 2011
Python 2 or Python 3?

Actually, that’s not the question. I would ask you what’s the problem you want to solve.

Recently some guy created the Python 3 Wall of Shame, which it’s supposed to expose a problem with Python 3 being released in 2008, but currently most of the packages don’t support Python 3 but Python 2.

As you may know, Python 3 isn’t exactly backwards compatible with Python 2, so it’s not as simple as using the latest version.

I’m my opinion, the aforementioned Wall of Shame is plainly wrong: the timeline for solid Python 3 adoption is 5 years, according to Python developers. There’s another site (Get Python 3), that it’s more positive, and I think it’s the way to go.

When I started with Python, I read Should I use Python 2 or Python 3 for my development activity?, and summing it up is just use the best tool available at the moment, keeping an eye in the new stuff.

by jjm on 8:37pm  |   URL: http://tmblr.co/ZPorZy37dKoU
(View comments  
February 7, 2011
"Python surged hugely this last year — more than any other language. […] I’m not convinced. I suspect part of the new adoption comes from Perl programmers who are throwing in the towel."

From The Rise And Fall of Languages in 2010.

This guy is good! Count me in!

by jjm on 7:12pm  |   URL: http://tmblr.co/ZPorZy2ylXhI
(View comments
January 9, 2011
"Programming language Python has become programming language of 2010. This award is given to the programming language that gained most market share in 2010. Python grew 1.81% since January 2010."

From TIOBE Programming Community Index for January 2011.

In other news PHP had a real bad time in 2010. Interesting, I moved from PHP to Pyhton with my job change at the end of 2010.

December 19, 2010
This Unit is Under Test
Every day I walk by the side of this sign in my commuting to Guildford, and it makes me smile.
The reason is that I’m doing a lot of unit testing at work, and it’s funny that the Reading Borough Council believes in unit testing too ;).

This Unit is Under Test

Every day I walk by the side of this sign in my commuting to Guildford, and it makes me smile.

The reason is that I’m doing a lot of unit testing at work, and it’s funny that the Reading Borough Council believes in unit testing too ;).

November 17, 2010
GFS the Google File System in 199 Lines of Python

I recommend the whole Python Cloud DB Blog, if you are interested in cloud computing stuff and (at least) can read Python.

It’s been a lot of buzz & hype lately about big data (can we blame Cassandra and Hadoop for this?), to the point that CouchDB people are willing to move away from NoSQL label.

by jjm on 9:34am  |   URL: http://tmblr.co/ZPorZy1VK3eJ
(View comments
Filed under: Cloud Python GFS DB NoSQL database kewl 
October 1, 2010
"New users wising to develop Python applications using GTK are recommended to use the
GObject-Introspection features available in PyGObject. Existing authors of PyGtk applications are also recommended to port their applications to PyGObject if they wish to take advantage of new features appearing in GTK-3 and beyond."

From PyGTK 2.22.0 announcement.

PyGTK-2.24 will be next and last PyGTK version, and if you’re willing to write a GTK+ application in Python it’s highly recommended that you use PyGObject, because it’s the project that will be supporting GTK+3 (and later).

At the moment it’s unlikely that your distribution has packaged a recent enough PyGObject, and the introspection modules are beta anyway, so I guess it’s a uncertain moment to start a new GTK+ application in Python.

September 18, 2010
Announcing Oraje Applet 0.1

As you may know, we moved to Reading (Berks) one month ago. Before that we where living in Exeter (Devon), and moving implies a lot of difficulties: you’re in different city, and you don’t know where’s anything (from shops to pubs, including all the city services) .

But there’s one thing you never expect: the weather applet in your system doesn’t support your new city (tragic music cue).

I checked about current status of the weather applet in Gnome, and I found an interesting proposal dated two years ago, but nothing that shows progress.

I suffered this exact frustration some time ago when I was living in my home town, and now, after years living in different supported cities… does it mean my luck is over? Not necessarily: I know Python and I’m prepared to use it!

So after a couple of weeks hacking form time to time (whenever I had free time, not that much lately), Oraje Applet was born.


Details dialog, Oraje Applet 0.1

I did some research and found that Yahoo! Weather has information about Reading (my current location), and there’s an API for it.

It’s not the best API ever (in fact, there’s no easy way to find a location without knowing the associated code, called WOEID), and sometimes the updates lag behind reality (AKA it’s raining and your applet says it isn’t), but overall it’s what I was looking for.

In the project page there’s a tarball with the first released version (0.1), and there’s a lot of work to do, but I think it’s kind of usable if you want to give it a try.

I’d like to package it for Fedora, but I didn’t have the time yet (any volunteer for that would be welcome!).

Update: you can find a WOEID using the Yahoo! GeoPlanet™ API. I need to check it deeply, because at first sight there are a couple of things I don’t like very much.

July 31, 2010
Making Reports in CVS from SQL

Sometimes I need to query a database, using SQL for that, and get the results to process them with OOo Calc o any other data analysis tool (for example GNU Plot).

The most easy way to import data into OOo Calc is using CSV, but the SQL clients I usually have at hand doesn’t support that output format and I always end using some AWK magic to get a correct CSV.

It’s not a big deal, but last week I had a different problem at work: what if you have to do different queries in different databases depending on a main table?

In fact, it was a multi-tenant application with one database for each instance, that can be in any of machine in our backend cluster, so having the list of the databases wouldn’t help.

Due to this I had to execute a query to get the database name and the connection information for each instance, and then run the query I actually needed for my report.

I did the work, and the report it’s being generated automatically as the customer requested, but I wasn’t satisfied with the resulting code (you know what happens when you don’t have enough time to do your work), so I’ve spent some of my spare time programming a tool to make easier this kind of reports: YAML Simple SQL Reports (yes, I know… what a fancy name!).

The idea is to specify the report in a YAML file. For example:

report:
    name: first report
    connect:
        type: mysql
        database: dbname
        username: user
        password: secret
    query: select foo, foo2 as whatever from bar
    output: file.csv

This will generate a CSV file into file.csv, with foo, whatever header and one line for each row resulting of the query.

At this point, al least, I got rid of the AWK post-processing. But, what about the multi-database problem? Easy peasy with subreports!

report:
    name: first report
    connect:
        type: mysql
        database: dbname
        username: user
        password: secret
    query: select dbname, dbhost, dbpasswd, dbuser from masterdb
    report:
        name: sub report
        connect:
            type: mysql
            dabase: {dbname}
            hostname: {dbhost}
            username: {dbuser}
            password: {dbpasswd}
        query: select * from table
    output: file.xml

The query in first report is executed, and for each row in the result, the report named sub report is executed (after replacing the {KEY} stuff in each loop with the value from the result row). The result of all the queries in sub report are concatenated and used as output (in this example as XML).

It’s not the ultimate tool to make reports, but it will work for me next time I’m asked to do a report ASAP from any SQL database.

Update: you can get the stable versions from the downloads section of github. PostgreSQL it’s being tested, and will be version 0.3.