Posts

We don't know how to use SQLAlchemy sessions

We use sqlalchemy in ckan and often we run into problems with DetachedInstanceErrors. For this reason, we normally jam all the data from a model object into a dictionary to avoid getting the DetachedInstanceError. This isn't sqlachemy's fault, it's more than likely that we are just incapable of using it properly. For a while, we kept on running into issues where paster db clean would hang and I spent a while trying to figure out what the hell was going on and what was wrong with the session. As it turned out, there was nothing wrong session really, it was just that we were handling it badly for command line functions. There were basically 2 problems. Not closing transactions that are implicity opened by sqlalchemy Closing and removing a session and then attempting to use the sqlalchemy objects from that session causing DetachedInstanceErrors To demonstrate this, we're going to use do some fiddling with ckan from the ipython command line shell. We'll st...

Python programmer discovers functional programming. World keeps turning.

(e:feb 2015. I originally wrote this years ago when I was looking for a job and 'having a blog' was a question you were asked at interviews. This is nothing revolutionary, hell I'd rewrite the snippet now so it would yield the rows instead of constructing the entire table and returning it. But I felt good coming up with it back in the day.) Handling time series data using python’s builtin datastructures is a bitch. I guess the best way would be to just use a library like pandas , but a colleague came across the way I’d handled it in this code snippet using itertools and functools and asked me to share it with the rest of the team. I’ll just concentrate on the parts which require more explanation. The most complex line to understand is line 24 List comprehensions and python ‘switch’ row = dict([ (option, function_dict[option](series['list'])) for option in table_options ]) The dict function can build dictionaries in a variety of ways, a list pairs in m...