Skip to main content

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.

Release files for pystream-collections 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pystream-collections 0.2.1
File Size Uploaded
pystream_collections-0.2.1.tar.gz 5.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pystream-collections 0.2.1
File Interpreter ABI Platform
pystream_collections-0.2.1-py3-none-any.whl Python 3 none any Details

Total release size: 12.5 kB

Release files / pystream_collections-0.2.1.tar.gz

Download URL pystream_collections-0.2.1.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
4ef00f7b5ef8e8bb06efadd73b98e8af01a95ff4fcca4bafdc203bbfd10d6eae
BLAKE2b-256 checksum
How to use checksums
e105e3c4b29da713af4c2895853d2d5a5d67280c57d9afd746a9f21630d01216
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

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 Nov 18, 2024.

Transparency log

Release files / pystream_collections-0.2.1-py3-none-any.whl

Download URL pystream_collections-0.2.1-py3-none-any.whl
Size 7.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9b85ea8a5c859521eb9b5c672f7d796a9b92481b6a6827375d856cc87f0ab678
BLAKE2b-256 checksum
How to use checksums
d0577701cb7e71e500f488189a5c275639c5fce3f21e7fd124a1784fcc73ce02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/5.1.1 CPython/3.12.7

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 Nov 18, 2024.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.1 This release

2 release files

0.2.0

2 release files

0.1.25

2 release files

0.1.24

2 release files

0.1.23

2 release files

0.1.22

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page