Skip to main content

Railway-Oriented Programming in Python.

Project description

Python on Rails

PyPI PyPI - Python Version PyPI - Status PyPI - License Python package

A library for supporting Railway-Oriented Programming in Python.

Installation

Install the package python_on_rails version 1.0+ from PyPi. The recommended requirements.txt line is python_on_rails~=1.0. This package uses semantic versioning.

Code example

Below is an example to get started. Define functions ("railway stops") and wrap the return object into a Result.success (or Result.failure) object. Create a Railway object and add your stops to it, finally call run().

from python_on_rails.railway import Railway
from python_on_rails.result import Result

def download(url):
    # fetch data from url...
    return Result.success(
        [
            {"date": "2022-01-19", "clicks": 13},
            {"date": "2022-01-20", "clicks": 37},
        ]
    )

def parse(downloaded_data):
    return Result.success([[entity["date"], entity["clicks"]] for entity in downloaded_data])

def output(parsed_data):
    return Result.success({"data": {"rows": parsed_data}})

result = Railway(download, parse, output).run()
print(f"{result=}")

Wrapping the return object can be omitted if you use as_result decorator from result module. Please see railway_catch_test.py for example.

railway module

Railway (class)

Combined with Result objects, a means of Railway-Oriented Programming. A Railway object represents a process comprising of a number of steps. It accepts a number of steps, each of which should be a function that accepts and returns a Result object.

run (instance method)

Triggers the process. If an initial value is not supplied, the first step will be called with a Result.success(None).

By default, if the process completes successfully, the final result will be unwrapped. If it fails however, a Result object with its error set will be returned. This behaviour can be configured by passing the optional success_handler and failure_handler. Those handlers are both functions that accept a single argument - a Result object.

result module

Result (class)

An object encapsulating a value or an error.

is_failure and is_success (instance methods)

Predicate methods denoting whether the result object has its error or value set.

Result.failure and Result.success (class methods)

Constructors that creates a Result object with either its error or value set.

as_result (function)

Convenience decorator builder applying the Result object to the generic catch decorator. Accepts any number of exceptions to catch and turn into Result objects.

failure (function)

Convenience function for creating a Result object with its error set. Has the same effect as calling the Result.failure constructor.

success (function)

Convenience function for creating a Result object with its value set. Has the same effect as calling the Result.success constructor.

unwrap (function)

Convenience function for extracting the value from a Result object.

either module

Either, Failure, and Success (classes)

An implementation of the either monad. The abstract Either base class encapsulates a value and is implemented as the Success and Failure classes.

bind (instance method)

Accepts a monadic function that takes a single argument and returns either a Success or a Failure.

For a Success, calls the supplied function with the unwrapped argument, returning its result. For a Failure, ignores the supplied function and returns the existing failure.

as_either (function)

Convenience decorator builder applying the Either monad to the generic catch decorator. Accepts any number of exceptions to catch and turn into Failure objects.

common module

catch (function)

A decorator builder that ensures that the result of calling a function is returned as a Result object. Given a list of exceptions, catches those and wraps them into a Result object with its error set.

Can optionally be configured to handle and use the Either monad instead by passing the result_class, failure, and success arguments.

identity (function)

The identity function.

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

python_on_rails-1.0.1.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

python_on_rails-1.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file python_on_rails-1.0.1.tar.gz.

File metadata

  • Download URL: python_on_rails-1.0.1.tar.gz
  • Upload date:
  • Size: 5.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.10.0 keyring/23.4.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for python_on_rails-1.0.1.tar.gz
Algorithm Hash digest
SHA256 274bdf32e3433fe9ad4b6e8df313826abe75545a8cb1ea2f8045a3e00b224b65
MD5 ada748873e839972f238e62e98b67fb3
BLAKE2b-256 952cc3500e43b14447455b0dff057c27639254f28dfb99e6ca97dae6936a3da5

See more details on using hashes here.

File details

Details for the file python_on_rails-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: python_on_rails-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.7 tqdm/4.62.3 importlib-metadata/4.10.0 keyring/23.4.0 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.12

File hashes

Hashes for python_on_rails-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d666d76a13e1157c969fce9391cfc5b8f5acfed7e2384409c24adc39dc736955
MD5 e643eae30309f87839dc3448338a7bd1
BLAKE2b-256 34ae715eb5a27db38a781f55d0121baf51ee9a75143c983ef12838e6b87f1025

See more details on using hashes here.

Supported by

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