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()).

__init__(func)[source]
__call__(*args, **kwargs)[source]
Returns:

the resulting generator

Return type:

Generator[T_yield, T_send, T_return]

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

irelay()

__init__(*genfuncs)[source]
__call__(func)[source]

Call self as a function.

class gentools.core.map_yield(*funcs)[source]

Decorate a generator callable to apply a function to each yield value

Example

>>> @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

imap_yield()

__init__(*funcs)[source]
__call__(func)[source]

Call self as a function.

class gentools.core.map_send(*funcs)[source]

Decorate a generator callable to apply functions to each send value

Example

>>> @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

imap_send()

__init__(*funcs)[source]
__call__(func)[source]

Call self as a function.

class gentools.core.map_return(*funcs)[source]

Decorate a generator callable to apply functions to the return value

Example

>>> @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

imap_return()

__init__(*funcs)[source]
__call__(func)[source]

Call self as a function.

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

__init__(*funcs)[source]
__hash__()[source]

Return hash(self).

__eq__(other)[source]

Return self==value.

__ne__(other)[source]

Return self!=value.

__call__(*args, **kwargs)[source]

Call self as a function.

gentools.core.imap_yield(func, gen)[source]

Apply a function to all yield values of a generator

Parameters:
  • func (Callable[[T_yield], T_mapped]) – the function to apply

  • gen (Generable[T_yield, T_send, T_return]) – the generator iterable.

Returns:

the mapped generator

Return type:

Generator[T_mapped, T_send, T_return]

gentools.core.imap_send(func, gen)[source]

Apply a function to all send values of a generator

Parameters:
  • func (Callable[[T_send], T_mapped]) – the function to apply

  • gen (Generable[T_yield, T_mapped, T_return]) – the generator iterable.

Returns:

the mapped generator

Return type:

Generator[T_yield, T_send, T_return]

gentools.core.imap_return(func, gen)[source]

Apply a function to the return value of a generator

Parameters:
  • func (Callable[[T_return], T_mapped]) – the function to apply

  • gen (Generable[T_yield, T_send, T_return]) – the generator iterable.

Return type:

Generator[T_yield, T_send, T_mapped]

gentools.core.irelay(gen, thru)[source]

Create a new generator by relaying yield/send interactions through another generator

Parameters:
  • gen (Generable[T_yield, T_send, T_return]) – the original generator

  • thru (Callable[[T_yield], Generator]) – the generator callable through which each interaction is relayed

Returns:

the relayed generator

Return type:

Generator

Types

base classes and interfaces

class gentools.types.Generable[source]

ABC for generable objects. Any object where __iter__() returns a generator implements it.

abstract __iter__()[source]
Returns:

the generator iterator

Return type:

Generator[T_yield, T_send, T_return]

class gentools.types.GeneratorCallable[source]

ABC for callables which return a generator. Note that generator functions already implement this.

__call__(*args, **kwargs)[source]
Returns:

the resulting generator

Return type:

Generator[T_yield, T_send, T_return]

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.

__init__(*args, **kwargs)[source]
__iter__()[source]
Returns:

the generator iterator

Return type:

Generator[T_yield, T_send, T_return]

__eq__(other)[source]

Return self==value.

__repr__()[source]

Return repr(self).

__hash__()[source]

Return hash(self).

replace(**kwargs)[source]

create a new instance with certain fields replaced

Parameters:

**kwargs – fields to replace

Returns:

a copy with replaced fields

Return type:

ReusableGenerator