put-back-iterator 0.1.0a0__py2.py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,106 @@
|
|
1
|
+
Metadata-Version: 2.4
|
2
|
+
Name: put-back-iterator
|
3
|
+
Version: 0.1.0a0
|
4
|
+
Summary: 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.
|
5
|
+
Author-email: Jifeng Wu <jifengwu2k@gmail.com>
|
6
|
+
License-Expression: MIT
|
7
|
+
Project-URL: Homepage, https://github.com/jifengwu2k/put-back-iterator
|
8
|
+
Project-URL: Bug Tracker, https://github.com/jifengwu2k/put-back-iterator/issues
|
9
|
+
Classifier: Programming Language :: Python :: 2
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
11
|
+
Classifier: Operating System :: OS Independent
|
12
|
+
Requires-Python: >=2
|
13
|
+
Description-Content-Type: text/markdown
|
14
|
+
License-File: LICENSE
|
15
|
+
Dynamic: license-file
|
16
|
+
|
17
|
+
# `put-back-iterator`
|
18
|
+
|
19
|
+
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.
|
20
|
+
|
21
|
+
## Why Use This?
|
22
|
+
|
23
|
+
Python's built-in iterators don't support pushing items back, forcing workarounds like:
|
24
|
+
|
25
|
+
- Rebuilding iterators with `itertools.chain`
|
26
|
+
- Using lists/deques as buffers
|
27
|
+
- Complex state tracking
|
28
|
+
|
29
|
+
This package solves it with:
|
30
|
+
|
31
|
+
- ✅ **Simple API**: `.put_back(item)` and `.has_next()`
|
32
|
+
- ✅ **Compatibility with `iter(iterable)`**: Works with any iterable (`str`, `list`, generators, etc.)
|
33
|
+
- ✅ **Python 2/3 compatible**
|
34
|
+
|
35
|
+
## Quick Start
|
36
|
+
|
37
|
+
```python
|
38
|
+
from put_back_iterator import PutBackIterator
|
39
|
+
|
40
|
+
# Wrap any iterable
|
41
|
+
it = PutBackIterator([1, 2, 3])
|
42
|
+
|
43
|
+
# Consume an item
|
44
|
+
print(next(it)) # 1
|
45
|
+
|
46
|
+
# Put it back for later
|
47
|
+
it.put_back(1)
|
48
|
+
|
49
|
+
# Check if more items exist (without consuming)
|
50
|
+
print(it.has_next()) # True
|
51
|
+
|
52
|
+
# Iterate as usual
|
53
|
+
print(list(it)) # [1, 2, 3]
|
54
|
+
```
|
55
|
+
|
56
|
+
## Use Cases
|
57
|
+
|
58
|
+
### 1. **Parsing/Tokenizing**
|
59
|
+
|
60
|
+
```python
|
61
|
+
tokens = PutBackIterator(tokenize(source_code))
|
62
|
+
while tokens.has_next():
|
63
|
+
token = next(tokens)
|
64
|
+
if token == 'IF':
|
65
|
+
# Peek ahead without consuming
|
66
|
+
if tokens.has_next() and next(tokens) == 'EOF':
|
67
|
+
tokens.put_back('EOF')
|
68
|
+
handle_incomplete_if()
|
69
|
+
```
|
70
|
+
|
71
|
+
### 2. **Backtracking Algorithms**
|
72
|
+
|
73
|
+
```python
|
74
|
+
def backtrack(it):
|
75
|
+
item = next(it)
|
76
|
+
if not is_valid(item):
|
77
|
+
it.put_back(item) # Try alternative path
|
78
|
+
return fallback()
|
79
|
+
```
|
80
|
+
|
81
|
+
### 3. **Stream Processing**
|
82
|
+
|
83
|
+
```python
|
84
|
+
for chunk in PutBackIterator(stream):
|
85
|
+
if needs_retry(chunk):
|
86
|
+
chunk = modify(chunk)
|
87
|
+
it.put_back(chunk) # Reprocess
|
88
|
+
```
|
89
|
+
|
90
|
+
## API Reference
|
91
|
+
|
92
|
+
### **`PutBackIterator(iterable)`** Wraps an iterable.,Compatible with `iter(iterable)`.
|
93
|
+
|
94
|
+
### Methods
|
95
|
+
|
96
|
+
- **`.put_back(item: T) -> None`** Pushes an item back into the iterator (LIFO order).
|
97
|
+
- **`.has_next() -> bool`** Returns `True` if more items exist (peeks without consuming).
|
98
|
+
- **`.__iter__()` and `.__next__()` / `.next()`** Compatible with Python 2/3 iterator protocol.
|
99
|
+
|
100
|
+
## Contributing
|
101
|
+
|
102
|
+
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
|
103
|
+
|
104
|
+
## License
|
105
|
+
|
106
|
+
This project is licensed under the [MIT License](LICENSE).
|
@@ -0,0 +1,6 @@
|
|
1
|
+
put_back_iterator.py,sha256=2ME12t-aK1xpCMwAae0yVTrFYIWQPBdNC3emHUpt7JI,1365
|
2
|
+
put_back_iterator-0.1.0a0.dist-info/licenses/LICENSE,sha256=FZ9XWedK_wQ4wfqVanrQVQpArRHDkxwxic2rgii1pZg,1066
|
3
|
+
put_back_iterator-0.1.0a0.dist-info/METADATA,sha256=GGca8R3UzTev5VB4uBbewobeaql8BeVC6SXrYVxXhLc,3008
|
4
|
+
put_back_iterator-0.1.0a0.dist-info/WHEEL,sha256=JNWh1Fm1UdwIQV075glCn4MVuCRs0sotJIq-J6rbxCU,109
|
5
|
+
put_back_iterator-0.1.0a0.dist-info/top_level.txt,sha256=8XJh5rF6eOPUkwrmXZutOmEHGnbXK6VqPPp6mrvkrs4,18
|
6
|
+
put_back_iterator-0.1.0a0.dist-info/RECORD,,
|
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Jifeng Wu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
@@ -0,0 +1 @@
|
|
1
|
+
put_back_iterator
|
put_back_iterator.py
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
from __future__ import absolute_import, division, print_function
|
2
|
+
import sys
|
3
|
+
|
4
|
+
|
5
|
+
_DEFAULT_PLACEHOLDER = object()
|
6
|
+
|
7
|
+
class PutBackIterator:
|
8
|
+
def __init__(self, iterable):
|
9
|
+
self._iterator = iter(iterable)
|
10
|
+
self._put_back_stack = []
|
11
|
+
|
12
|
+
def __iter__(self):
|
13
|
+
return self
|
14
|
+
|
15
|
+
def _next_impl(self, default=_DEFAULT_PLACEHOLDER):
|
16
|
+
if self._put_back_stack:
|
17
|
+
return self._put_back_stack.pop()
|
18
|
+
else:
|
19
|
+
end_sentinel = object()
|
20
|
+
element_or_sentinel = next(self._iterator, end_sentinel)
|
21
|
+
if element_or_sentinel is end_sentinel:
|
22
|
+
if default is _DEFAULT_PLACEHOLDER:
|
23
|
+
raise StopIteration
|
24
|
+
else:
|
25
|
+
return default
|
26
|
+
else:
|
27
|
+
return element_or_sentinel
|
28
|
+
|
29
|
+
if sys.version_info < (3,):
|
30
|
+
next = _next_impl
|
31
|
+
else:
|
32
|
+
__next__ = _next_impl
|
33
|
+
|
34
|
+
def put_back(self, element):
|
35
|
+
self._put_back_stack.append(element)
|
36
|
+
|
37
|
+
def has_next(self):
|
38
|
+
if self._put_back_stack:
|
39
|
+
return True
|
40
|
+
else:
|
41
|
+
end_sentinel = object()
|
42
|
+
element_or_sentinel = next(self, end_sentinel)
|
43
|
+
if element_or_sentinel is end_sentinel:
|
44
|
+
return False
|
45
|
+
else:
|
46
|
+
self._put_back_stack.append(element_or_sentinel)
|
47
|
+
return True
|