Stream4Py: Functional, chainable streams for Python with lazy evaluation
Project description
Stream4Py
A Python library inspired by Java Streams, Haskell lists, and Linux pipes, providing a powerful, lazy-evaluated Stream class for functional-style data manipulation. Stream4Py makes it easy to work with iterables, files, subprocess output, and general data pipelines.
Features
- Lazy and eager evaluation methods.
- Chainable operations like
map,filter,flat_map,unique,enumerate,flatten,sections. - File parsing and subprocess piping with
from_ioandpipe. - Collection helpers like
to_list,to_dict,to_set, andcache. - Built-in itertools utilities (
islice,zip_longest,accumulate, etc.). - Inspired by Java Streams, Haskell functional programming, and Python itertools.
Installation
Install via pip:
pip install stream4py
Quick Start
from stream4py import Stream
# Create a stream
s = Stream([1, 2, 3, 4, 5])
# Lazy operations
result = (
s.filter(lambda x: x % 2 == 0)
.map(lambda x: x * 10)
.unique()
)
# Convert to list (triggers evaluation)
print(result.to_list()) # [20, 40]
# Stream lines from a file
lines = Stream.from_io(open("file.txt"))
lines.filter(lambda x: "error" in x).for_each(print)
# Subprocess streaming
Stream.subprocess_run(("seq", "100")).pipe(("grep", "1")).for_each(print)
Quick Reference
| Method | Type | Description | Example |
|---|---|---|---|
map(func) |
Lazy | Apply a function to each item | Stream([1,2,3]).map(lambda x: x*2) |
filter(predicate) |
Lazy | Keep items satisfying a predicate | Stream([1,2,3]).filter(lambda x: x>1) |
filterfalse(predicate) |
Lazy | Keep items for which predicate is False | Stream([1,2,3]).filterfalse(lambda x: x>1) |
flat_map(func) |
Lazy | Map then flatten iterables | Stream([1,2]).flat_map(lambda x: (x,x*10)) |
flatten() |
Lazy | Flatten nested iterables | Stream([[1,2],[3]]).flatten() |
unique(key=None) |
Lazy | Keep only unique items | Stream([1,2,2]).unique() |
type_is(cls) |
Lazy | Keep items of a specific type | Stream([1,'a']).type_is(int) |
enumerate(start=0) |
Lazy | Enumerate items | Stream(['a','b']).enumerate(1) |
peek(func) |
Lazy | Apply function without changing items | Stream([1,2]).peek(print) |
islice(start, stop, step) |
Lazy | Slice like itertools.islice |
Stream([1,2,3]).islice(1,3) |
batched(size) |
Lazy | Yield items in batches | Stream(range(5)).batched(2) |
drop(n) |
Lazy | Drop first n items |
Stream([1,2,3]).drop(1) |
take(n) |
Lazy | Take first n items |
Stream([1,2,3]).take(2) |
dropwhile(predicate) |
Lazy | Drop items while predicate is true | Stream([1,2,3]).dropwhile(lambda x: x<2) |
takewhile(predicate) |
Lazy | Take items while predicate is true | Stream([1,2,3]).takewhile(lambda x: x<3) |
reverse() |
Lazy | Reverse the items | Stream([1,2,3]).reverse() |
zip(*iterables) |
Lazy | Zip with other iterables | Stream([1,2]).zip(['a','b']) |
zip_longest(*iterables) |
Lazy | Zip with padding | Stream([1]).zip_longest([2,3]) |
accumulate(func=None, initial=None) |
Lazy | Cumulative sums or function | Stream([1,2,3]).accumulate() |
subprocess_run(command) |
Lazy | Run a subprocess and stream output | Stream.subprocess_run(('ls',)) |
pipe(command) |
Lazy | Pipe stream to subprocess | Stream(['foo']).pipe(('grep','f')) |
sum(start=0) |
Eager | Sum all items | Stream([1,2,3]).sum() |
min(key=None, default=None) |
Eager | Minimum value | Stream([1,2,3]).min() |
max(key=None, default=None) |
Eager | Maximum value | Stream([1,2,3]).max() |
sorted(key=None, reverse=False) |
Eager | Sort items | Stream([3,1,2]).sorted() |
first(default=None) |
Eager | First item | Stream([1,2]).first() |
find(func) |
Eager | Find first item matching function | Stream([1,2,3]).find(lambda x: x>1) |
group_by(key) |
Eager | Group items by key | Stream([1,2,3,4]).group_by(lambda x: x%2) |
for_each(func) |
Eager | Apply function to all items | Stream([1,2]).for_each(print) |
cache() |
Eager | Cache stream items | Stream(range(3)).cache() |
to_list() |
Eager | Collect as list | Stream([1,2]).to_list() |
to_tuple() |
Eager | Collect as tuple | Stream([1,2]).to_tuple() |
to_set() |
Eager | Collect as set | Stream([1,2]).to_set() |
to_dict() |
Eager | Collect as dict (from tuples) | Stream([(1,'a')]).to_dict() |
collect(func) |
Eager | Apply function to iterable | Stream([1,2]).collect(sum) |
from_io(io) |
Lazy | Stream lines from file or binary IO | Stream.from_io(open('file.txt')) |
sections(predicate) |
Lazy | Split into sections based on predicate | Stream([1,1,2]).sections(lambda x:x==2) |
range(start, stop, step=1) |
Lazy | Stream over a range | Stream.range(1,5) |
Contributing
Contributions are welcome! Please open an issue or pull request with improvements or bug fixes.
License
stream4py is distributed under the terms of 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
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 stream4py-0.1.1.tar.gz.
File metadata
- Download URL: stream4py-0.1.1.tar.gz
- Upload date:
- Size: 16.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6a6a5cb2f8c65db29675d6fd8c9b9f2c34222b8270ded3f83f0c4c85870a94a6
|
|
| MD5 |
ba7e009d104bbf6e7bcfb85ad804037b
|
|
| BLAKE2b-256 |
fef30564942fda94eac152bf0c2cf4f5ac5f45864530291b98de305f48ca1d1d
|
Provenance
The following attestation bundles were made for stream4py-0.1.1.tar.gz:
Publisher:
main.yaml on FlavioAmurrioCS/stream4py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stream4py-0.1.1.tar.gz -
Subject digest:
6a6a5cb2f8c65db29675d6fd8c9b9f2c34222b8270ded3f83f0c4c85870a94a6 - Sigstore transparency entry: 543070762
- Sigstore integration time:
-
Permalink:
FlavioAmurrioCS/stream4py@8eb19dfb95be6bfc5205e70a471e27aa4ca3e60f -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/FlavioAmurrioCS
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yaml@8eb19dfb95be6bfc5205e70a471e27aa4ca3e60f -
Trigger Event:
push
-
Statement type:
File details
Details for the file stream4py-0.1.1-py3-none-any.whl.
File metadata
- Download URL: stream4py-0.1.1-py3-none-any.whl
- Upload date:
- Size: 12.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e318b5500740fc1264f27b6308e55e60730d05b5410e4bd22d168154809a2e55
|
|
| MD5 |
0d96cd65e6e1b1b4a9c097a9df56e47f
|
|
| BLAKE2b-256 |
283ae2b765446d02dea23ffafe2b4f4ca87803e49b44eab5f95f31d12af3b5ff
|
Provenance
The following attestation bundles were made for stream4py-0.1.1-py3-none-any.whl:
Publisher:
main.yaml on FlavioAmurrioCS/stream4py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
stream4py-0.1.1-py3-none-any.whl -
Subject digest:
e318b5500740fc1264f27b6308e55e60730d05b5410e4bd22d168154809a2e55 - Sigstore transparency entry: 543070764
- Sigstore integration time:
-
Permalink:
FlavioAmurrioCS/stream4py@8eb19dfb95be6bfc5205e70a471e27aa4ca3e60f -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/FlavioAmurrioCS
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yaml@8eb19dfb95be6bfc5205e70a471e27aa4ca3e60f -
Trigger Event:
push
-
Statement type: