Skip to content

Comment on (Python) Generator Tricks For Systems Programmer (pdf)

Comments

I love generators! They produce attractive code that is understandable (once one really understands what a generator is)

I remember using one when I was building a Mail DB search appliance (on the backburner..) with the mail retrieval code being this:

https://gist.github.com/1014045

Basically, after importing, retrieving emails is as simple as this:

    get_messages, close_server  = __pop3()
    all_messages = get_messages()
    for message in all_messages:
            fn(message)
    close_server()
__pop3 returning a simple generator, while close_server deals with shutting off the server / cleaning up the closure itself. No need to retrieve/initialize all the emails at once (with the memory / other overhead involved); just one at a time (with the connection being kept open in the background). Normally if you'd want to do that, you'd have to stick the "message handling code" into the "message retrieval code", or at least pass along a "handling" function - neither of which really seems that kosher to me. Generators solved that problem handily.

Looks like you could benefit from python's context managers (aka the "with" statement http://www.python.org/dev/peps/pep-0343/ ). It allows you to do something at the start, do stuff in the middle, then guantee that something is done at the end, even if an exception is thrown. things that need to be closed afterwards are a great example of where with statements help.

Thanks, I'll take a look.

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.