Skip to main content

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.

Project description

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) -> None Pushes an item back into the iterator (LIFO order).
  • .has_next() -> bool Returns True if 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.

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

put_back_iterator-0.1.0a1.tar.gz (4.4 kB view details)

Uploaded Source

Built Distribution

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

put_back_iterator-0.1.0a1-py2.py3-none-any.whl (4.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file put_back_iterator-0.1.0a1.tar.gz.

File metadata

  • Download URL: put_back_iterator-0.1.0a1.tar.gz
  • Upload date:
  • Size: 4.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for put_back_iterator-0.1.0a1.tar.gz
Algorithm Hash digest
SHA256 9a721b36068a962b4b42c62c65fa241aa0962b645054d2838a2c2c6c2f7c3620
MD5 d1af236734b78b4498c377d0f3494513
BLAKE2b-256 a2a55bdedc17a9a87b3dda4b012bb411608760a173d5928899299177782e15da

See more details on using hashes here.

File details

Details for the file put_back_iterator-0.1.0a1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for put_back_iterator-0.1.0a1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 28975f85ae60f1ba5b717db0dfa4d903864fe5b7e54ded61df627ecba242c584
MD5 9f9249c52167d116ff5f5e7a43548919
BLAKE2b-256 e8dd8ff58613fef752e448963dd9f79d359ebf991d135f21e098b8cce7f0ba48

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