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.3.tar.gz (4.5 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.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyio_effect-0.1.3.tar.gz
  • Upload date:
  • Size: 4.5 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.3.tar.gz
Algorithm Hash digest
SHA256 f51f76b74afc051f56c17a16bfd8e761729087bf73544067642fcd3da0989ad0
MD5 4b3375965fc8b18905f4244003040036
BLAKE2b-256 8c9d02b24efcd4521b00959abbc92b5ae1b29697a496713a6b22e8477d72e1a6

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyio_effect-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 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.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ff6c016a1470e7ea4f750c344992afa63a141b770328b840ed7a3b214251ffb4
MD5 555cbcf798bf271200a6431a28cc68f2
BLAKE2b-256 d2b4a11b7bbc266843edaac128b9071f5fcef290ff2a5506b9a7862463ac7723

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