bor.borygmus

A programming weblog by Hao Lian. • A long walk through an angry forest. • A series of memory leaks brought on by senility.

John Arbash’s Meliae is the sexy easy library to examine memory usage in your Python runtime, from object-by-object inspection to wholesale statistics. Meliae 0.3.0 was released a few weeks ago.

jam-bazaar.blogspot.com

[(August 25, 2010) .]

Let’s talk about SQLAlchemy, __init__(), and __repr__()!

It’s another post about Python. And SQLAlchemy. There’s got to be a way to monetize this.

Let’s say we have this, because we are young powerful virile men and women capable of great sexy things.

user_table = Table(
    'user', meta.metadata,
    Column('id', Integer, primary_key=True),
    Column('name', Unicode(100), unique=True),
    Column('strength', Integer))

class User(object): pass

mapper(User, user_table)

If you are lost, maps are on the table in the corner to your left. No, not the sparkly table, that’s Django’s. The solid wooden table. Yes, that one.

Now we journey out into the world, using our new User class.

>>> User(name=u'Gosh McMuffin', strength=10)
[snip]
TypeError: __init__() got an unexpected keyword argument 'name'

Ah. SQLAlchemy is not Magic School Bus ORM. You never defined an __init__() on User. Neither did our mapper() call. And this makes sense: We don’t want a function that monkeypatches an entire class. It gives into The Temptation of Globals; any monkeypatching we do is only available in that thread and we just shot down the chances of anybody reusing our code without growing a few more gray hairs. It’s rude. And, worst of all, it’s un-Pythonic. What to do, what to do.

Jump.
[(December 28, 2009) .]

It’s time now to write about two of my favorite pieces of software ever written: Georg Brandl’s Sphinx and Pylons’ Pylons. Normally, these get along like a house on concrete, which is to say very well. A Pylons web application is a well-behaved, standard Python package that pays all the taxes: a setup.py, no module magic like pre-modern Django, and imports as expected. However, if you have ever used the highly useful Sphinx autodoc extension and have pointed it at a module in your Pylons app, you may have run into a traceback that looks like this:

[snip]
File "[snip]/autodoc.py", line 1019, in can_document_member
  return (isdescriptor(member) and not
File "[snip]/autodoc.py", line 203, in isdescriptor
  if hasattr(getattr(x, item, None), '__call__'):
File "[snip]/paste/registry.py", line 137, in __getattr__
  return getattr(self._current_obj(), attr)
File "[snip]/paste/registry.py", line 194, in _current_obj
  'thread' % self.____name__)
TypeError: No object (name: url) has been registered for this thread

Two questions should emerge immediately: What is paste.registry? And why is it throwing a traceback?

Jump.
[(July 25, 2009) .]

WebMynd has a great little post about extending SQLAlchemy to back Amazon’s Simple Storage Service. I wasn’t even aware of MapperExtension until now. If you love reading about all the little nooks and crannies of SQLAlchemy, check this out. I also just realized that the Mike Bayer who commented on bor.borygmus’s first grumble is the same person who created SQLAlchemy, which is marvelous.

[(July 19, 2009) .]

In any development community, packaging is a topic that often doesn’t get explored. It’s pretty boring. And it’s mostly a set of de facto standard operating procedures that desiccate over the eons until they become the code everybody cribs from everybody else. Come along, as we explore the why and how of Python packaging. B-b-b-blue’s Clues.

Jump.
[(July 16, 2009) .]

2to3 and unittest have likely done untold wonders in keeping together the families of library authors porting from Python 2 to Python 3, taking care of the low-hanging fruit and leaving the most insidious and therefore fun bugs to root out. The biggest category of these would be Unicode, and out of that category, the biggest subcategory would be relying on some quirk of representing textual data in bytes when it should have been Unicode all along.

But then there’s this: What if you need to represent bytes? Take hashlib, which will flat-out refuse to consume a Unicode string. There’s a byte literal in Python 3, but is there a way to get there from 2to3 and Python 2?

Jump.
[(July 4, 2009) .]

Ian Bicking’s Python Paste jar of goods is more or less a testament to the power of having a standardized web protocol like WSGI. We can use and abuse the Paste Script like a futon to quickly run Python web applications without the hassle of a big web server.

Jump.
[(April 2, 2009) .]

matplotlib takes the goodness of the Matlab plotting API (and more!) and puts it in Python. It’s an astonishingly identical API. And now you want to compile it on Cygwin. You’ve scoured the Usenets. You’ve scoured your pots and pans. And it’s impossible. Or is it?

Jump.
[(March 19, 2009) .]