2009-04-18

IntegerDateTime

So I try once and again to get into the Python ORM wave, this time with the elegant Elixir library.

The way I see it, Elixir/SqlAlchemy or any of the other ORM libraries can make a lot for you, provided you do the right things from the beginning. Integrating it into my current work flow is just too much work and I'm in constant fear everything will crumble down at some point and I'll have to rewrite everything.

Anyway the problem I had, which I pasted into stackoverflow involved our consistent use of integer columns instead of datetime columns in mysql, if I wanted to make a table wrapper I needed to cover that case so in the end I wrote my own schalchem data type (also pasted at stackoverflow)



import datetime, time
from sqlalchemy.types import TypeDecorator, DateTime
class IntegerDateTime(TypeDecorator):
"""a type that decorates DateTime, converts to unix time on
the way in and to datetime.datetime objects on the way out."""
impl = DateTime
def process_bind_param(self, value, engine):
"""Assumes a datetime.datetime"""
assert isinstance(value, datetime.datetime)
return int(time.mktime(value.timetuple()))
def process_result_value(self, value, engine):
return datetime.datetime.fromtimestamp(float(value))
def copy(self):
return IntegerDateTime(timezone=self.timezone)

In the end tough, it wasn't very useful because I don't have another table to which I can link this one so the main reason to write a wrapper for this class was void. Also, the query syntax was less nice than the SqlSoup auto-generated one so I should probably just use SqlSoup.

I still think Elixir/SqlAlchemy mappers are great, I understand they do a lot of stuff for you, like data definition centralization. But I just can't get a chance to use them where they aren't a hindrance!

So sad...