Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pystream_collections-0.2.1.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

pystream_collections-0.2.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

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

Hashes for pystream_collections-0.2.1.tar.gz
Algorithm Hash digest
SHA256 4ef00f7b5ef8e8bb06efadd73b98e8af01a95ff4fcca4bafdc203bbfd10d6eae
MD5 3dbaff7a395629937f786dc8d62b6ce7
BLAKE2b-256 e105e3c4b29da713af4c2895853d2d5a5d67280c57d9afd746a9f21630d01216

See more details on using hashes here.

Provenance

The following attestation bundles were made for pystream_collections-0.2.1.tar.gz:

Publisher: publish.yml on rmariano/pystream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pystream_collections-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for pystream_collections-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9b85ea8a5c859521eb9b5c672f7d796a9b92481b6a6827375d856cc87f0ab678
MD5 c7e94da4981e163ff7612a68e37da861
BLAKE2b-256 d0577701cb7e71e500f488189a5c275639c5fce3f21e7fd124a1784fcc73ce02

See more details on using hashes here.

Provenance

The following attestation bundles were made for pystream_collections-0.2.1-py3-none-any.whl:

Publisher: publish.yml on rmariano/pystream

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page