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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyio_effect-0.1.2.tar.gz
  • Upload date:
  • Size: 5.1 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.2.tar.gz
Algorithm Hash digest
SHA256 f1e9f92152d0cb40c599aadda67c3583f5011efdf8fc763dc15f7aff3a812158
MD5 a5149889ddd754d529b55c1e23435c24
BLAKE2b-256 706adf579c5b460ece552c5a076dd1e083d24f44ed12cac17482daf5c27f2e9f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyio_effect-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.7 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 a751eff1d0fc0f05a3225ad1904d3c07da823563655ea2debbd934a5dada7f8f
MD5 38ec34b6a44b87e599fef035be538d35
BLAKE2b-256 f07945903551591d2b74c5bb292cf187a59f16629142bea0eabbb2e7c6c15896

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