Skip to main content

A Railway pattern based pypeline package

Project description

Pypelinerr

ƒpython-versions pypi-badge license

Railway Oriented Programming implementation for Python.

Motivation

Handling events that trigger a sequence of commands or side-effects can be extremely elegant using the railway pattern. This package can allow you to create a dedicated pipeline for each flow or event, that will preform the tasks in an ordered fashion.

Basic Usage

  1. Create a class that inherit from Pipeline, with phases method:
from pypelinerr import Pipeline


class SomeEventHandler(Pipeline):
    def phases(self):
        return [
            'phase_one',
            'phase_two',
            'phase_three'
        ]

    def phase_one(self):
        self.options['option_for_second_phase'] = True

    def phase_two(self):
        if 'option_for_second_phase' in self.options:
            return 'an option got passed from first phase, cool!'

    def phase_three(self):
        self.options['reached_third_phase'] = True
  1. in the pypeline manager, use SomeEventHandler(options).run(), where options are the input to the pipeline flow.

Example

For example, if you create a pipeline that first connects to a database, then fetches the a document by an id, validates it, and finally send a post request of the entry's user_id, it can look like this:

import requests
from pymongo import MongoClient

from pypelinerr import Pipeline


class RetrieveDataAndPost(Pipeline):
    def phases(self):
        return [
            'connect_to_mongo',
            'fetch_document',
            'validate_document',
            'post_results'
        ]

    def connect_to_mongo(self):
        self.options['albums_collection'] = MongoClient('<MongoDB_URL>').albums

    def fetch_document(self):
        album_id = self.options.get('album_id')
        self.options['album'] = self.options['albums_collection'].find_one({ 'id': album_id })

    def validate_document(self):
        if 'artist_name' not in self.options['album']:
            self.fail_operation()

    def post_results(self):
        selected_artist =  self.options.get('album').get('artist_name')
        requests.post('<Artists_Service_URL>', data={'selected_artist': selected_artist})

Features

Break operation

Calling the break_operation(message?) allows you to break from the pipeline's (event's handler) pipeline on an invalid result, without failing the whole pipeline. An example for such a use-case is when checking in a DataBase if an entity not exists, and if so - not continuing the pipeline's flow.

In the bellow flow, we want to process a CRUD event named OrderCreated of our online shop, where if the user details not present in our DB, we would not want to proceed, because only signed-up users can create an order:

from pypelinerr import Pipeline


class OrderCreated(Pipeline):
    def phases(self):
        return [
            'validate_user_exists',
            'create_an_order',
            '...',
            '...'
        ]

    def validate_user_exists(self):
        user = db.user_collection.find_one({'id': self.options['user_id']})
        if not user:
            self.break_operation(message='user not in mongo collection')

    def create_an_order(self):
        ...

Fail operation

By calling fail_operation(message?), we can stop and fail the pipeline's operation. This can be very useful in case of, for example, a momentarily network connection issue with another service or a remote DB, when you will want to send the pipeline's operation result object to a failed-queue, for later processing of the event from the phase it failed, and with the current options.

We can also raise an Exception and it will count as a failed-operation, but in a case where we have a wrapper around the service call / DB access which already catches the exception, this is very useful:

Other Module that handle mongo connection:

from pymongo import MongoClient


class UserCollection:
    def init_connection(self):
        try:
            return MongoClient('<MongoDB_URL>').db.user_collection
        except Exception as e:
            return None
    ...

Our pypeline:

from models.user import UserCollection
from pypelinerr import Pipeline


class OrderCreated(Pipeline):
    def phases(self):
        return [
            'validate_user_exists',
            'create_an_order',
            '...',
            '...'
        ]

    def validate_user_exists(self):
        user_collection =  UserCollection().init_connection()
        if not user_collection:
            self.fail_operation(message='mongo connection failure')

    def create_an_order(self):
        ...

Schema

The Schema mechanism allows us to validate the messages (the event payload) been passed to the pipeline, before the pipeline itself starts:

schema = Schema({'user_id': int, 'logged_in': bool})
event_payload = {'user_id': 'not a number', 'logged_in': True}
OrderCreated(options=event_payload, schema=schema).run()

This run will result in a failure, with a fail message of: schema.SchemaUnexpectedTypeError: 'not a number' should be instance of 'int'

entry_phase

The entry phase allow us to use the pipeline from a specific phase, and skip it's previous phases. The most common use-case for this is re-running the pipeline in a case of failure, or using only a small part of the whole pipeline.

event_payload = {'user_id': 123, 'logged_in': True}
OrderCreated(options=event_payload, entry_phase='create_an_order').run()

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

pypelinerr-0.0.4.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.

pypelinerr-0.0.4-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file pypelinerr-0.0.4.tar.gz.

File metadata

  • Download URL: pypelinerr-0.0.4.tar.gz
  • Upload date:
  • Size: 5.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pypelinerr-0.0.4.tar.gz
Algorithm Hash digest
SHA256 8fc3e33e3faf818d964b24df65dfd7e34bb1ac2feb0699367d58ebfe399329b1
MD5 51d649d2fa3cf52be6a79c4a287c2a41
BLAKE2b-256 fce7ff27433cc4a9e23ce8be7f5cc259b0a41dbda1352e535c17d6276472848c

See more details on using hashes here.

File details

Details for the file pypelinerr-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pypelinerr-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.8.5

File hashes

Hashes for pypelinerr-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 41131532d1df6a987d20ef48c152f039f8f4af9f79c1b09306cf2d48e0bf6ebe
MD5 0d328e99350e1fed213f02712eee37f6
BLAKE2b-256 0f4f7b392451f3aca1fc2d0ad32a2b4f5e6bcaaf45ed921e280985cef3546004

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