nr-stream

1.1.5 · active · verified Thu Apr 16

The `nr-stream` package provides utilities for writing functional-style code in Python, offering `Stream`, `Optional`, and `Refreshable` classes. The `Stream` class wraps iterables to enable chained modifiers, simplifying common operations. `Optional` represents a value that might be `None`, allowing for safe chaining, while `Refreshable` acts as a container for values that can be updated, propagating changes to listeners. The current version is 1.1.5, released on February 14, 2023, with an active but irregular release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the core `Stream` class for chained iterable operations, as well as basic usage of the `Optional` and `Refreshable` utilities. The `Stream` example shows how to chunk and transform data, while `Optional` handles potentially absent values, and `Refreshable` illustrates reactive updates.

from nr.stream import Stream

values = [3, 6, 4, 7, 1, 2, 5]

# Create a Stream, chunk it, map each chunk to its sum, and collect the results
processed_stream = Stream(values).chunks(3, fill=0).map(sum)

print(list(processed_stream))
# Expected output: [13, 10, 5]

from nr.stream import Optional
import os

# Example with Optional
opt_var = Optional(os.getenv("NON_EXISTENT_VAR"))
value_or_default = opt_var.or_else_get(lambda: "default_value")
print(f"Value or default: {value_or_default}")

from nr.stream import Refreshable

# Example with Refreshable
root_refreshable = Refreshable[int | None](None)
child_refreshable = root_refreshable.map(lambda v: 42 if v is None else v)

print(f"Initial root: {root_refreshable.get()}, child: {child_refreshable.get()}")
root_refreshable.update(10)
print(f"Updated root: {root_refreshable.get()}, child: {child_refreshable.get()}")

view raw JSON →