Skip to main content

Tiny IO/Try-like effect wrapper for Python.

Project description

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

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

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

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

Usage Examples

Basic Chaining

result = (PyIO("hello world")
          .map(str.upper)
          .map(lambda s: s + "!!!")
          .get())  # "HELLO WORLD!!!"

Error Handling

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

result = (PyIO(15)
          .when(lambda n: n > 10, lambda n: n * 100)
          .get())  # 1500

Installation

Simply copy the pyio.py file to your project directory and import:

from pyio import PyIO

License

This project is open source. See the license file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyio_effect-0.1.0.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyio_effect-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file pyio_effect-0.1.0.tar.gz.

File metadata

  • Download URL: pyio_effect-0.1.0.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for pyio_effect-0.1.0.tar.gz
Algorithm Hash digest
SHA256 42211574249b46aeff9a98247b00a1be34365be65be4fb223095219271ba750d
MD5 404952843928e7a4065e191ae4c62bac
BLAKE2b-256 46cbf9a191ac0f85d59cbf95b15f7fdf624b7344b0bd8eb9d745a58113cdc883

See more details on using hashes here.

File details

Details for the file pyio_effect-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pyio_effect-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for pyio_effect-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b4299a0acc9d149560283cb3bc8c7895af05d22de5228c9b85425fb92bc990f8
MD5 b01231527a65f1c48bc5d0315620f4f8
BLAKE2b-256 bae7685b428877f608de4933bd7e4d6eb8f36847d5a3f2feb5e8d172c23c3f35

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page