Skip to main content

Cache generator's results and reuse them

Project description

reusegen

PyPI Travis (.org)

Generators are exhausted during iteration:

xs = (x * x for x in range(10))
ys = [x + 1 for x in xs]  # [1, 2, ...]
zs = [x + 2 for x in xs]  # []

Sometimes this is not expected. With reusegen, you can reuse generators like lists:

from reusegen import reuse
xs = reuse(x * x for x in range(10))
ys = [x + 1 for x in xs]
zs = [x + 1 for x in xs]
print(ys == zs)  # True

And it also works as decorator:

@reuse
def double(xs):
  for x in xs:
    yield x * 2

By default, the results of generator is cached. You could make it cacheless to save memory as original generator:

xs = reuse(x * x for x in range(10), cache=False)

The generator will be executed multiple times.

TODO

Add cacheless version of reuse.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reusegen-0.1.2.tar.gz (2.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reusegen-0.1.2-py3-none-any.whl (3.0 kB view details)

Uploaded Python 3

File details

Details for the file reusegen-0.1.2.tar.gz.

File metadata

  • Download URL: reusegen-0.1.2.tar.gz
  • Upload date:
  • Size: 2.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for reusegen-0.1.2.tar.gz
Algorithm Hash digest
SHA256 2e9ce296f6be95ac1870672ea1ca35bb7579b462481a8d8ca845ea3494d3b512
MD5 467e64265c1db675280aac1a012671bf
BLAKE2b-256 81de40139896d8be12f491a521f1d20326795bcb2d4f9f5278b274160c58e6b4

See more details on using hashes here.

File details

Details for the file reusegen-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: reusegen-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for reusegen-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ce693bd9bae0dfa2a27b6ddf570c33101b3e44002664e12aae0f98d4b30fea07
MD5 743ba05e59702a2f12765d70a8ece6fc
BLAKE2b-256 e900f885efd81d4296a643753383860d0e59fdebee4e7292f28b297413b924e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page