Skip to main content

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.

Metadata

Release files for python-on-rails 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-on-rails 1.0.1
File Size Uploaded Provenance Actions
python_on_rails-1.0.1.tar.gz 5.6 kB Without provenance Details for python_on_rails-1.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-on-rails 1.0.1
File Interpreter ABI Platform Actions
python_on_rails-1.0.1-py3-none-any.whl Python 3 none any Details for python_on_rails-1.0.1-py3-none-any.whl

Total release size: 12.2 kB

Release files / python_on_rails-1.0.1.tar.gz

Download URL python_on_rails-1.0.1.tar.gz
Size 5.6 kB
Tags Source
SHA-256 checksum
How to use checksums
274bdf32e3433fe9ad4b6e8df313826abe75545a8cb1ea2f8045a3e00b224b65
BLAKE2b-256 checksum
How to use checksums
952cc3500e43b14447455b0dff057c27639254f28dfb99e6ca97dae6936a3da5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / python_on_rails-1.0.1-py3-none-any.whl

Download URL python_on_rails-1.0.1-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d666d76a13e1157c969fce9391cfc5b8f5acfed7e2384409c24adc39dc736955
BLAKE2b-256 checksum
How to use checksums
34ae715eb5a27db38a781f55d0121baf51ee9a75143c983ef12838e6b87f1025
Upload date
Uploaded using Trusted Publishing?
What is 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

Security for python-on-rails 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page