/pyrop

Imperative-style railway-oriented programming in Python

Primary LanguagePythonMIT LicenseMIT

PyPI PyPI - License Documentation Tests Run on Ubuntu Python Versions Tests Run on Macos Python Versions Tests Run on Windows Python Versions Github Repo

pyrop

Overview

Imperative-style railway-oriented programming in Python, including fully-typed errors. Supports sync and async functions and methods.

Getting Started

Install pyrop:

pip install pyrop

A simple example:

from pyrop import EitherMonad, Left, catch, monadic

catcher = catch[ValueError | TypeError]()


@catcher
def func_with_error() -> None:
    raise ValueError("This is an error")


@catcher
def success_func() -> int:
    return 1


@monadic
def func(do: EitherMonad[ValueError | TypeError]) -> int:
    value = do << success_func()
    print(f"Value is {value}")
    do << func_with_error()
    print("This will not execute")
    return value


res = func()
assert isinstance(res, Left)
assert isinstance(res.value, ValueError)

try:
    func().get()
except ValueError as e:
    print(f"Caught error: {e}")

assert func().map_left(lambda e: "Error occurred") == Left("Error occurred")
assert func().get_or_else(1) == 1

Links

See the documentation here.

Development Status

This project is currently in early-stage development. There may be breaking changes often. While the major version is 0, minor version upgrades will often have breaking changes.

Developing

See the development guide for development details.

Author

Created by Nick DeRobertis. MIT License.