Dynamic list filtering with Python

Python is AMAZING!
I was defining a field for a Django model which represents minutes and I thought to allow the selection of ranges of 5 minutes only (ie: 5, 10, 15…), this can be accomplished by passing an iterable of tuples in “choices” param… the point is: how can I pass such collection in the simplest and cleanest manner? The answer I found is: using the powerful, compressed and surprisingly human-readable list filtering feature of python!
And this is the snippet:

[(n, str(n)) for n in range(60) if n % 5 == 0] 

which generates the following list:

[(0, '0'), (5, '5'), (10, '10'), (15, '15'), (20, '20'), (25, '25'), (30, '30'), (35, '35'), (40, '40'), (45, '45'), (50, '50'), (55, '55')]

The snipped can be translated into the following english sentence:

“give me back a list of tuples containing the number and its string conversion for each number divisible by 5 in the numeric range 0-60”

\o/ woah!


When I wrote this post I was still a python beginner… the snippet can be simplified using only range() in this way:

[(n, str(n)) for n in range(0, 60, 5)]

Solving launchctl load “nothing found to load” error

Today I was playing with OSX launchctl command, in order to autoload a program using a simple and human-readable XML file (.plist).
Initially my command (sudo launchctl load /path/to/myplist.plist) was giving back an error message “nothing found to load” and I thought it was a bad configuration in the plist, so I checked every path and every settings I specified in it and I realized it was ok… so using the manual (man launchctl) I looked for helpful flags and/or additional help, I then modified the command adding the -F and -w flags and I was able to run my program succesfully. It also started up automatically after a reboot as I wished.
The final command is:

sudo launchctl load -w -F /path/to/myplist.plist

Flags descriptions are the following:

-w Overrides the Disabled key and sets it to false. In previous versions, this option would modify the configuration file. Now the state of the Disabled key is stored elsewhere on-disk.

-F Force the loading of the plist. Ignore the Disabled key.