No project description provided
Project description
nr-stream
This package provides utilities for writing functional-style code in Python. The package originally contained only
the Stream
class, hence the name, but since we've adopted the terminology for letting us streamline large chunks
of our code.
API
Optional objects
Represents an optional value, i.e. one that either has a valid value or is None
. The class is useful to
chain modifications and have them execute based on whether a value is available or not.
Example
import os
from nr.stream import Optional
opt = Optional(os.getenv("SOMEVAR"))
value = opt.or_else_get(lambda: do_something_else())
value = opt.or_else_raise(lambda: Exception("SOMEVAR not set"))
opt = opt.map(lambda value: value + " another value")
len(opt.stream().count()) # 0 or 1
Refreshable objects
A Refreshable is a container for a value that can be updated and inform listeners. A chained operations on a
refreshable will be replayed if the parent refreshable is updated. This is eager evaluation, not lazy evaluation
and allows performant calls to .get()
without going through a lazy chain of operations each time.
Unlike Optional
or Stream
, the Refreshable
knows no "empty" state.
This class is often useful to pass configuration data around in your application. It allows making modifications to the configuration and have it automatically propagate throughout the application.
Example
from nr.stream import Refreshable
root = Refreshable[int | None](None)
child = root.map(lambda v: 42 if v is None else v)
print(root.get()) # None
print(child.get()) # 42
root.update(10)
print(root.get()) # 10
print(child.get()) # 10
Stream objects
The Stream class wraps an iterable and allows you to build a chain of modifiers on top of it. This often greatly simplifies consecutive operations on an iterable object and its items.
Example
from nr.stream import Stream
values = [3, 6, 4, 7, 1, 2, 5]
assert list(Stream(values).chunks(values, 3, fill=0).map(sum)) == [13, 10, 5]
Important: Stream objects always immediately convert the object passed to an iterator. This means that you cannot branch stream objects, as both forks will share the same initial iterator.
Supplier objects
The Supplier class allows you to lazily evaluate the retrieval of a value, as well as chain modifications
on top of it and even trace the lineage of these modifications. It provides convenience methods such as
.map()
, .once()
, .get_or_raise()
. Unlike an Optional
, a supplier will treat None
as a valid value
and instead separately track the state of "no value".
Trying to read a value from an empty supplier raises a Supplier.Empty
exception. Note that suppliers always
evaluate lazily, unlike Optional
.
Example
from nr.stream import Supplier
sup = Supplier.of(42)
sup = sup.map(lambda value: print(value))
assert sup.get() == None # prints: 42
assert sup.get() == None # prints: 42
Supplier.void().get() # raises Supplier.Empty
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 nr_stream-1.1.5.tar.gz
.
File metadata
- Download URL: nr_stream-1.1.5.tar.gz
- Upload date:
- Size: 10.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb0216c6bfc61a46d4568dba3b588502c610ec8ddef4ac98f3932a2bd7264f65 |
|
MD5 | 5e0847a3e8341c173b48e481b16035e1 |
|
BLAKE2b-256 | b737e4d36d852c441233c306c5fbd98147685dce3ac9b0a8bbf4a587d0ea29ea |
File details
Details for the file nr_stream-1.1.5-py3-none-any.whl
.
File metadata
- Download URL: nr_stream-1.1.5-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.28.2 requests-toolbelt/0.10.1 urllib3/1.26.14 tqdm/4.64.1 importlib-metadata/6.0.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 47e12150b331ad2cb729cfd9d2abd281c9949809729ba461c6aa87dd9927b2d4 |
|
MD5 | cfe882d17271ac87ee7c6f4d2001716a |
|
BLAKE2b-256 | 1de1f93485fe09aa36c0e1a3b76363efa1791241f7f863a010f725c95e8a74fe |