Name | cached-iterators JSON |
Version |
1.0.3
JSON |
| download |
home_page | https://github.com/ulbwa/cached-iterators |
Summary | A Python library providing cacheable iterator wrappers for both synchronous and asynchronous iterators. |
upload_time | 2024-07-09 18:20:21 |
maintainer | None |
docs_url | None |
author | ульба |
requires_python | <4.0,>=3.10 |
license | None |
keywords |
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# cached-iterators
A Python library providing cacheable iterator wrappers for both synchronous and asynchronous iterators. This allows
iterators to be reused multiple times with cached values, reducing the need for recomputations.
<!-- TOC -->
* [Features](#features)
* [Installation](#installation)
* [Usage](#usage)
* [Synchronous Iterator Wrapper](#synchronous-iterator-wrapper)
* [Output](#output)
* [Asynchronous Iterator Wrapper](#asynchronous-iterator-wrapper)
* [Output](#output-1)
<!-- TOC -->
## Features
- **Reusable Iterators**: Wrap synchronous and asynchronous iterators to allow them to be iterated multiple times
with cached results.
- **Lazy Loading**: Only fetch and cache results as needed, avoiding unnecessary computations or I/O operations.
- **Ease of Use**: Simple API with decorators for easy integration.
- **Improved Concurrency**: Efficiently handle concurrent tasks that need to use the same iterator.
## Installation
You can install this package via pip:
```shell
python3 -m pip install cached-iterators
```
## Usage
### Synchronous Iterator Wrapper
You can use the synchronous iterator wrapper in two ways: by decorating a function that returns an iterator or by directly wrapping an iterator instance.
1. **Decorating a function**:
```python
from cached_iterators import cacheable_iterator
from typing import Iterator
@cacheable_iterator
def generate_numbers() -> Iterator[int]:
for i in range(5):
print(f"Generating {i}")
yield i
# Create a cacheable iterator using the decorated function
cached_iter = generate_numbers()
# First iteration (values will be generated and cached)
print("First iteration:")
for num in cached_iter:
print(num)
# Second iteration (values will be retrieved from cache)
print("Second iteration:")
for num in cached_iter:
print(num)
```
2. **Wrapping an existing iterator**:
```python
from cached_iterators import CacheableIteratorWrapper
from typing import Iterator
def generate_numbers() -> Iterator[int]:
for i in range(5):
print(f"Generating {i}")
yield i
# Create a cacheable iterator by wrapping an existing iterator instance
iterator = generate_numbers()
cached_iter = CacheableIteratorWrapper(iterator)
# First iteration (values will be generated and cached)
print("First iteration:")
for num in cached_iter:
print(num)
# Second iteration (values will be retrieved from cache)
print("Second iteration:")
for num in cached_iter:
print(num)
```
#### Output
```
First iteration:
Generating 0
0
Generating 1
1
Generating 2
2
Generating 3
3
Generating 4
4
Second iteration:
0
1
2
3
4
```
### Asynchronous Iterator Wrapper
Similarly, the asynchronous iterator wrapper can be used by decorating a function that returns an asynchronous
iterator or by directly wrapping an asynchronous iterator instance.
1. **Decorating a function**:
```python
import asyncio
from cached_iterators import cacheable_async_iterator
from typing import AsyncIterator
@cacheable_async_iterator
async def async_generate_numbers() -> AsyncIterator[int]:
for i in range(5):
print(f"Generating {i}")
yield i
await asyncio.sleep(0) # Simulate async work
# Create a cacheable async iterator using the decorated function
cached_iter = async_generate_numbers()
async def main():
# First iteration (values will be generated and cached)
print("First iteration:")
async for num in cached_iter:
print(num)
# Second iteration (values will be retrieved from cache)
print("Second iteration:")
async for num in cached_iter:
print(num)
# Run the example
asyncio.run(main())
```
2. **Wrapping an existing iterator**:
```python
import asyncio
from cached_iterators import CacheableAsyncIteratorWrapper
from typing import AsyncIterator
async def async_generate_numbers() -> AsyncIterator[int]:
for i in range(5):
print(f"Generating {i}")
yield i
await asyncio.sleep(0) # Simulate async work
# Create a cacheable async iterator by wrapping an existing async iterator instance
iterator = async_generate_numbers()
cached_iter = CacheableAsyncIteratorWrapper(iterator)
async def main():
# First iteration (values will be generated and cached)
print("First iteration:")
async for num in cached_iter:
print(num)
# Second iteration (values will be retrieved from cache)
print("Second iteration:")
async for num in cached_iter:
print(num)
# Run the example
asyncio.run(main())
```
#### Output
```
First iteration:
Generating 0
0
Generating 1
1
Generating 2
2
Generating 3
3
Generating 4
4
Second iteration:
0
1
2
3
4
```
Raw data
{
"_id": null,
"home_page": "https://github.com/ulbwa/cached-iterators",
"name": "cached-iterators",
"maintainer": null,
"docs_url": null,
"requires_python": "<4.0,>=3.10",
"maintainer_email": null,
"keywords": null,
"author": "\u0443\u043b\u044c\u0431\u0430",
"author_email": "ulbwa@icloud.com",
"download_url": "https://files.pythonhosted.org/packages/16/c2/9913e76ab6f31ee26bec6954018af8935e9e4a785feb78d8a0d12ab699a2/cached_iterators-1.0.3.tar.gz",
"platform": null,
"description": "# cached-iterators\n\nA Python library providing cacheable iterator wrappers for both synchronous and asynchronous iterators. This allows \niterators to be reused multiple times with cached values, reducing the need for recomputations.\n\n<!-- TOC -->\n* [Features](#features)\n* [Installation](#installation)\n* [Usage](#usage)\n * [Synchronous Iterator Wrapper](#synchronous-iterator-wrapper)\n * [Output](#output)\n * [Asynchronous Iterator Wrapper](#asynchronous-iterator-wrapper)\n * [Output](#output-1)\n<!-- TOC -->\n\n## Features\n\n- **Reusable Iterators**: Wrap synchronous and asynchronous iterators to allow them to be iterated multiple times \n with cached results.\n- **Lazy Loading**: Only fetch and cache results as needed, avoiding unnecessary computations or I/O operations.\n- **Ease of Use**: Simple API with decorators for easy integration.\n- **Improved Concurrency**: Efficiently handle concurrent tasks that need to use the same iterator.\n\n## Installation\n\nYou can install this package via pip:\n\n```shell\npython3 -m pip install cached-iterators\n```\n\n## Usage\n\n### Synchronous Iterator Wrapper\n\nYou can use the synchronous iterator wrapper in two ways: by decorating a function that returns an iterator or by directly wrapping an iterator instance.\n\n1. **Decorating a function**:\n\n```python\nfrom cached_iterators import cacheable_iterator\nfrom typing import Iterator\n\n\n@cacheable_iterator\ndef generate_numbers() -> Iterator[int]:\n for i in range(5):\n print(f\"Generating {i}\")\n yield i\n\n\n# Create a cacheable iterator using the decorated function\ncached_iter = generate_numbers()\n\n# First iteration (values will be generated and cached)\nprint(\"First iteration:\")\nfor num in cached_iter:\n print(num)\n\n# Second iteration (values will be retrieved from cache)\nprint(\"Second iteration:\")\nfor num in cached_iter:\n print(num)\n```\n\n2. **Wrapping an existing iterator**:\n\n```python\nfrom cached_iterators import CacheableIteratorWrapper\nfrom typing import Iterator\n\n\ndef generate_numbers() -> Iterator[int]:\n for i in range(5):\n print(f\"Generating {i}\")\n yield i\n\n\n# Create a cacheable iterator by wrapping an existing iterator instance\niterator = generate_numbers()\ncached_iter = CacheableIteratorWrapper(iterator)\n\n# First iteration (values will be generated and cached)\nprint(\"First iteration:\")\nfor num in cached_iter:\n print(num)\n\n# Second iteration (values will be retrieved from cache)\nprint(\"Second iteration:\")\nfor num in cached_iter:\n print(num)\n```\n\n#### Output\n\n```\nFirst iteration:\nGenerating 0\n0\nGenerating 1\n1\nGenerating 2\n2\nGenerating 3\n3\nGenerating 4\n4\nSecond iteration:\n0\n1\n2\n3\n4\n```\n\n### Asynchronous Iterator Wrapper\n\nSimilarly, the asynchronous iterator wrapper can be used by decorating a function that returns an asynchronous \niterator or by directly wrapping an asynchronous iterator instance.\n\n1. **Decorating a function**:\n\n```python\nimport asyncio\nfrom cached_iterators import cacheable_async_iterator\nfrom typing import AsyncIterator\n\n\n@cacheable_async_iterator\nasync def async_generate_numbers() -> AsyncIterator[int]:\n for i in range(5):\n print(f\"Generating {i}\")\n yield i\n await asyncio.sleep(0) # Simulate async work\n\n\n# Create a cacheable async iterator using the decorated function\ncached_iter = async_generate_numbers()\n\n\nasync def main():\n # First iteration (values will be generated and cached)\n print(\"First iteration:\")\n async for num in cached_iter:\n print(num)\n\n # Second iteration (values will be retrieved from cache)\n print(\"Second iteration:\")\n async for num in cached_iter:\n print(num)\n\n\n# Run the example\nasyncio.run(main())\n```\n\n2. **Wrapping an existing iterator**:\n\n```python\nimport asyncio\nfrom cached_iterators import CacheableAsyncIteratorWrapper\nfrom typing import AsyncIterator\n\n\nasync def async_generate_numbers() -> AsyncIterator[int]:\n for i in range(5):\n print(f\"Generating {i}\")\n yield i\n await asyncio.sleep(0) # Simulate async work\n\n\n# Create a cacheable async iterator by wrapping an existing async iterator instance\niterator = async_generate_numbers()\ncached_iter = CacheableAsyncIteratorWrapper(iterator)\n\n\nasync def main():\n # First iteration (values will be generated and cached)\n print(\"First iteration:\")\n async for num in cached_iter:\n print(num)\n\n # Second iteration (values will be retrieved from cache)\n print(\"Second iteration:\")\n async for num in cached_iter:\n print(num)\n\n\n# Run the example\nasyncio.run(main())\n```\n\n#### Output\n\n```\nFirst iteration:\nGenerating 0\n0\nGenerating 1\n1\nGenerating 2\n2\nGenerating 3\n3\nGenerating 4\n4\nSecond iteration:\n0\n1\n2\n3\n4\n```",
"bugtrack_url": null,
"license": null,
"summary": "A Python library providing cacheable iterator wrappers for both synchronous and asynchronous iterators.",
"version": "1.0.3",
"project_urls": {
"Homepage": "https://github.com/ulbwa/cached-iterators",
"Repository": "https://github.com/ulbwa/cached-iterators"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "fd39a112ec52858686d24d1d5de730a2c2f1969908d2ccf63828295c70792afc",
"md5": "16cbd09d6adcdec1fba6a72070f7ec3d",
"sha256": "ab9b27cd9a763b8b5d844324692c544bcb1dd0bc7b1656f13db93aed9124c12b"
},
"downloads": -1,
"filename": "cached_iterators-1.0.3-py3-none-any.whl",
"has_sig": false,
"md5_digest": "16cbd09d6adcdec1fba6a72070f7ec3d",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": "<4.0,>=3.10",
"size": 4843,
"upload_time": "2024-07-09T18:20:18",
"upload_time_iso_8601": "2024-07-09T18:20:18.896208Z",
"url": "https://files.pythonhosted.org/packages/fd/39/a112ec52858686d24d1d5de730a2c2f1969908d2ccf63828295c70792afc/cached_iterators-1.0.3-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "16c29913e76ab6f31ee26bec6954018af8935e9e4a785feb78d8a0d12ab699a2",
"md5": "255603e8d614f6665250ce3c0c6e07fe",
"sha256": "38e07a8364edeadfdd8c6dfb37b9154d5bf151a41e3dfa294e84a1ce0fdab38c"
},
"downloads": -1,
"filename": "cached_iterators-1.0.3.tar.gz",
"has_sig": false,
"md5_digest": "255603e8d614f6665250ce3c0c6e07fe",
"packagetype": "sdist",
"python_version": "source",
"requires_python": "<4.0,>=3.10",
"size": 3313,
"upload_time": "2024-07-09T18:20:21",
"upload_time_iso_8601": "2024-07-09T18:20:21.902102Z",
"url": "https://files.pythonhosted.org/packages/16/c2/9913e76ab6f31ee26bec6954018af8935e9e4a785feb78d8a0d12ab699a2/cached_iterators-1.0.3.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-09 18:20:21",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "ulbwa",
"github_project": "cached-iterators",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "cached-iterators"
}