put-back-iterator 0.1.0a0__tar.gz

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,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,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,90 @@
1
+ # `put-back-iterator`
2
+
3
+ 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.
4
+
5
+ ## Why Use This?
6
+
7
+ Python's built-in iterators don't support pushing items back, forcing workarounds like:
8
+
9
+ - Rebuilding iterators with `itertools.chain`
10
+ - Using lists/deques as buffers
11
+ - Complex state tracking
12
+
13
+ This package solves it with:
14
+
15
+ - ✅ **Simple API**: `.put_back(item)` and `.has_next()`
16
+ - ✅ **Compatibility with `iter(iterable)`**: Works with any iterable (`str`, `list`, generators, etc.)
17
+ - ✅ **Python 2/3 compatible**
18
+
19
+ ## Quick Start
20
+
21
+ ```python
22
+ from put_back_iterator import PutBackIterator
23
+
24
+ # Wrap any iterable
25
+ it = PutBackIterator([1, 2, 3])
26
+
27
+ # Consume an item
28
+ print(next(it)) # 1
29
+
30
+ # Put it back for later
31
+ it.put_back(1)
32
+
33
+ # Check if more items exist (without consuming)
34
+ print(it.has_next()) # True
35
+
36
+ # Iterate as usual
37
+ print(list(it)) # [1, 2, 3]
38
+ ```
39
+
40
+ ## Use Cases
41
+
42
+ ### 1. **Parsing/Tokenizing**
43
+
44
+ ```python
45
+ tokens = PutBackIterator(tokenize(source_code))
46
+ while tokens.has_next():
47
+ token = next(tokens)
48
+ if token == 'IF':
49
+ # Peek ahead without consuming
50
+ if tokens.has_next() and next(tokens) == 'EOF':
51
+ tokens.put_back('EOF')
52
+ handle_incomplete_if()
53
+ ```
54
+
55
+ ### 2. **Backtracking Algorithms**
56
+
57
+ ```python
58
+ def backtrack(it):
59
+ item = next(it)
60
+ if not is_valid(item):
61
+ it.put_back(item) # Try alternative path
62
+ return fallback()
63
+ ```
64
+
65
+ ### 3. **Stream Processing**
66
+
67
+ ```python
68
+ for chunk in PutBackIterator(stream):
69
+ if needs_retry(chunk):
70
+ chunk = modify(chunk)
71
+ it.put_back(chunk) # Reprocess
72
+ ```
73
+
74
+ ## API Reference
75
+
76
+ ### **`PutBackIterator(iterable)`** Wraps an iterable.,Compatible with `iter(iterable)`.
77
+
78
+ ### Methods
79
+
80
+ - **`.put_back(item: T) -> None`** Pushes an item back into the iterator (LIFO order).
81
+ - **`.has_next() -> bool`** Returns `True` if more items exist (peeks without consuming).
82
+ - **`.__iter__()` and `.__next__()` / `.next()`** Compatible with Python 2/3 iterator protocol.
83
+
84
+ ## Contributing
85
+
86
+ Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
87
+
88
+ ## License
89
+
90
+ This project is licensed under the [MIT License](LICENSE).
@@ -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,9 @@
1
+ LICENSE
2
+ README.md
3
+ put_back_iterator.py
4
+ pyproject.toml
5
+ setup.cfg
6
+ put_back_iterator.egg-info/PKG-INFO
7
+ put_back_iterator.egg-info/SOURCES.txt
8
+ put_back_iterator.egg-info/dependency_links.txt
9
+ put_back_iterator.egg-info/top_level.txt
@@ -0,0 +1 @@
1
+ put_back_iterator
@@ -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
@@ -0,0 +1,26 @@
1
+ [build-system]
2
+ requires = ["setuptools>=61.0"]
3
+ build-backend = "setuptools.build_meta"
4
+
5
+ [project]
6
+ name = "put-back-iterator"
7
+ version = "0.1.0a0"
8
+ description = "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."
9
+ readme = "README.md"
10
+ requires-python = ">=2"
11
+ license = "MIT"
12
+ authors = [
13
+ { name="Jifeng Wu", email="jifengwu2k@gmail.com" }
14
+ ]
15
+ classifiers = [
16
+ "Programming Language :: Python :: 2",
17
+ "Programming Language :: Python :: 3",
18
+ "Operating System :: OS Independent",
19
+ ]
20
+ dependencies = [
21
+ ]
22
+
23
+
24
+ [project.urls]
25
+ "Homepage" = "https://github.com/jifengwu2k/put-back-iterator"
26
+ "Bug Tracker" = "https://github.com/jifengwu2k/put-back-iterator/issues"
@@ -0,0 +1,7 @@
1
+ [bdist_wheel]
2
+ universal = 1
3
+
4
+ [egg_info]
5
+ tag_build =
6
+ tag_date = 0
7
+