rafa-acioly / pipestream

pypeline is an utility package to create a pipeline of classes or callables

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Pipestream

Usage

Pipestream provides a convenient way to "pipe" a given input through a series of classes or callable, giving each class the opportunity to inspect or modify the input and invoke the next callable in the pipeline:

from pipestream import Pipeline


def sum_one(value, next):
    return next(value + 1)


def multiply_by_two(value, next):
    return next(value * 2)


Pipeline
    .send(1)
    .through(
        sum_one,
        multiply_by_two
    )
    .then(print)

# output: 4

As you can see, each invokable class or closure in the pipeline is provided the input and a next closure. Invoking the next closure will invoke the next callable in the pipeline. As you may have noticed, this is very similar to middleware and chain of responsability pattern.

When the last callable in the pipeline invokes the next closure, the callable provided to the then method will be invoked. Typically, this callable will simply return the given input.

Of course, as discussed previously, you are not limited to providing functions to your pipeline. You may also provide classes with common method's between them. If a class is provided, the class method will be accessed by python std method getattr.

from pipestream import Pipeline


class GenerateProfilePhoto:
    def handle(value, next):
        # do stuff
        return next(value)

class ActivateSubscription:
    def handle(value, next):
        # do stuff
        return next(value)

class SendWelcomeEmail:
    def handle(value, next):
        # do stuff
        return next(value)


user = Pipeline
    .send(user)
    .through(
        GenerateProfilePhoto,
        ActivateSubscription,
        SendWelcomeEmail
    )
    .then_return()

By default the Pipeline will try to call the method handle on each class, if you want to change this method you can set it through .via(), you can also set a new destination for the final value with .then()

user = Pipeline
    .send(user)
    .through(
        GenerateProfilePhoto,
        ActivateSubscription,
        SendWelcomeEmail
    )
    .via('do')
    .then(print)

Install

$ pip install pipestream

Running tests

$ pytest tests/

This package is inspired by Laravel's Pipeline Helper

About

pypeline is an utility package to create a pipeline of classes or callables

License:MIT License


Languages

Language:Python 100.0%