| Name | pystream-collections JSON |
| Version |
0.2.0
JSON |
| download |
| home_page | None |
| Summary | A helper library to manage collections more easily |
| upload_time | 2024-10-21 21:08:27 |
| maintainer | None |
| docs_url | None |
| author | Mariano Anaya |
| requires_python | <4.0,>=3.12 |
| license | MIT |
| keywords |
|
| VCS |
|
| bugtrack_url |
|
| requirements |
No requirements were recorded.
|
| Travis-CI |
No Travis.
|
| coveralls test coverage |
No coveralls.
|
# 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:
```python
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:
```python
>>> 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:
```python
>>> 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:
```python
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.
Raw data
{
"_id": null,
"home_page": null,
"name": "pystream-collections",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.12",
"maintainer_email": null,
"keywords": null,
"author": "Mariano Anaya",
"author_email": "mariano_anaya@yahoo.com.ar",
"download_url": "https://files.pythonhosted.org/packages/2f/3d/79f3e5e817faa7a37985948f19c180ddd2f8c22167902f178ef7a0f187ba/pystream_collections-0.2.0.tar.gz",
"platform": null,
"description": "# PyStream - A library for managing collections more conveniently\n\nInspired by other language's features (e.g. Java's streaming API, or JavaScript functional traits on arrays),\nthis library helps you interact with collections or iterable objects, by easily chaining operations,\nand collecting the results at the end (thus, having lazy-evaluation).\n\nSome basic examples:\n\n```python\nfrom pystream.stream import Stream\n\nStream(2, 3, 5, 7, 11).skip(2).collect() # [5, 7, 11]\nStream([1, 2, 3]).map(lambda x: x + 1).filter(lambda x: x > 2).collect() # [3, 4]\n```\n\nYou can also use the `.reduce()` function to obtain a final result based on a provided transformation function:\n\n```python\n>>> stream = Stream((\"paris\", \"london\", \"stockholm\")).map(str.title)\n>>> stream.reduce(lambda w1, w2: f\"{w1}, {w2}\")\n\"Paris, London, Stockholm\"\n```\n\nIt's also possible to use a specific object to collect the results into (by default is a list). For example, if the\nstream consists of key/value pairs, you can collect them into a dictionary:\n\n```python\n>>> Stream((\"one\", 1), (\"two\", 2), (\"forty two\", 42))\n>>> stream.collect(dict)\n{\"one\": 1, \"two\": 2, \"forty two\": 42}\n```\n\n## Asynchronous Code\nThis library supports working with coroutines and asynchronous iterators as well. Working with the built-in `map()`, and\n`filter()` functions is great, and also the niceties of the `itertools` module, but there's no counterpart of these\ncapabilities for asynchronous code.\n\nInstead, this more compact (and functional-like) object is provided, which can work like this:\n\n```python\nclass Locker(NamedTuple):\n name: str\n size: str\n is_available: bool\n\nasync def _get_db_records() -> AsyncGenerator:\n yield Locker(\"park street 1\", \"L\", False)\n yield Locker(\"Union street\", \"S\", True)\n yield Locker(\"Main Square 12\", \"M\", False)\n yield Locker(\"Central Station\", \"M\", True)\n yield Locker(\"Central Station2\", \"L\", True)\n yield Locker(\"Central Station3\", \"L\", True)\n\n>>> count = (\n await AsyncStream(_get_db_records())\n .filter(lambda locker: locker.is_available)\n .map(lambda locker: locker.size)\n .collect(Counter)\n)\n{\"S\": 1, \"M\": 1, \"L\": 2}\n```\n\n## Motivation\nMissing the `itertools`-like capabilities is one of the most annoying things,\nwhen working with asynchronous code. In addition, adding another interface for\nthe programmer that allows chaining data structures, similar to how Unix\npipelines work, enables programmers to think more clearly about their programs.\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A helper library to manage collections more easily",
"version": "0.2.0",
"project_urls": null,
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c4a076773a4122aa817faaf12a64c388e49b1a97245ea61da3a2dded94f029d2",
"md5": "eb4882be86afcffebc3c13cb22c7c96e",
"sha256": "2d6cb77641e6146d51c1539dfe98000d2ab49f92ebd8caf58a24a75ac044e7e8"
},
"downloads": -1,
"filename": "pystream_collections-0.2.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "eb4882be86afcffebc3c13cb22c7c96e",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.12",
"size": 7163,
"upload_time": "2024-10-21T21:08:26",
"upload_time_iso_8601": "2024-10-21T21:08:26.159511Z",
"url": "https://files.pythonhosted.org/packages/c4/a0/76773a4122aa817faaf12a64c388e49b1a97245ea61da3a2dded94f029d2/pystream_collections-0.2.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "2f3d79f3e5e817faa7a37985948f19c180ddd2f8c22167902f178ef7a0f187ba",
"md5": "204f72b3a21ede6d7c99f3d0df940649",
"sha256": "db7f9a86f0d69b1003285b6a9f8b750a31d52b556ca9ac03c83b8cd3e1f5e990"
},
"downloads": -1,
"filename": "pystream_collections-0.2.0.tar.gz",
"has_sig": false,
"md5_digest": "204f72b3a21ede6d7c99f3d0df940649",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.12",
"size": 5389,
"upload_time": "2024-10-21T21:08:27",
"upload_time_iso_8601": "2024-10-21T21:08:27.270863Z",
"url": "https://files.pythonhosted.org/packages/2f/3d/79f3e5e817faa7a37985948f19c180ddd2f8c22167902f178ef7a0f187ba/pystream_collections-0.2.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-21 21:08:27",
"github": false,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"lcname": "pystream-collections"
}