Name | pyio-effect JSON |
Version |
0.1.4
JSON |
| download |
home_page | None |
Summary | Tiny IO/Try-like effect wrapper for Python. |
upload_time | 2025-08-18 21:35:32 |
maintainer | None |
docs_url | None |
author | Pablo Picouto Garcia |
requires_python | >=3.9 |
license | Copyright (c) 2025 Pablo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. |
keywords |
effects
monads
io
try
functional
|
VCS |
 |
bugtrack_url |
|
requirements |
setuptools
testcontainers
confluent-kafka
OSlash
pykka
pyio-effect
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# PyIO - Python Effects System
A lightweight monadic effects system for Python that provides safe error handling and functional composition. PyIO wraps values and exceptions, allowing you to chain operations without explicit error checking at each step.
## Overview
PyIO acts like a `Try` type - it can hold either a successful value or a captured exception. Operations are automatically skipped if a previous step failed, and errors propagate through the chain until handled.
## Core Concept
```python
from pyio import PyIO
# Success case
result = PyIO("hello").map(str.upper).get() # "HELLO"
# Error case - division by zero is captured and propagated
result = PyIO(10).map(lambda x: x // 0).get_or_else(42) # 42
```
## Operators
### Transformation Operators
- **`map(func)`** - Transform the value if successful, capture any exceptions thrown by `func`
- **`flat_map(func)`** - Transform with a function that returns a PyIO, flatten the result
- **`filter(predicate)`** - Keep value only if predicate returns True, otherwise become empty
### Recovery Operators
- **`recover(func)`** - Handle errors by providing a recovery function that takes the exception
- **`recover_with(func)`** - Handle errors with a function that returns a PyIO
### Conditional Operators
- **`when(predicate, func)`** - Apply `func` only if `predicate` returns True
### Parallel Processing Operators
- **`on_parallel(func_1, func_2, merge_func, max_workers=None)`** - Run two functions concurrently on the same value, then merge their results using `merge_func`
### Side-Effect Operators
- **`on_success(func)`** - Execute a side-effect function if the PyIO contains a value
- **`on_error(func)`** - Execute a side-effect function if the PyIO contains an error
### State Inspection Operators
- **`is_success()`** - Returns True if the PyIO contains a value and no error
- **`is_error()`** - Returns True if the PyIO contains an error
- **`is_empty()`** - Returns True if the PyIO's value is None
### Extraction Operators
- **`get()`** - Extract the value (unsafe - throws if there was an error)
- **`get_or_else(default)`** - Extract the value or return default if error/None
- **`failed()`** - Extract the captured exception (returns the BaseException)
## Usage Examples
### Basic Chaining
```python
result = (PyIO("hello world")
.map(str.upper)
.map(lambda s: s + "!!!")
.get()) # "HELLO WORLD!!!"
```
### Error Handling
```python
result = (PyIO(10)
.map(lambda x: x // 0) # ZeroDivisionError captured
.recover(lambda ex: 999) # Recover with default value
.map(lambda x: x * 2)
.get()) # 1998
```
### Conditional Processing
```python
result = (PyIO(15)
.when(lambda n: n > 10, lambda n: n * 100)
.get()) # 1500
```
### Parallel Processing
```python
import time
def slow_double(x):
time.sleep(1)
return x * 2
def slow_square(x):
time.sleep(1)
return x ** 2
result = (PyIO(5)
.on_parallel(
slow_double, # 5 * 2 = 10
slow_square, # 5 ** 2 = 25
lambda a, b: a + b, # 10 + 25 = 35
max_workers=2
)
.get()) # 35 (computed in ~1 second instead of 2)
```
### State Inspection
```python
# Check if computation was successful
pyio_value = PyIO(42).map(lambda x: x * 2)
if pyio_value.is_success():
print(f"Success: {pyio_value.get()}")
# Check for errors
pyio_error = PyIO(10).map(lambda x: x // 0)
if pyio_error.is_error():
print(f"Error occurred: {pyio_error.failed()}")
# Check if value is None
empty_pyio = PyIO(None)
if empty_pyio.is_empty():
print("Value is None")
```
### Side Effects
```python
result = (PyIO("processing data")
.on_success(lambda msg: print(f"LOG: {msg}")) # Prints log message
.map(str.upper)
.on_success(lambda msg: print(f"RESULT: {msg}")) # Prints result
.get())
```
### Error Side Effects
```python
result = (PyIO(10)
.map(lambda x: x // 0) # This will fail
.on_error(lambda ex: print(f"Error logged: {type(ex).__name__}"))
.recover(lambda ex: 0)
.get()) # 0
```
## Installation
Simply copy the `pyio.py` file to your project directory and import:
```python
from pyio import PyIO
```
## License
This project is open source. See the license file for details.
Raw data
{
"_id": null,
"home_page": null,
"name": "pyio-effect",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.9",
"maintainer_email": null,
"keywords": "effects, monads, io, try, functional",
"author": "Pablo Picouto Garcia",
"author_email": null,
"download_url": "https://files.pythonhosted.org/packages/eb/25/dd6c091cd917ceda2d02538d1e106da183cfd0d105e614db2bc0e80149a4/pyio_effect-0.1.4.tar.gz",
"platform": null,
"description": "# PyIO - Python Effects System\n\nA lightweight monadic effects system for Python that provides safe error handling and functional composition. PyIO wraps values and exceptions, allowing you to chain operations without explicit error checking at each step.\n\n## Overview\n\nPyIO acts like a `Try` type - it can hold either a successful value or a captured exception. Operations are automatically skipped if a previous step failed, and errors propagate through the chain until handled.\n\n## Core Concept\n\n```python\nfrom pyio import PyIO\n\n# Success case\nresult = PyIO(\"hello\").map(str.upper).get() # \"HELLO\"\n\n# Error case - division by zero is captured and propagated\nresult = PyIO(10).map(lambda x: x // 0).get_or_else(42) # 42\n```\n\n## Operators\n\n### Transformation Operators\n\n- **`map(func)`** - Transform the value if successful, capture any exceptions thrown by `func`\n- **`flat_map(func)`** - Transform with a function that returns a PyIO, flatten the result\n- **`filter(predicate)`** - Keep value only if predicate returns True, otherwise become empty\n\n### Recovery Operators\n\n- **`recover(func)`** - Handle errors by providing a recovery function that takes the exception\n- **`recover_with(func)`** - Handle errors with a function that returns a PyIO\n\n### Conditional Operators\n\n- **`when(predicate, func)`** - Apply `func` only if `predicate` returns True\n\n### Parallel Processing Operators\n\n- **`on_parallel(func_1, func_2, merge_func, max_workers=None)`** - Run two functions concurrently on the same value, then merge their results using `merge_func`\n\n### Side-Effect Operators\n\n- **`on_success(func)`** - Execute a side-effect function if the PyIO contains a value\n- **`on_error(func)`** - Execute a side-effect function if the PyIO contains an error\n\n### State Inspection Operators\n\n- **`is_success()`** - Returns True if the PyIO contains a value and no error\n- **`is_error()`** - Returns True if the PyIO contains an error\n- **`is_empty()`** - Returns True if the PyIO's value is None\n\n### Extraction Operators\n\n- **`get()`** - Extract the value (unsafe - throws if there was an error)\n- **`get_or_else(default)`** - Extract the value or return default if error/None\n- **`failed()`** - Extract the captured exception (returns the BaseException)\n\n## Usage Examples\n\n### Basic Chaining\n```python\nresult = (PyIO(\"hello world\")\n .map(str.upper)\n .map(lambda s: s + \"!!!\")\n .get()) # \"HELLO WORLD!!!\"\n```\n\n### Error Handling\n```python\nresult = (PyIO(10)\n .map(lambda x: x // 0) # ZeroDivisionError captured\n .recover(lambda ex: 999) # Recover with default value\n .map(lambda x: x * 2)\n .get()) # 1998\n```\n\n### Conditional Processing\n```python\nresult = (PyIO(15)\n .when(lambda n: n > 10, lambda n: n * 100)\n .get()) # 1500\n```\n\n### Parallel Processing\n```python\nimport time\n\ndef slow_double(x):\n time.sleep(1)\n return x * 2\n\ndef slow_square(x):\n time.sleep(1)\n return x ** 2\n\nresult = (PyIO(5)\n .on_parallel(\n slow_double, # 5 * 2 = 10\n slow_square, # 5 ** 2 = 25\n lambda a, b: a + b, # 10 + 25 = 35\n max_workers=2\n )\n .get()) # 35 (computed in ~1 second instead of 2)\n```\n\n### State Inspection\n```python\n# Check if computation was successful\npyio_value = PyIO(42).map(lambda x: x * 2)\nif pyio_value.is_success():\n print(f\"Success: {pyio_value.get()}\")\n\n# Check for errors\npyio_error = PyIO(10).map(lambda x: x // 0)\nif pyio_error.is_error():\n print(f\"Error occurred: {pyio_error.failed()}\")\n\n# Check if value is None\nempty_pyio = PyIO(None)\nif empty_pyio.is_empty():\n print(\"Value is None\")\n```\n\n### Side Effects\n```python\nresult = (PyIO(\"processing data\")\n .on_success(lambda msg: print(f\"LOG: {msg}\")) # Prints log message\n .map(str.upper)\n .on_success(lambda msg: print(f\"RESULT: {msg}\")) # Prints result\n .get())\n```\n\n### Error Side Effects\n```python\nresult = (PyIO(10)\n .map(lambda x: x // 0) # This will fail\n .on_error(lambda ex: print(f\"Error logged: {type(ex).__name__}\"))\n .recover(lambda ex: 0)\n .get()) # 0\n```\n\n## Installation\n\nSimply copy the `pyio.py` file to your project directory and import:\n\n```python\nfrom pyio import PyIO\n```\n\n## License\n\nThis project is open source. See the license file for details.\n",
"bugtrack_url": null,
"license": "Copyright (c) 2025 Pablo\n Permission is hereby granted, free of charge, to any person obtaining a copy\n of this software and associated documentation files (the \u201cSoftware\u201d), to deal\n in the Software without restriction, including without limitation the rights\n to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n copies of the Software, and to permit persons to whom the Software is\n furnished to do so, subject to the following conditions:\n The above copyright notice and this permission notice shall be included in\n all copies or substantial portions of the Software.\n THE SOFTWARE IS PROVIDED \u201cAS IS\u201d, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\n THE SOFTWARE.",
"summary": "Tiny IO/Try-like effect wrapper for Python.",
"version": "0.1.4",
"project_urls": {
"Homepage": "https://github.com/politrons/Dive-into-Python",
"Issues": "https://github.com/politrons/Dive-into-Python"
},
"split_keywords": [
"effects",
" monads",
" io",
" try",
" functional"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "b4d50562d9201c44fd6a7436e687d814f36320f64b8ca255df6540f4998d2c78",
"md5": "41a27a937cd1ee3207922f6cfcf849aa",
"sha256": "46931e44d796d9948cd39e6ed6475b3f11a43e47d2c24a0211e5aacaa496c111"
},
"downloads": -1,
"filename": "pyio_effect-0.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "41a27a937cd1ee3207922f6cfcf849aa",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.9",
"size": 5933,
"upload_time": "2025-08-18T21:35:30",
"upload_time_iso_8601": "2025-08-18T21:35:30.572066Z",
"url": "https://files.pythonhosted.org/packages/b4/d5/0562d9201c44fd6a7436e687d814f36320f64b8ca255df6540f4998d2c78/pyio_effect-0.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "eb25dd6c091cd917ceda2d02538d1e106da183cfd0d105e614db2bc0e80149a4",
"md5": "4543b552e4d4370182778e3c8e7e772a",
"sha256": "3f1886fda94b790b9bd41b208ab22e466194783e1d996bde3ca734e541ef292d"
},
"downloads": -1,
"filename": "pyio_effect-0.1.4.tar.gz",
"has_sig": false,
"md5_digest": "4543b552e4d4370182778e3c8e7e772a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.9",
"size": 5357,
"upload_time": "2025-08-18T21:35:32",
"upload_time_iso_8601": "2025-08-18T21:35:32.707937Z",
"url": "https://files.pythonhosted.org/packages/eb/25/dd6c091cd917ceda2d02538d1e106da183cfd0d105e614db2bc0e80149a4/pyio_effect-0.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-08-18 21:35:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "politrons",
"github_project": "Dive-into-Python",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "setuptools",
"specs": [
[
"~=",
"80.3.1"
]
]
},
{
"name": "testcontainers",
"specs": [
[
"~=",
"4.12.0"
]
]
},
{
"name": "confluent-kafka",
"specs": [
[
"~=",
"2.11.0"
]
]
},
{
"name": "OSlash",
"specs": [
[
"~=",
"0.6.3"
]
]
},
{
"name": "pykka",
"specs": [
[
"~=",
"4.2.0"
]
]
},
{
"name": "pyio-effect",
"specs": [
[
"==",
"0.1.3"
]
]
}
],
"lcname": "pyio-effect"
}