Skip to main content

lazysequence

PyPI Status Python Version License Read the documentation at https://lazysequence.readthedocs.io/ Tests Codecov

A lazy sequence makes an iterator look like an immutable sequence:

from lazysequence import lazysequence

def load_records():
    return range(10)  # let's pretend this is expensive

records = lazysequence(load_records())
if not records:
    raise SystemExit("no records found")

first, second = records[:2]

print("The first record is", first)
print("The second record is", second)

for record in records.release():  # do not cache all records in memory
    print("record", record)

Why?

Sometimes you need to peek ahead at items returned by an iterator. But what if later code needs to see all the items from the iterator? Then you have some options:

  1. Pass any consumed items separately. This can get messy, though.

  2. Copy the iterator into a sequence beforehand, if that does not take a lot of space or time.

  3. Duplicate the iterator using itertools.tee, or write your own custom itertool that buffers consumed items internally. There are some good examples of this approach on SO, by Alex Martelli, Raymond Hettinger, and Ned Batchelder.

A lazy sequence combines advantages from option 2 and option 3. It is an immutable sequence that wraps the iterable and caches consumed items in an internal buffer. By implementing collections.abc.Sequence, lazy sequences provide the full set of sequence operations on the iterable. Unlike a copy (option 2), but like a duplicate (option 3), items are only consumed and stored in memory as far as required for any given operation.

There are some caveats:

  • The lazy sequence will eventually store all items in memory. If this is a problem, use s.release() to obtain an iterator over the sequence items without further caching. After calling this function, the sequence should no longer be used.

  • Explicit is better than implicit. Clients may be better off being passed an iterator and dealing with its limitations. For example, clients may not expect len(s) to incur the cost of consuming the iterator to its end.

Installation

You can install lazysequence via pip from PyPI:

$ pip install lazysequence

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, lazysequence is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from @cjolowicz’s Hypermodern Python Cookiecutter template.

Metadata

Release files for lazysequence 0.3.0

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

Source distribution (sdist)

Source distribution for lazysequence 0.3.0
File Size Uploaded Provenance
lazysequence-0.3.0.tar.gz 7.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for lazysequence 0.3.0
File Interpreter ABI Platform
lazysequence-0.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 14.8 kB

Release files / lazysequence-0.3.0.tar.gz

Download URL lazysequence-0.3.0.tar.gz
Size 7.6 kB
Tags Source
SHA-256 checksum
How to use checksums
f5712a80f3bb14d2d77ccb777aad7d327fe9d4dc4cba0393247780d83d2b2fe0
BLAKE2b-256 checksum
How to use checksums
e29728728ae0e2c0ec4a2cb8d7030a2e8f2356b4d246b461cfe4993a91e1f85f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Release files / lazysequence-0.3.0-py3-none-any.whl

Download URL lazysequence-0.3.0-py3-none-any.whl
Size 7.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8f01e3499d17dae6020947ea15df3c3c2b7fa66f7d2a6f1548aed5e050bba927
BLAKE2b-256 checksum
How to use checksums
dd95bd5e24669aa21c6238861410ba3b4743e1e9486680de4e7c630e37bbd886
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.4.1 importlib_metadata/4.6.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.61.2 CPython/3.9.6

Security for lazysequence 0.3.0

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