Magicdate

Wed, 06 Feb 2008

Magicdate is a simple Python module that I wrote, inspired by Simon Willison's dateparse.js, to parse fuzzy dates like "last sunday" or "Jan 4th". Particularly, I like to use it with my Python scripts to parse command line arguments; it even comes with a custom parser for optparse:

1
2
3
4
5
from optparse import OptionParser
import magicdate
parser = OptionParser(option_class=magicdate.MagicDateOption)
parser.add_option('-s', '--start', dest='start', type='magicdate', default=None)
parser.add_option('-e', '--end', dest='end', type='magicdate', default='today')

So it's now possible to do:

1
$ some-script.py --start "1 week and 2 days ago" --end yesterday

Today I added support for the "x time ago" syntax, per Kurt's request (the only person I know who has a CV as a Google Earth KML file!). The new syntax was pretty easy to implement, since the code iterates over pairs of regexps/parsing functions to handle the processing when a match is found. All I had to do was add a new function and upload the code to the Cheeseshop.

The module still needs a homepage (the Cheeseshop entry points to a 404, although the download link works). I need to organize all my projects on my homepage, but it'll have to wait a little.

And speaking of 404s, two weeks ago I went to a music school close to home, to have pandeiro (a brazilian percussion instrument) lessons. When a woman sent me to room 404 I had to fight the urge to make the joke "I'll never find it!". :)

Roberto De Almeida