Name | nr-stream JSON |
Version |
1.1.4
JSON |
| download |
home_page | |
Summary | |
upload_time | 2022-12-22 12:35:16 |
maintainer | |
docs_url | None |
author | Niklas Rosenstein |
requires_python | >=3.6,<4.0 |
license | MIT |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# 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__
```py
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__
```py
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__
```py
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__
```py
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
```
Raw data
{
"_id": null,
"home_page": "",
"name": "nr-stream",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.6,<4.0",
"maintainer_email": "",
"keywords": "",
"author": "Niklas Rosenstein",
"author_email": "rosensteinniklas@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/71/2b/c10b220d8a0bddc3290887a88365aa27824177aa62b44ac90d6af2430728/nr_stream-1.1.4.tar.gz",
"platform": null,
"description": "# nr-stream\n\nThis package provides utilities for writing functional-style code in Python. The package originally contained only\nthe `Stream` class, hence the name, but since we've adopted the terminology for letting us *streamline* large chunks\nof our code.\n\n## API\n\n### Optional objects\n\nRepresents an optional value, i.e. one that either has a valid value or is `None`. The class is useful to\nchain modifications and have them execute based on whether a value is available or not.\n\n__Example__\n\n```py\nimport os\nfrom nr.stream import Optional\n\nopt = Optional(os.getenv(\"SOMEVAR\"))\nvalue = opt.or_else_get(lambda: do_something_else())\nvalue = opt.or_else_raise(lambda: Exception(\"SOMEVAR not set\"))\nopt = opt.map(lambda value: value + \" another value\")\nlen(opt.stream().count()) # 0 or 1\n```\n\n### Refreshable objects\n\nA Refreshable is a container for a value that can be updated and inform listeners. A chained operations on a\nrefreshable will be replayed if the parent refreshable is updated. This is eager evaluation, not lazy evaluation\nand allows performant calls to `.get()` without going through a lazy chain of operations each time.\n\nUnlike `Optional` or `Stream`, the `Refreshable` knows no \"empty\" state.\n\nThis class is often useful to pass configuration data around in your application. It allows making modifications\nto the configuration and have it automatically propagate throughout the application.\n\n__Example__\n\n```py\nfrom nr.stream import Refreshable\n\nroot = Refreshable[int | None](None)\nchild = root.map(lambda v: 42 if v is None else v)\n\nprint(root.get()) # None\nprint(child.get()) # 42\nroot.update(10)\nprint(root.get()) # 10\nprint(child.get()) # 10\n```\n\n### Stream objects\n\nThe Stream class wraps an iterable and allows you to build a chain of modifiers on top of it. This often\ngreatly simplifies consecutive operations on an iterable object and its items.\n\n__Example__\n\n```py\nfrom nr.stream import Stream\n\nvalues = [3, 6, 4, 7, 1, 2, 5]\nassert list(Stream(values).chunks(values, 3, fill=0).map(sum)) == [13, 10, 5]\n```\n\n> __Important__: Stream objects always immediately convert the object passed to an iterator. This means\n> that you cannot branch stream objects, as both forks will share the same initial iterator.\n\n### Supplier objects\n\nThe Supplier class allows you to lazily evaluate the retrieval of a value, as well as chain modifications\non top of it and even trace the lineage of these modifications. It provides convenience methods such as\n`.map()`, `.once()`, `.get_or_raise()`. Unlike an `Optional`, a supplier will treat `None` as a valid value\nand instead separately track the state of \"no value\".\n\nTrying to read a value from an empty supplier raises a `Supplier.Empty` exception. Note that suppliers _always_\nevaluate lazily, unlike `Optional`.\n\n__Example__\n\n```py\nfrom nr.stream import Supplier\n\nsup = Supplier.of(42)\nsup = sup.map(lambda value: print(value))\nassert sup.get() == None # prints: 42\nassert sup.get() == None # prints: 42\n\nSupplier.void().get() # raises Supplier.Empty\n```\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "",
"version": "1.1.4",
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"md5": "153c958cf88db472e9397f1cdf72e492",
"sha256": "218ffbe82d8dad970697e9ede0748a0ff21d5b58333f51a8eccff51dc191bd1c"
},
"downloads": -1,
"filename": "nr_stream-1.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "153c958cf88db472e9397f1cdf72e492",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.6,<4.0",
"size": 10436,
"upload_time": "2022-12-22T12:35:15",
"upload_time_iso_8601": "2022-12-22T12:35:15.229700Z",
"url": "https://files.pythonhosted.org/packages/07/43/0e6e1ba802be3c56c56fc92b2f714fa761186481d35cef5556d2fff88f1a/nr_stream-1.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"md5": "2d481a32bf5e037021c0d361748a9004",
"sha256": "8890ac8db35c07566f94dd1564a84a416d51d6bcddabad97e871a90cb6e4c5c9"
},
"downloads": -1,
"filename": "nr_stream-1.1.4.tar.gz",
"has_sig": false,
"md5_digest": "2d481a32bf5e037021c0d361748a9004",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.6,<4.0",
"size": 10525,
"upload_time": "2022-12-22T12:35:16",
"upload_time_iso_8601": "2022-12-22T12:35:16.883008Z",
"url": "https://files.pythonhosted.org/packages/71/2b/c10b220d8a0bddc3290887a88365aa27824177aa62b44ac90d6af2430728/nr_stream-1.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2022-12-22 12:35:16",
"github": false,
"gitlab": false,
"bitbucket": false,
"lcname": "nr-stream"
}