sized
Sized Generators with Decorators
Why?
- The
SizedGeneratortype provides a simple and robust way to keep track of iterations and max sizes for generators and iterators. - An issue with using normal Generators with long-running / multithread processes has been reporting progress.
- Here is an example of a standard
Generatorbeing wrapped withtqdm:
from tqdm import tqdm
def gen():
n = 2
for _ in range(100):
n += (5 * 1 / n)
yield n
for x in tqdm(gen()):
pass
# Output has no progress bar:
> 100it [00:00, 1.00it/s]
- A solution would be to keep track of the total progress, but this gets messy if an iteration is interrupted by user actions and the progress bar needs to continue.
- Now with the
sizeddecorator:
from sized import sized
@sized(100)
def s_gen():
n = 2
for _ in range(100):
n += (5 * 1 / n)
yield n
for x in tqdm(s_gen()):
pass
# Now with a progress bar!
> 100%|██████████| 100/100 [00:00<00:00, 1.00it/s]
SizedGeneratorwill also track iterations called and reflect remaining length
gen_instance = s_gen()
len(gen_instance) -> 100
next(gen_instance)
next(gen_instance)
len(gen_instance) -> 98
Getting started
There are 2 main ways to create a SizedGenerator
1. @sized decorator
from sized import sized
@sized(5)
def gen():
yield ...
2. SizedGenerator constructor
from sized import SizedGenerator
gen = SizedGenerator((x ** 2 for x in range(10)), 10)
Additional Info
- The
sizeargument can be either anint,Sizedobject, or a callable accepting a dictionary of arguments and keyword-arguments, returning anintorSizedobject.
@sized(15) # int
def gen():
for i in range(15):
yield i ** 2
ls = [1, 4, 5]
@sized(ls) # `Sized` objects will have len(obj) called automatically
def gen():
for i in ls:
yield i ** 2
@sized(lambda x: x['some_arg']) # Callable using keyword argument, returning `Sized`
def gen(arg = None):
for i in arg:
yield i ** 2
@sized(lambda x: 2 * len(x['1'])) # Callable using positional argument, returning `int`
def gen(arg_0, arg_1):
for _ in range(2):
for i in arg_1:
yield i ** 2 + arg_0
License
The code in this project is released under the MIT License.
Metadata
Release files for sized 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| sized-0.1.0.tar.gz | 5.2 kB | Without provenance | Details for sized-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| sized-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for sized-0.1.0-py3-none-any.whl |
Total release size: 10.1 kB
Release files / sized-0.1.0.tar.gz
| Download URL | sized-0.1.0.tar.gz |
|---|---|
| Size | 5.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64418002924c6975ae81eecd7f69312ed71cb5fd3362c7880e71314b0a933328
|
|
BLAKE2b-256 checksum How to use checksums |
dc5302a2082c1602b6266c7377178cf9bae46c807ed1fac931bc3e9e035ea159
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.5 Linux/5.15.0-1014-azure
|
Release files / sized-0.1.0-py3-none-any.whl
| Download URL | sized-0.1.0-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2075288a76d20fb407b9ec52d0c3ccc7c9a14fca563e547cc56dc9cffc2df57e
|
|
BLAKE2b-256 checksum How to use checksums |
cbf9afffdf636685a3978302c684ff7f1eb6676615b2938091425c63889af456
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.13 CPython/3.10.5 Linux/5.15.0-1014-azure
|
Security for sized 0.1.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.