This release is a pre-release and may not be stable for production use.
put-back-iterator
A drop-in replacement for iter(iterable) that allows putting items back after consuming them. Ideal for parsing, lexing, and other scenarios requiring lookahead or backtracking.
Why Use This?
Python's built-in iterators don't support pushing items back, forcing workarounds like:
- Rebuilding iterators with
itertools.chain - Using lists/deques as buffers
- Complex state tracking
This package solves it with:
- ✅ Simple API:
.put_back(item)and.has_next() - ✅ Compatibility with
iter(iterable): Works with any iterable (str,list, generators, etc.) - ✅ Python 2/3 compatible
Quick Start
# coding=utf-8
from __future__ import print_function
from put_back_iterator import PutBackIterator
# Wrap any iterable
it = PutBackIterator([1, 2, 3])
# Consume an item
print(next(it)) # 1
# Put it back for later
it.put_back(1)
# Check if more items exist (without consuming)
print(it.has_next()) # True
# Iterate as usual
print(list(it)) # [1, 2, 3]
Use Cases
1. Parsing/Tokenizing
tokens = PutBackIterator(tokenize(source_code))
while tokens.has_next():
token = next(tokens)
if token == 'IF':
# Peek ahead without consuming
if tokens.has_next() and next(tokens) == 'EOF':
tokens.put_back('EOF')
handle_incomplete_if()
2. Backtracking Algorithms
def backtrack(it):
item = next(it)
if not is_valid(item):
it.put_back(item) # Try alternative path
return fallback()
3. Stream Processing
for chunk in PutBackIterator(stream):
if needs_retry(chunk):
chunk = modify(chunk)
it.put_back(chunk) # Reprocess
API Reference
PutBackIterator(iterable) Wraps an iterable.,Compatible with iter(iterable).
Methods
.put_back(item: T) -> NonePushes an item back into the iterator (LIFO order)..has_next() -> boolReturnsTrueif more items exist (peeks without consuming)..__iter__()and.__next__()/.next()Compatible with Python 2/3 iterator protocol.
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Metadata
Release files for put-back-iterator 0.1.0a1
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 |
|---|---|---|---|---|
| put_back_iterator-0.1.0a1.tar.gz | 4.4 kB | Without provenance | Details for put_back_iterator-0.1.0a1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| put_back_iterator-0.1.0a1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details for put_back_iterator-0.1.0a1-py2.py3-none-any.whl |
Total release size: 9.3 kB
Release files / put_back_iterator-0.1.0a1.tar.gz
| Download URL | put_back_iterator-0.1.0a1.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9a721b36068a962b4b42c62c65fa241aa0962b645054d2838a2c2c6c2f7c3620
|
|
BLAKE2b-256 checksum How to use checksums |
a2a55bdedc17a9a87b3dda4b012bb411608760a173d5928899299177782e15da
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Release files / put_back_iterator-0.1.0a1-py2.py3-none-any.whl
| Download URL | put_back_iterator-0.1.0a1-py2.py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
28975f85ae60f1ba5b717db0dfa4d903864fe5b7e54ded61df627ecba242c584
|
|
BLAKE2b-256 checksum How to use checksums |
e8dd8ff58613fef752e448963dd9f79d359ebf991d135f21e098b8cce7f0ba48
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.2
|
Security for put-back-iterator 0.1.0a1
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.