Wrapper for Python iterators and iterables that implements a list-like random-access interface.
Project description
Wrapper for Python iterators and iterables that implements a list-like random-access interface by caching retrieved items for later reuse.
Package Installation and Usage
The package is available on PyPI:
python -m pip install reiter
The library can be imported in the usual way:
import reiter from reiter import reiter
Examples
The library makes it possible to wrap iterators and iterables within an interface that enables repeated iteration over – and random access by index of – the items contained within. A reiter instance yields the same sequence of items as the wrapped iterator:
>>> from reiter import reiter >>> xs = iter([1, 2, 3]) >>> ys = reiter(xs) >>> list(ys) [1, 2, 3]
However, unlike an iterator, the instance of this class can be iterated any number of times:
>>> list(ys), list(ys) ([1, 2, 3], [1, 2, 3])
Furthermore, it is also possible to access elements by their index:
>>> xs = iter([1, 2, 3]) >>> ys = reiter(xs) >>> ys[0], ys[1], ys[2] (1, 2, 3)
The built-in Python next function is also supported, and any attempt to retrieve an item once the sequence of items is exhausted raises an exception in the usual manner:
>>> xs = reiter(iter([1, 2, 3])) >>> next(xs), next(xs), next(xs) (1, 2, 3) >>> next(xs) Traceback (most recent call last): ... StopIteration
However, all items yielded during iteration can be accessed by their index, and it is also possible to iterate over those items again:
>>> xs[0], xs[1], xs[2] (1, 2, 3) >>> [x for x in xs] [1, 2, 3]
Instances of reiter support additional methods, as well. For example, the has method returns a boolean value indicating whether a next item is available and the length method returns the length of the sequence of items emitted by the instance (once no more items can be emitted):
>>> xs = reiter(iter([1, 2, 3])) >>> xs.has(), xs.has(), xs.has(), xs.has() (True, True, True, False) >>> xs.length() 3
Documentation
The documentation can be generated automatically from the source files using Sphinx:
cd docs python -m pip install -r requirements.txt sphinx-apidoc -f -E --templatedir=_templates -o _source .. ../setup.py && make html
Testing and Conventions
All unit tests are executed and their coverage is measured when using pytest (see setup.cfg for configuration details):
python -m pip install pytest pytest-cov python -m pytest
All unit tests are included in the module itself and can be executed using doctest:
python reiter/reiter.py -v
Style conventions are enforced using Pylint:
python -m pip install pylint python -m pylint reiter
Contributions
In order to contribute to the source code, open an issue or submit a pull request on the GitHub page for this library.
Versioning
The version number format for this library and the changes to the library associated with version number increments conform with Semantic Versioning 2.0.0.
Publishing
This library can be published as a package on PyPI by a package maintainer. Install the wheel package, remove any old build/distribution files, and package the source into a distribution archive:
python -m pip install wheel rm -rf dist *.egg-info python setup.py sdist bdist_wheel
Next, install the twine package and upload the package distribution archive to PyPI:
python -m pip install twine python -m twine upload dist/*
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.