Archive for October, 2010

  1. Django localization and internationalization

    We have been using the localization and internationalization ( l10n& i18n ) from Django quite a bit for our translations, and although we haven’t had to tackle anything difficult like Arabic or Persian yet there are still some shortcomings in the system.

    The standard Django module for input of dates and datetime stamps is quite restrictive. It depends on a module, I think written by Simon Willson which uses the PHP notation rather than the Python/C strftime / strptime notation. Thus although its easy to display dates or datetime based upon the included locale files, parsing them is not so trivial from what I can see. To get around it, a configuration is placed to try and parse the date or datetime’s until a match is found. Looking at the code it does indeed use a try and except method of matching, mean potentially 10/10/10 could be any combination.

    Secondly we use the JS18n/ url which outputs translations to be used by Javascript, but this is not in the form that the Javascript Date object can understand so one has to create a function to parse the datetime:

    formats['DATETIME_FORMAT'] = 'N j, Y, P';

    That can’t be understood by Javascript unless you do so.

    Looks like there is some work to be done there, but mid-project is not the time to start making patches to code libraries.

    By timc3 on the
    October 19th, 2010
  2. Celery, RabbitMQ and sending messages directly

    Been using RabbitMQ quite a lot at Cantemo, where we use it for long standing jobs, working with other languages and for notifications.

    But I needed to decouple some of the code from my application, to be able to distribute it and talk from other systems – exactly what we needed to do, run tasks that are in the application from another application via posting messages to RabbitMQ. Tasks are held in Celery, and started usually within the code base.

    Staying with Python, though not always using CPython, I have a task “notification_of_upload_handler” which is registered in Celery (it actually has a full path to namespace it correctly, but there is no need to show that for my example).

    Obviously to do this you will need, Celery (which includes carrot), and RabbitMQ with a user and virtual host.

    Using the Carrot library, I can start this task by sending a message to RabbitMQ like so:

    from carrot.connection import BrokerConnection
    from carrot.messaging import Publisher
    from celery.utils import gen_unique_id
    conn = BrokerConnection(hostname="rabbithost", port=5672, userid="myrabbituser", password="mypassword", virtual_host="rabbitvhost")
    publisher = Publisher(connection=conn, exchange="celery", routing_key="celery")
    publisher.send({"task":"notification_of_upload_handler", "args": ("more",), "kwargs": {}, "id": gen_unique_id()})
    publisher.close()

    Of course you could use any language and any AQMP library to do this. Check out http://celeryq.org/docs/internals/protocol.html for more on the protocol.

    My next task is to broadcast this so that messages don’t stay around.

    By timc3 on the
    October 17th, 2010
  3. NetGrowl

    I needed to test sending growl messages to machines running Growl, using just Python.

    There was a problem trying to find scripts, old websites were not responding, and there was a distinct lack of being able to get anything off the group easily but I chanced upon a script that worked. Fearing that the site it is hosted on might also end up biting dust, I made my changes to the script and have uploaded it to GitHub:

    http://github.com/timc3/netgrowl

    So if you need to send Growl messages either from the command line or from within your Python project check it out.

    The original authors are detailed in the script.

    By timc3 on the
    October 2nd, 2010