A helper library to manage collections more easily
Project description
PyStream - A library for managing collections more conveniently
Inspired by other language's features (e.g. Java's streaming API, or JavaScript functional traits on arrays), this library helps you interact with collections or iterable objects, by easily chaining operations, and collecting the results at the end (thus, having lazy-evaluation).
Some basic examples:
from pystream.stream import Stream
Stream(2, 3, 5, 7, 11).skip(2).collect() # [5, 7, 11]
Stream([1, 2, 3]).map(lambda x: x + 1).filter(lambda x: x > 2).collect() # [3, 4]
You can also use the .reduce()
function to obtain a final result based on a provided transformation function:
>>> stream = Stream(("paris", "london", "stockholm")).map(str.title)
>>> stream.reduce(lambda w1, w2: f"{w1}, {w2}")
"Paris, London, Stockholm"
It's also possible to use a specific object to collect the results into (by default is a list). For example, if the stream consists of key/value pairs, you can collect them into a dictionary:
>>> Stream(("one", 1), ("two", 2), ("forty two", 42))
>>> stream.collect(dict)
{"one": 1, "two": 2, "forty two": 42}
Asynchronous Code
This library supports working with coroutines and asynchronous iterators as well. Working with the built-in map()
, and
filter()
functions is great, and also the niceties of the itertools
module, but there's no counterpart of these
capabilities for asynchronous code.
Instead, this more compact (and functional-like) object is provided, which can work like this:
class Locker(NamedTuple):
name: str
size: str
is_available: bool
async def _get_db_records() -> AsyncGenerator:
yield Locker("park street 1", "L", False)
yield Locker("Union street", "S", True)
yield Locker("Main Square 12", "M", False)
yield Locker("Central Station", "M", True)
yield Locker("Central Station2", "L", True)
yield Locker("Central Station3", "L", True)
>>> count = (
await AsyncStream(_get_db_records())
.filter(lambda locker: locker.is_available)
.map(lambda locker: locker.size)
.collect(Counter)
)
{"S": 1, "M": 1, "L": 2}
Motivation
Missing the itertools
-like capabilities is one of the most annoying things,
when working with asynchronous code. In addition, adding another interface for
the programmer that allows chaining data structures, similar to how Unix
pipelines work, enables programmers to think more clearly about their programs.
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
File details
Details for the file pystream_collections-0.2.1.tar.gz
.
File metadata
- Download URL: pystream_collections-0.2.1.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
4ef00f7b5ef8e8bb06efadd73b98e8af01a95ff4fcca4bafdc203bbfd10d6eae
|
|
MD5 |
3dbaff7a395629937f786dc8d62b6ce7
|
|
BLAKE2b-256 |
e105e3c4b29da713af4c2895853d2d5a5d67280c57d9afd746a9f21630d01216
|
Provenance
The following attestation bundles were made for pystream_collections-0.2.1.tar.gz
:
Publisher:
publish.yml
on rmariano/pystream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
pystream_collections-0.2.1.tar.gz
-
Subject digest:
4ef00f7b5ef8e8bb06efadd73b98e8af01a95ff4fcca4bafdc203bbfd10d6eae
- Sigstore transparency entry: 149492360
- Sigstore integration time:
-
Permalink:
rmariano/pystream@f7524fc6d034cdb24dad21f675e5ff3fa9493f92
-
Branch / Tag:
refs/heads/main
- Owner: https://github.com/rmariano
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish.yml@f7524fc6d034cdb24dad21f675e5ff3fa9493f92
-
Trigger Event:
push
-
Statement type:
File details
Details for the file pystream_collections-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: pystream_collections-0.2.1-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
9b85ea8a5c859521eb9b5c672f7d796a9b92481b6a6827375d856cc87f0ab678
|
|
MD5 |
c7e94da4981e163ff7612a68e37da861
|
|
BLAKE2b-256 |
d0577701cb7e71e500f488189a5c275639c5fce3f21e7fd124a1784fcc73ce02
|
Provenance
The following attestation bundles were made for pystream_collections-0.2.1-py3-none-any.whl
:
Publisher:
publish.yml
on rmariano/pystream
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
pystream_collections-0.2.1-py3-none-any.whl
-
Subject digest:
9b85ea8a5c859521eb9b5c672f7d796a9b92481b6a6827375d856cc87f0ab678
- Sigstore transparency entry: 149492365
- Sigstore integration time:
-
Permalink:
rmariano/pystream@f7524fc6d034cdb24dad21f675e5ff3fa9493f92
-
Branch / Tag:
refs/heads/main
- Owner: https://github.com/rmariano
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish.yml@f7524fc6d034cdb24dad21f675e5ff3fa9493f92
-
Trigger Event:
push
-
Statement type: