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.

Files for reusegen, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size reusegen-0.1.2-py3-none-any.whl (3.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size reusegen-0.1.2.tar.gz (2.0 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page