Skip to main content

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

Metadata

Release files for sliceable-generator 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sliceable-generator 0.1.2
File Size Uploaded Provenance Actions
sliceable-generator-0.1.2.tar.gz 4.0 kB Without provenance Details for sliceable-generator-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sliceable-generator 0.1.2
File Interpreter ABI Platform Actions
sliceable_generator-0.1.2-py3-none-any.whl Python 3 none any Details for sliceable_generator-0.1.2-py3-none-any.whl

Total release size: 8.8 kB

Release files / sliceable-generator-0.1.2.tar.gz

Download URL sliceable-generator-0.1.2.tar.gz
Size 4.0 kB
Tags Source
SHA-256 checksum
How to use checksums
474495ec459ea6fda01bac59d1481cb6e72e99c7cd51ee130ceb540e031bdd24
BLAKE2b-256 checksum
How to use checksums
5a55adffdf40f68049b7563c08492d18b741b708c1813b21b3f551efd9f0ca55
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / sliceable_generator-0.1.2-py3-none-any.whl

Download URL sliceable_generator-0.1.2-py3-none-any.whl
Size 4.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b63cb3f1c807668abf4f4fdd6fabffdb090a65a140cf44e8c39ba2263542bc96
BLAKE2b-256 checksum
How to use checksums
a85ea5d7d0f9bd8d742e32cf5a9f27d7926705ed49c81e42444c28c83b918ba6
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for sliceable-generator 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page