This release is a pre-release and may not be stable for production use.
ben42code.myitertools
Providing some additional capabilities on top of itertools.
Everything here is lazy and stream-friendly: values are pulled from the source only as far as the requested result requires.
Installation
pip install ben42code.myitertools
Requires Python 3.10+.
Features
| Feature | Intent |
|---|---|
islice_extended |
itertools.islice with negative start/stop/step support. |
IteratorCounter |
Transparent wrapper that counts the values pulled through it. |
StreamSequence |
A lazy Sequence view (len, indexing, slicing) over a one-shot iterator. |
collapse |
Lazily flatten arbitrarily nested iterables, with per-type customisation. |
The
>>>examples below are executed as doctests by the test suite, so they are guaranteed to stay in sync with the code.
Stability
This project follows Semantic Versioning.
The public API is exactly the names exported from ben42code.myitertools
(islice_extended, IteratorCounter, StreamSequence, collapse). Anything
else — including the underscore-prefixed modules — is internal and may change
without notice.
⚠️ Pre-1.0: while the version is 0.x, the public API may still change in
backwards-incompatible ways between minor releases. Pin a version
(ben42code.myitertools==0.0.6) if you need stability. Once 1.0.0 ships,
breaking changes will only occur in major releases.
islice_extended
islice_extended(iterable: Iterable[T], stop: int | None) -> Iterator[T]
islice_extended(iterable: Iterable[T], start: int | None, stop: int | None, step: int | None = 1) -> Iterator[T]
Slice any iterable the way list[start:stop:step] would — including negative
start/stop indices and a negative step — pulling from the source lazily
instead of materialising it up front.
>>> from ben42code.myitertools import islice_extended
>>> list(islice_extended([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], -1, -5, -1))
[9, 8, 7, 6]
>>> list(islice_extended([0, 1, 2], None, None, -1))
[2, 1, 0]
Notes
- Non-negative bounds + positive step → pure
itertools.islicedelegation, O(1) extra memory. - ⚠️ A negative
start/stopmust find the end of the stream: the input is consumed to exhaustion and buffered — O(n) time and memory (hangs on an unbounded source). - A negative
step(with non-negative bounds) buffers up tostart + 1values — O(start). - Buffered values are released as they are yielded, so retained memory shrinks while you iterate.
IteratorCounter
IteratorCounter(iterator: Iterator[T] | Iterable[T])
count: int # values pulled from the source so far
Wrap an iterator/iterable and expose, at any time, how many values have been pulled through it — without changing what flows downstream.
>>> from itertools import islice
>>> from ben42code.myitertools import IteratorCounter
>>> wrapper = IteratorCounter('ABCDEFGHIJKLMNOP')
>>> list(islice(wrapper, 2, 5))
['C', 'D', 'E']
>>> wrapper.count
5
Notes
- Fully lazy and single-pass: O(1) time per value, O(1) extra memory.
.countreflects values actually pulled from the source, so it doubles as a probe of how far a downstream consumer really advanced the stream (e.g.islice(_, 2, 5)pulls5, not3).
StreamSequence
StreamSequence(iterable: Iterable[T])
take(n: int | None) -> list[T] # advance the visible front, returning the values
consume(n: int | None) -> None # advance the visible front, discarding the values
Expose a one-shot iterator/iterable (generator, file, socket, itertools chain …)
through the
collections.abc.Sequence
protocol — len(), positive/negative indexing, iteration and slicing — while
still consuming the source lazily.
>>> from itertools import count
>>> from ben42code.myitertools import StreamSequence
>>> stream = StreamSequence(count(0)) # unbounded source
>>> stream[3] # pulls only 0..3
3
>>> stream.take(3)
[0, 1, 2]
>>> stream[0] # the visible front has advanced
3
>>> StreamSequence(count(0))[::2].take(5) # lazy slice over an unbounded source
[0, 2, 4, 6, 8]
Notes
- On-demand: a non-negative index or
take(n)pulls only as far as needed; with no live sub-iterators,consume/takestream straight past the cache — O(1) retained memory on a self-draining source. - Slices and previously created iterators keep their own anchor, so they still see values the parent moved past; buffering is bounded to what live sub-iterators hold back, and released values are tombstoned and compacted in amortised O(1).
- ⚠️ Operations that must reach the end —
len(), negative indexing,__contains__/indexon a miss,count,reversed— walk the source to exhaustion and hang on an unbounded stream; use them only on a bounded view. - Built for one-shot sources: wrapping a random-access
Sequence(list,tuple,range…) only adds indirection — use it directly instead. - ⚠️ Not thread-safe: a
StreamSequenceand its sub-iterators need external synchronisation for concurrent use.
collapse
collapse(iterable: Iterable, *, handlers: Mapping[type, Callable[[Any], Iterable]] | None = None, atoms: tuple[type, ...] | None = None) -> Iterator
Lazily flatten an arbitrarily nested iterable into a stream of leaves, pulling from the source only as far as the consumer requests — with optional per-type rules for what counts as a leaf and how to transform specific types.
>>> from ben42code.myitertools import collapse
>>> list(collapse([1, [2, [3, 4], 5], 6]))
[1, 2, 3, 4, 5, 6]
>>> list(collapse([1, "AB", 2])) # strings are kept whole by default
[1, 'AB', 2]
>>> handlers = {str: lambda s: s.encode("ascii")}
>>> bytes(collapse([10, "AB", [11, 12]], handlers=handlers, atoms=()))
b'\nAB\x0b\x0c'
Notes
- Fully lazy: a nested source is walked depth-first and pulled only as far as needed, so it stays safe under
isliceon an unbounded/deeply nested stream. handlers={type: fn}— a matching element is replaced byfn(element)and the result is flattened in turn (re-resolved), enabling chains likestr → bytes → ints.atomsare yielded whole instead of being iterated into; when omitted it defaults to(str, bytes)so text is not exploded into characters. Pass an explicit tuple (e.g.atoms=()) to override.- A type may be an atom or a handler, not both. When an element matches several registered types, the most specific one wins (by MRO, abstract base classes included) — independent of declaration order.
- Overlapping an explicit
atomsentry with a handler raisesValueError(overriding a default atom with a handler is allowed); an element matching two unrelated registered types resolves ambiguously and raisesValueError. - Uses an explicit stack rather than recursion, so arbitrarily deep nesting cannot raise
RecursionError. - ⚠️ A handler may not terminate if its output keeps resolving to a handled type (e.g. a
strhandler that keeps returningstrvalues).
Metadata
Release files for ben42code.myitertools 0.0.7rc26
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 |
|---|---|---|---|---|
| ben42code_myitertools-0.0.7rc26.tar.gz | 17.1 kB | With provenance | Details for ben42code_myitertools-0.0.7rc26.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ben42code_myitertools-0.0.7rc26-py3-none-any.whl | Python 3 | none | any | Details for ben42code_myitertools-0.0.7rc26-py3-none-any.whl |
Total release size: 33.5 kB
Release files / ben42code_myitertools-0.0.7rc26.tar.gz
| Download URL | ben42code_myitertools-0.0.7rc26.tar.gz |
|---|---|
| Size | 17.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d98eec2bf478beee8ca09d62fff25ebd983308e546bbb9c659417d6282dfd378
|
|
BLAKE2b-256 checksum How to use checksums |
752f4cf40960ac572bec3d70cfd9c4bffdf59fcbe940bc5ac72bf658285bbb08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logRelease files / ben42code_myitertools-0.0.7rc26-py3-none-any.whl
| Download URL | ben42code_myitertools-0.0.7rc26-py3-none-any.whl |
|---|---|
| Size | 16.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
b5b7a82c0e4d1fd84e9a6a133d47ade80c83a3f223eb2d104fc0b83ef49ad6b1
|
|
BLAKE2b-256 checksum How to use checksums |
3467d2c7e9331c97bcded6bd8afa4cef1dcda1dd9ea0682eb10961691faa86f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logSecurity for ben42code.myitertools 0.0.7rc26
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for ben42code_myitertools-0.0.7rc26.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 12, 2026.
Transparency logA 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.