Skip to main content

like itertools, for generators, generator functions, and generator-based coroutines

Project description

https://img.shields.io/pypi/v/gentools.svg https://img.shields.io/pypi/l/gentools.svg https://img.shields.io/pypi/pyversions/gentools.svg https://travis-ci.org/ariebovenberg/gentools.svg?branch=master https://coveralls.io/repos/github/ariebovenberg/gentools/badge.svg?branch=master Documentation Status Maintainability

like itertools, for generators, generator functions, and generator-based coroutines.

Examples

  • Make generator functions reusable:

>>> @reusable
... def countdown(value, step):
...     while value > 0:
...         yield value
...         value -= step

>>> from_3 = countdown(3, step=1)
>>> list(from_3)
[3, 2, 1]
>>> list(from_3)
[3, 2, 1]
  • map a generator’s yield, send, and return values:

>>> @map_return('final value: {}'.format)
... @map_send(int)
... @map_yield('the current max is: {}'.format)
... def my_max(value):
...     while value < 100:
...         newvalue = yield value
...         if newvalue > value:
...             value = newvalue
...     return value

>>> gen = my_max(5)
>>> next(gen)
'the current max is: 5'
>>> gen.send(11.3)
'the current max is: 11'
>>> gen.send(104)
StopIteration('final value: 104')
  • pipe a generator’s yield/send through another generator:

>>> def try_until_positive(outvalue):
...     value = yield outvalue
...     while value < 0:
...         value = yield 'not positive, try again'
...     return value

>>> @pipe(try_until_positive)
... def my_max(value):
...     while value < 100:
...         newvalue = yield value
...         if newvalue > value:
...             value = newvalue
...     return value

>>> gen = my_max(5)
>>> next(gen)
5
>>> gen.send(-4)
'not positive, try again'
>>> gen.send(8)
8
>>> gen.send(104)
StopIteration(104)

Release history

development

0.3.0 (2018-01-22)

  • make reusable generators callable as methods

0.2.2 (2018-01-21)

  • updates to readme

0.2.0 (2018-01-21)

  • reorganized modules, improved docs, renamed functions.

0.1.0 (2018-01-17)

  • initial release

Project details


Download files

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

Source Distribution

gentools-0.3.0.tar.gz (6.2 kB view hashes)

Uploaded Source

Built Distribution

gentools-0.3.0-py3-none-any.whl (8.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page