Gentools
Tools for generators, generator functions, and generator-based coroutines.
Key features:
Create reusable generators
Compose generators
Build python 2/3-compatible generators (gentools version <1.2 only)
Installation
pip install gentools
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]
>>> isinstance(from_3, countdown) # generator func is wrapped in a class
True
>>> from_3.step # attribute access to arguments
1
>>> from_3.replace(value=5) # create new instance with replaced fields
countdown(value=5, step=1) # descriptive repr()
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')
relay a generator’s yield/send interactions through another generator:
>>> 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)
make python 2/3 compatible generators with return. (gentools version <1.2 only)
>>> @py2_compatible
... def my_max(value):
... while value < 100:
... newvalue = yield value
... if newvalue > value:
... value = newvalue
... return_(value)
Metadata
Release files for gentools 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| gentools-1.2.2.tar.gz | 6.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| gentools-1.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 15.7 kB
Release files / gentools-1.2.2.tar.gz
| Download URL | gentools-1.2.2.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c692af51d5a8f7f3406ed8c39d6da686f4f2fe4dbfaa13ddb24035b0471d9305
|
|
BLAKE2b-256 checksum How to use checksums |
214d68ba01cf9e6da4d024c13684d97206346e9ba11ad470516104e1cd13d857
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.5
|
Release files / gentools-1.2.2-py3-none-any.whl
| Download URL | gentools-1.2.2-py3-none-any.whl |
|---|---|
| Size | 9.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
75b7b0452691115ad11914e17c8c2b5f9b146b01f20167785e8b6d99b565d190
|
|
BLAKE2b-256 checksum How to use checksums |
1412e495615dc5e47c2ad40768c661fdd70e9863cd6aec9d6aac07c2888d6fd8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.5
|