Unicode Normalization Forms

I was walking across a bridge one day, and I saw a man standing on the edge, about to jump off. So I ran over and said, “Stop! Don’t do it!”

“I can’t help it,” he cried. “I’ve lost my will to live.”

“What do you do for a living?” I asked.

He said, “I create web services specifications.”

“Me too!” I said. “Do you use REST web services or SOAP web services?”

He said, “REST web services.”

“Me too!” I said. “Do you use text-based XML or binary XML?”

He said, “Text-based XML.”

“Me too!” I said. “Do you use XML 1.0 or XML 1.1?”

He said, “XML 1.0.”

“Me too!” I said. “Do you use UTF-8 or UTF-16?”

He said, “UTF-8.”

“Me too!” I said. “Do you use Unicode Normalization Form C or Unicode Normalization Form KC?”

He said, “Unicode Normalization Form KC.”

“Die, heretic scum!” I shouted, and I pushed him over the edge.

(with apologies to Emo Philips)
(resurrected from a dead Dive Into Mark posting from 2004)

Make datetime naive for mysql in sqlalchemy

If you’re using mysql and are trying to store UTC datetime values, and elsewhere are sane enough to use datetime values that are timezone aware, here’s a little type for stripping out the timezone data on store and replacing it on load:

from sqlalchemy.types import TypeDecorator, DateTime
from dateutil.tz import tzutc
from datetime import datetime

class UTCDateTime(TypeDecorator):

    impl = DateTime

    def process_bind_param(self, value, engine):
        if value is not None:
            return value.astimezone(tzutc()).replace(tzinfo=None)

    def process_result_value(self, value, engine):
        if value is not None:
            return value.replace(tzinfo=tzutc())

Automatically associating users with django models on save

I have spent way too much time trying to figure out how to automatically have created_by and modified_by be foreign keys to the current user, without mucking with every view and form. Every example I could find that told me how to do this, including django’s documentation itself, told me I had to customise every view and form, which I found to be rather un-DRY.

Thanks to stealing borrowing some code from django-audit-log (in order to be threadsafe) I figured out how to update these values on any model that has them, without any fiddling with forms or views. I might wrap this up in a little project, but for now, here’s the gist.

Note that you should set editable=False on your created_by and modified_by fields so that they will not otherwise show up in forms.

If anyone is really interested i could wrap this in a self-installable package.

Setting up a dev environment (10.8 Mountain Lion Edition)

Notes to myself so I can set things up properly. Hope they’re useful to you as well.

  1. Install any OS Updates.
  2. Get XCode from the app store. Launch it, accept the license, and go to Preferences, then choose “Downloads”, and install Command Line Tools.
  3. Install XQuartz.
  4. Install Homebrew:
    ruby <(curl -fsSkL raw.github.com/mxcl/homebrew/go)
  5. Install git from homebrew (brew install git).
  6. Install Tower.
  7. Install Sublime Text.
  8. If you need subversion, install it from homebrew. If you need to use java, make sure you pass the –java flag.

You’re pretty much set at this point. Special per-dev type notes below: Continue reading