Package: python-on-rails
Python on RailsA library for supporting Railway-Oriented Programming in Python.InstallationInstall the packagepython_on_railsversion1.0+from PyPi.
The recommendedrequirements.txtline ispython_on_rails~=1.0.
This package usessemantic versioning.Code exampleBelow is an example to get started. Define functions ("railway stops") and wrap the return object into aResult.success(orResult.failure) object.
Create aRailwayobject and add your stops to it, finally callrun().frompython_on_rails.railwayimportRailwayfrompython_on_rails.resultimportResultdefdownload(url):# fetch data from url...returnResult.success([{"date":"2022-01-19","clicks":13},{"date":"2022-01-20","clicks":37},])defparse(downloaded_data):returnResult.success([[entity["date"],entity["clicks"]]forentityindownloaded_data])defoutput(parsed_data):returnResult.success({"data":{"rows":parsed_data}})result=Railway(download,parse,output).run()print(f"{result=}")Wrapping the return object can be omitted if you useas_resultdecorator fromresultmodule. Please seerailway_catch_test.pyfor example.railwaymoduleRailway(class)Combined withResultobjects, a means of Railway-Oriented Programming.
ARailwayobject 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 aResultobject.run(instance method)Triggers the process. If aninitialvalue is not supplied, the first step will be called with aResult.success(None).By default, if the process completes successfully, the final result will be unwrapped. If it fails however, aResultobject with its error set will be returned. This behaviour can be configured by passing the optionalsuccess_handlerandfailure_handler. Those handlers are both functions that accept a single argument - aResultobject.resultmoduleResult(class)An object encapsulating avalueor anerror.is_failureandis_success(instance methods)Predicate methods denoting whether the result object has its error or value set.Result.failureandResult.success(class methods)Constructors that creates aResultobject with either its error or value set.as_result(function)Convenience decorator builder applying theResultobject to the genericcatchdecorator. Accepts any number of exceptions to catch and turn intoResultobjects.failure(function)Convenience function for creating a Result object with its error set.
Has the same effect as calling theResult.failureconstructor.success(function)Convenience function for creating a Result object with its value set.
Has the same effect as calling theResult.successconstructor.unwrap(function)Convenience function for extracting the value from a Result object.eithermoduleEither,Failure, andSuccess(classes)An implementation of theeithermonad. The abstractEitherbase class encapsulates a value and is implemented as theSuccessandFailureclasses.bind(instance method)Accepts a monadic function that takes a single argument and returns either aSuccessor aFailure.For aSuccess, calls the supplied function with the unwrapped argument, returning its result.
For aFailure, ignores the supplied function and returns the existing failure.as_either(function)Convenience decorator builder applying theEithermonad to the genericcatchdecorator. Accepts any number of exceptions to catch and turn intoFailureobjects.commonmodulecatch(function)A decorator builder that ensures that the result of calling a function is returned as aResultobject. Given a list of exceptions, catches those and wraps them into aResultobject with its error set.Can optionally be configured to handle and use theEithermonad instead by passing theresult_class,failure, andsuccessarguments.identity(function)Theidentity function.