A lightweight Python library providing one-line iterator and algorithm helpers
Project description
itersys
A lightweight Python library providing one-line iterator and algorithm helpers. Simplify your daily tasks with powerful utilities for chunking lists, flattening iterables, sliding windows, common algorithms, combinatorial operations, and nested dictionary utilities.
Installation
pip install itersys
Or install from source:
git clone https://github.com/ZeroDevsLtd/itersys.git
cd itersys
pip install -e .
Quick Start
import itersys
# Chunk a list
print(list(itersys.chunk([1, 2, 3, 4, 5], 2))) # [[1, 2], [3, 4], [5]]
# Flatten nested structures
print(list(itersys.flatten([1, [2, 3], [4, [5]]]))) # [1, 2, 3, 4, 5]
# Sort with merge sort
print(itersys.merge_sort([5, 2, 9, 1])) # [1, 2, 5, 9]
Features
Iteration Utilities (itersys.iteration)
chunk(iterable, size)
Split an iterable into chunks of the specified size.
from itersys.iteration import chunk
print(list(chunk([1, 2, 3, 4, 5], 2))) # [[1, 2], [3, 4], [5]]
print(list(chunk("abcdef", 3))) # [['a', 'b', 'c'], ['d', 'e', 'f']]
window(iterable, size)
Create a sliding window over an iterable.
from itersys.iteration import window
print(list(window([1, 2, 3, 4, 5], 3))) # [(1, 2, 3), (2, 3, 4), (3, 4, 5)]
flatten(nested_iterable)
Flatten a nested iterable structure.
from itersys.iteration import flatten
print(list(flatten([1, [2, 3], [4, [5]]]))) # [1, 2, 3, 4, 5]
print(list(flatten([[1, 2], [3, 4]]))) # [1, 2, 3, 4]
unique_by(iterable, key=lambda x: x)
Return unique items from an iterable based on a key function.
from itersys.iteration import unique_by
print(list(unique_by([1, 2, 2, 3, 1]))) # [1, 2, 3]
# With custom key function
data = [{'a': 1}, {'a': 1}, {'a': 2}]
print(list(unique_by(data, lambda x: x['a']))) # [{'a': 1}, {'a': 2}]
Algorithms (itersys.algorithms)
binary_search(arr, target)
Perform binary search on a sorted list.
from itersys.algorithms import binary_search
arr = [1, 2, 3, 4, 5]
print(binary_search(arr, 3)) # 2
print(binary_search(arr, 6)) # None
merge_sort(arr)
Sort a list using merge sort algorithm.
from itersys.algorithms import merge_sort
print(merge_sort([5, 2, 9, 1])) # [1, 2, 5, 9]
print(merge_sort([3, 1, 4, 1, 5, 9, 2, 6])) # [1, 1, 2, 3, 4, 5, 6, 9]
fibonacci(n)
Calculate the nth Fibonacci number.
from itersys.algorithms import fibonacci
print(fibonacci(5)) # 5
print(fibonacci(10)) # 55
gcd(a, b)
Calculate the greatest common divisor of two integers.
from itersys.algorithms import gcd
print(gcd(48, 18)) # 6
print(gcd(17, 13)) # 1
Combinatorics (itersys.combinatorics)
powerset(iterable)
Generate the powerset of an iterable (all possible subsets).
from itersys.combinatorics import powerset
print(list(powerset([1, 2, 3])))
# [(), (1,), (2,), (3,), (1, 2), (1, 3), (2, 3), (1, 2, 3)]
cartesian_product(*iterables)
Compute the Cartesian product of multiple iterables.
from itersys.combinatorics import cartesian_product
print(list(cartesian_product([1, 2], [3, 4])))
# [(1, 3), (1, 4), (2, 3), (2, 4)]
Utilities (itersys.utils)
deep_get(d, key_path, default=None)
Get a value from a nested dictionary using a dot-separated key path or list of keys.
from itersys.utils import deep_get
d = {"a": {"b": {"c": 42}}}
print(deep_get(d, "a.b.c")) # 42
print(deep_get(d, ["a", "b", "c"])) # 42
print(deep_get(d, "a.b.d", default=0)) # 0
deep_set(d, key_path, value)
Set a value in a nested dictionary using a dot-separated key path or list of keys.
from itersys.utils import deep_set
d = {}
deep_set(d, "a.b.c", 42)
print(d) # {'a': {'b': {'c': 42}}}
deep_set(d, ["x", "y"], 10)
print(d) # {'a': {'b': {'c': 42}}, 'x': {'y': 10}}
CLI Usage
The itersys package includes a command-line interface for quick operations:
Chunk Command
itersys chunk --input '[1,2,3,4,5]' --size 2
# Output: [[1,2],[3,4],[5]]
Flatten Command
itersys flatten --input '[1,[2,3],[4,[5]]]'
# Output: [1,2,3,4,5]
Import Examples
You can import functions in multiple ways:
# Import from main package
import itersys
print(list(itersys.chunk([1, 2, 3, 4], 2)))
# Import from specific modules
from itersys.iteration import chunk, flatten
from itersys.algorithms import merge_sort
# Import everything
from itersys import chunk, flatten, merge_sort, powerset
Testing
Run tests with pytest:
pytest tests/
License
MIT License - see LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file itersys-0.1.0.tar.gz.
File metadata
- Download URL: itersys-0.1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16b6f8eec3d7dadf82b0c31a94ea689eba0b32d768a208fd1db35f64f84bced1
|
|
| MD5 |
1016192aca89a0801ab2fdbda35ac4bb
|
|
| BLAKE2b-256 |
275efd2e548b1416f51cd89ba524fcc04791dae9843fce8ce74f70b8bae6d1bf
|
Provenance
The following attestation bundles were made for itersys-0.1.0.tar.gz:
Publisher:
python-publish.yml on ZeroDevsLtd/itersys
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
itersys-0.1.0.tar.gz -
Subject digest:
16b6f8eec3d7dadf82b0c31a94ea689eba0b32d768a208fd1db35f64f84bced1 - Sigstore transparency entry: 1677416058
- Sigstore integration time:
-
Permalink:
ZeroDevsLtd/itersys@19c81fcceba33bafd0e031bfa6edc6aa66ea77b0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ZeroDevsLtd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@19c81fcceba33bafd0e031bfa6edc6aa66ea77b0 -
Trigger Event:
release
-
Statement type:
File details
Details for the file itersys-0.1.0-py3-none-any.whl.
File metadata
- Download URL: itersys-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d51e6a00fc4c2584fb0512402c94e43cf3fd200d9e5f6e50661af5ddfcb29443
|
|
| MD5 |
b10d104d647fc21f0b477863d61c8cdf
|
|
| BLAKE2b-256 |
5e96d85c18dab18bc2bf774394999407b0c0c503ad0e1325f89fb76a936da5bc
|
Provenance
The following attestation bundles were made for itersys-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on ZeroDevsLtd/itersys
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
itersys-0.1.0-py3-none-any.whl -
Subject digest:
d51e6a00fc4c2584fb0512402c94e43cf3fd200d9e5f6e50661af5ddfcb29443 - Sigstore transparency entry: 1677416133
- Sigstore integration time:
-
Permalink:
ZeroDevsLtd/itersys@19c81fcceba33bafd0e031bfa6edc6aa66ea77b0 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/ZeroDevsLtd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@19c81fcceba33bafd0e031bfa6edc6aa66ea77b0 -
Trigger Event:
release
-
Statement type: