bor.borygmus

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

There’s an open call by Tav, looking for people to break the security boundaries on safelite.FileReader—a file reader—and write a file. As pointed out by seul on reddit, this is basically a window into how really smart Python programmers think.

[(February 24, 2009) .]

Update: I am currently unable to get any of this to work on the yasnippet 0.6.x release series. Setting yas/trigger-key seems to not do anything or throws an integer-or-marker-p error, and then even when I leave that out, yas/expand hangs for half a second before declaring that it cannot find any snippets. This grumble was written with yasnippet 0.5.5 installed. If anyone knows what’s happening, drop a comment.

You can stick this in your yasnippet (Emacs templating) pipe and chew on it for a while: "cls" "class ${class}(${object}):$0".

Jump.
[(February 13, 2009) .]

The setup.

>>> a = range(1, 6)
>>> a
[1, 2, 3, 4, 5]

The take.

>>> zip(a, a, a)
[(0, 0, 0), (1, 1, 1), (2, 2, 2), (3, 3, 3), (4, 4, 4), (5, 5, 5)]

The con.

>>> i = iter(a)
>>> zip(i, i, i)
[(0, 1, 2), (3, 4, 5)]

The explanation.

David Jones makes group soup out of these lemons. Since zip and iter are both implemented in C, this is one of the fastest and tersest ways to group.

[(January 30, 2009) .]

When we last left off on our framework, we had a simple WSGI app.

def app(environ, respond):
    headers = [('Content-type', 'text/plain')]
    respond('200 OK', headers)
    return ['I have a wife and children!\n']

Not the most dynamic web site in the world. Let’s pass the buck and force some other function to do our hard work.

Jump.
[(January 27, 2009) .]

All modern Python servers—except mod_python—talk to the application using the same pseudo-protocol called the Python Web Server Gateway Interface (WSGI).

Old-timies out there will recognize the parallels in naming the WSGI after the Common Gateway Interface, which was spectacularly simple. A browser connects to the web server. The web server runs perl website.pl or python website.py. Your HTTP request, headers and all, is sent through via standard input. Your HTTP response, headers and text and all, is sent through via standard output.

One process for every request. Simple. Slow.

Starting up an interpreter each time bogs you down. A better solution is to keep the interpreter around for multiple requests. Or, better yet, start a whole farm of fuzzy little interpreters. They run around, eagerly trying to fulfill your HTTP requests until they’re plump enough to be brutally decapitated in a mass orgy of death-worship in order to become your HTTP bacon sandwiches.

Jump.
[(January 23, 2009) .]

Google App Engine’s datastore API is a continuation of a trend that the outstanding Python SQLAlchemy started, namely to re-purpose the class, an object-oriented concept, as a declaration of a table. Take, for example, the SA example

Base = declarative_base()
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    fullname = Column(String)
    password = Column(String),

which by all standards seems primitive. I have to call declarative_base? And what are those underscores doing in my pristine class? Can’t you pluralize the table for me? I have to sit down, I have a headache now. SQL is hard.

Jump.

[(January 2, 2009) .]