API reference#
Core#
- gentools.core.reusable(func)[source]#
Create a reusable class from a generator function
- Parameters:
func (GeneratorCallable[T_yield, T_send, T_return]) – the function to wrap
Note
the callable must have an inspectable signature
If bound to a class, the new reusable generator is callable as a method. To opt out of this, add a
staticmethod()
decorator above this decorator.
- class gentools.core.oneyield(func)[source]#
Decorate a function to turn it into a basic generator
The resulting generator yields the function’s return value once, and then returns the value it is sent (with
send()
).
- gentools.core.sendreturn(gen, value)[source]#
Send an item into a generator expecting a final return value
- Parameters:
gen (Generator[T_yield, T_send, T_return]) – the generator to send the value to
value (T_send) – the value to send
- Raises:
RuntimeError – if the generator did not return as expected
- Returns:
the generator’s return value
- Return type:
T_return
- class gentools.core.relay(*genfuncs)[source]#
Decorate a generator callable to relay yield/send values through another generator
Example
>>> def try_until_positive(outvalue): ... value = yield outvalue ... while value < 0: ... value = yield 'not positive, try again' ... return value ... >>> @relay(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(-1) 'not positive, try again' >>> gen.send(8) 8 >>> gen.send(104) StopIteration(104)
See also
- class gentools.core.map_yield(*funcs)[source]#
Decorate a generator callable to apply a function to each
yield
valueExample
>>> @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) 'the current max is: 11' >>> gen.send(104) StopIteration(104)
See also
- class gentools.core.map_send(*funcs)[source]#
Decorate a generator callable to apply functions to each
send
valueExample
>>> @map_send(int) ... 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(11.3) 11 >>> gen.send('104') 104
See also
- class gentools.core.map_return(*funcs)[source]#
Decorate a generator callable to apply functions to the
return
valueExample
>>> @map_return('final value: {}'.format) ... 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(11.3) 11.3 >>> gen.send(104) StopIteration('final value: 104')
See also
- class gentools.core.compose(*funcs)[source]#
compose a function from a chain of functions
- Parameters:
*funcs – callables to compose
Note
if given no functions, acts as an identity function
Types#
base classes and interfaces
- class gentools.types.Generable[source]#
ABC for generable objects. Any object where
__iter__()
returns a generator implements it.
- class gentools.types.GeneratorCallable[source]#
ABC for callables which return a generator. Note that generator functions already implement this.
- class gentools.types.ReusableGenerator(*args, **kwargs)[source]#
base class for reusable generator functions
Warning
Do not subclass directly. Subclasses are created with the
reusable()
decorator.
- __iter__()[source]#
- Returns:
the generator iterator
- Return type:
Generator[T_yield, T_send, T_return]