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.1.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.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyio_effect-0.1.1.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.1.tar.gz
Algorithm Hash digest
SHA256 6b531e9190d311840ec1a354f72ac1bd7685434654baa6db8babac14f79bde09
MD5 293735a9b09b26bce62d8b13bf14d21b
BLAKE2b-256 c5b71a301b58454e7e9265f7cd5bfe122300333e2301c4913c10948e3741362c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyio_effect-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5ebdc90bc03b7a98dc36472478c449f83f8f7c1826f8420b09603328b79e948e
MD5 16e061211b1d5ba3a45fea893eb759cf
BLAKE2b-256 1aae12cfd62fecfb3375be23ec14ff136cc81dd92c436ea4c992538b1e375382

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