Neat calendar rendering tricks: Neat little trick to generate HTML calendars in python for rendering in django. Once you pass it the day/month/year, you have access to each day rendering for whatever your heart’s content. The way the class is structured gives you a lot of freedom in setting css classes per cell in the calendar rendering.
Working with (X/HT)ML in Python: I was playing around with scraping some web pages at home and I found this tool to be pretty neat: http://pypi.python.org/pypi/pyquery – pyquery allows you to make jquery queries on xml documents.
It’s similar to lxml’s cssselect ( http://lxml.de/cssselect.html
) but it supports jquery’s special selectors (:first, :eq(n), etc). Word of warning, it there are some edge cases with the special selectors that don’t work yet.
There are several different ways to parse a time string, depending on your input.
- dimagi.utils.parsing.string_to_datetime (which calls the standard dateutil library’s parser.parse). This will take input such as “January 5”, “2011-02-02”, “11/12/11”, etc.
- For dates in JSON/Couch format (“2011-12-23T20:59:19Z”) use dimagi.utils.parsing.json_format_datetime.
- Check out the diagram at http://wiki.python.org/moin/WorkingWithTime.
strip, lstrip, rstrip
The way we “delete” things in couch is often to just change the doc_type so it doesn’t show up in views. For instance, an XFormInstance that has been deleted will have doc_type “XFormInstance-Deleted”. This lets us undelete them later if it was an error.
When it came time to write the code to undelete, I thought I’d just use rstrip to strip the string ‘-Deleted’ off of the right of the string.
It turns out that’s not what that does. According to the docs: “The chars argument is not a prefix or suffix; rather, all combinations of its values are stripped”. So in my case I was getting “XformInstanc”, since ‘e’ is in ‘-Deleted’.