like itertools, for generators, generator functions, and generator-based coroutines
Project description
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]
True
>>> list(from_3) == [3, 2, 1]
True
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(value):
... value = yield value
... while value < 0:
... value = yield 'not positive, try again'
... return newvalue
>>> @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.2.0 (2018-01-21)
reorganized modules, improved docs, renamed functions.
0.1.0 (2018-01-17)
initial release
Project details
Release history Release notifications | RSS feed
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.2.1.tar.gz
(6.0 kB
view hashes)