Skip to main content

Sugar around Python generators and context managers

Project description

This package provides an helper to write cofunction with the help of Python generators. It provides a decorator that let you write the following paradigm:

from infrae.comethod import cofunction

def processor(**options):
    # Init code
    content = yield
    while content is not None:
         # Work on content, set result in result.
         content = yield result
    # Cleanup code

If you create more of those comethods you can nest them:

import logging

def logger(parent **options):
    logger = logging.getLogger(options.get('name', 'default'))'Start')
    content = yield
    while content is not None:
         result = parent(content)'Processed {0}, got {1}'.format(content, result))
         content = yield result
    # Cleanup code'Done')

And you can use it like this:

with processor() as process:
   with logger(process) as log:
      for item in generator():

Or alternatively:

with processor() as process:
   with logger(process) as log:

Where generator is a generator that yield values to work on. Of course, you can do more interesting things after.

You can find the Mercurial code repository at:


1.0 (2011/11/07)

  • Initial version.

Project details

Release history Release notifications

This version
History Node


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
infrae.comethods-1.0.tar.gz (2.7 kB) Copy SHA256 hash SHA256 Source None Nov 7, 2011

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page