Sliceable subscriptable reusable generator.
Project description
sliceable-generator: Powerful generator for Python
In python, generators are designed for one-time use, zero space complexity, lazy computation collection.
However, sometimes we need a lazy computation list, with the capability to index, slice and reuse, which is the propose of this package.
Installation
pip install sliceable-generator
Usage
Sliceable generator aims to provide the same experience as builtin generators, but more powerful. So, simply wrap the builtin generators is ok.
from sliceable_generator import SliceableGenerator
g = SliceableGenerator(range(300, 400))
for i in g[50:53]:
print(i)
# 350
# 351
# 352
Nested generators
Pay attention that python builtin generators will perform unexpected when nested. The parent variable will not pass to child generators.
g = (((i, j) for j in range(20, 30)) for i in range(10, 20))
sub1, sub2, sub3 = next(g), next(g), next(g)
print(list(sub2))
# [(12, 20), (12, 21), (12, 22), (12, 23), (12, 24), (12, 25), (12, 26), (12, 27), (12, 28), (12, 29)]
print(list(sub1))
# [(12, 20), (12, 21), (12, 22), (12, 23), (12, 24), (12, 25), (12, 26), (12, 27), (12, 28), (12, 29)]
Sliceable generator will not operate such case, so if you use nested generators, please manage your generator like such code:
g = ((lambda i: ((i, j) for j in range(20, 30)))(i) for i in range(10, 20))
sub1, sub2, sub3 = next(g), next(g), next(g)
print(list(sub2))
# [(11, 20), (11, 21), (11, 22), (11, 23), (11, 24), (11, 25), (11, 26), (11, 27), (11, 28), (11, 29)]
print(list(sub1))
# [(10, 20), (10, 21), (10, 22), (10, 23), (10, 24), (10, 25), (10, 26), (10, 27), (10, 28), (10, 29)]
Thanks to QQ user 村长
for providing this solution.
If provided correct nested generator, wrap it with SliceableGenerator
:
from sliceable_generator import SliceableGenerator
g = SliceableGenerator(((lambda i: ((i, j) for j in range(20, 30)))(i) for i in range(10, 20)), depth=2)
print(g[3:5, 4:6].to_list())
# [[(13, 24), (13, 25)], [(14, 24), (14, 25)]]
print(g[3:5, 4].to_list())
# [(13, 24), (14, 24)]
Attention
Pay attention that this package provide a low performance generator implementation. Only when you need both lazy computation and subscript functions, you can use this package. Otherwise, use builtin generator expressions instead.
Contribution
This is a tiny package and if you want to contribute, just raise a pull request, and any proposals are welcome!
History
- v0.1.2: fix type hint bug
- v0.1.1: add type hint
- v0.1.0: finish basic logic
- length of generator
- reusable generator
- sliceable generator
- subscriptable generator
- nested generator
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
Built Distribution
File details
Details for the file sliceable-generator-0.1.2.tar.gz
.
File metadata
- Download URL: sliceable-generator-0.1.2.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 474495ec459ea6fda01bac59d1481cb6e72e99c7cd51ee130ceb540e031bdd24 |
|
MD5 | 06b327a699a2291ef274852ffc628098 |
|
BLAKE2b-256 | 5a55adffdf40f68049b7563c08492d18b741b708c1813b21b3f551efd9f0ca55 |
File details
Details for the file sliceable_generator-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: sliceable_generator-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.8.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b63cb3f1c807668abf4f4fdd6fabffdb090a65a140cf44e8c39ba2263542bc96 |
|
MD5 | cbea5bb6449450f5e7e3dda3b23f7f0d |
|
BLAKE2b-256 | a85ea5d7d0f9bd8d742e32cf5a9f27d7926705ed49c81e42444c28c83b918ba6 |