This library allows users to easily wrap functions with a series of decorators to form
an execution pipeline.
This is useful in scenarios where input needs to be cleaned and output needs to be processed in a systematic way.
## Installation
pip install execution-pipeline
## Usage
A pipeline consists of four optional segments
### Pre
The `pre` execution segment allows you to modify any input parameters passed to the decorated function. Any functions
passed to the `pre` segment will always be executed first.
from pipeline import execution_pipeline
def do_thing_before(params):
params['arg1'] = 'okay'
return params
@execution_pipeline(pre=[do_thing_before])
def do_thing(arg1=5):
return arg1*10
do_thing()
# okayokayokayokayokayokayokayokayokayokay
### Post
The `post` execution segment allows you to modify or handle the result after the decorated function has already run.
def do_thing_after(response):
response['added'] = 'yup'
return response
@execution_pipeline(post=[do_thing_after])
def do_thing(**kwargs):
return {**kwargs} # just make a new dictionary using the passed keyword arguments
do_thing(apples=2, oranges=3, bananas=0)
# {'apples': 2, 'oranges': 3, 'bananas': 0, 'added': 'yup'}
Just like the other segments, you can use as many post processing functions as you need; they will be executed in the order
that they are passed.
def do_another_thing_after(response):
assert response['added'] == 'yup' # the one that is first in the pipeline happens first.
response['also_added'] = 'also yup'
return response
@execution_pipeline(post=[do_thing_after, do_another_thing_after])
def do_thing(**kwargs):
return {**kwargs}
do_thing()
# {'apples': 2, 'oranges': 3, 'bananas': 0, 'added': 'yup', 'also_added': 'also yup'}
### Error
The `error` execution segment allows you to pass error handling to log, modify, absorb, etc. any exceptions that are
raised by the wrapped function.
class MyException(BaseException):
pass
def handle_this_error(e, response=None):
print(f"oh no, Bob! {e}")
return "Don't worry, we handled a TypeError."
def handle_that_error(e, response=None):
print(f"oh no, Bob! {e}")
return "Don't worry, we handled MyException."
error_handlers = [
{"exception_class": TypeError, "handler": handle_this_error},
{"exception_class": MyException, "handler": handle_that_error},
]
@execution_pipeline(pre=[do_thing_before], post=[do_thing_after], error=error_handlers)
def fun_boys(arg1, arg4, arg2, arg3, thing=None):
raise MyException('Something went wrong!')
result = fun_boys(1, 2, 3, 4, 5)
# oh no, Bob! Something went wrong!
print(result)
# Don't worry, we handled MyException.
### Cache
The `cache` execution segment will record all arguments (before and after the `pre` segment) and store the result
(after the `post` and `error` segments).
from pipeline.cache.mock import MockCache
# MockCache is basically just a dict() with some expiration convenience methods.
mock_cache = MockCache()
changing_value = 0
@execution_pipeline(cache=mock_cache)
def fun_boys(arg1, arg4, arg2, arg3, thing=None):
return changing_value
fun_boys(1, 2, 3, 4, 5)
# 0
changing_value = 100
fun_boys(1, 2, 3, 4, 5)
# 0 # ignores the changes ( ¯\_(ツ)_/¯ that's caching! )
#### Supported Cache Backends
Note: if the appropriate backend is not installed, they will be replaced with a `MockCache` instance at runtime. This
is intended to improve portability of pipeline code.
##### Redis
```
pip install redis
```
And then same as above except with
from pipeline.cache.redis import RedisCache
redis = RedisCache(host='localhost', port=6379) # defaults
##### MemCached
```
pip install memcached
```
And then the same as above except with
from pipeline.cache.mem_cache import MemCache
mem_cache = MemCache(host='localhost', port=11211) # defaults
Metadata
Release files for execution-pipeline 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| execution-pipeline-0.2.0.tar.gz | 6.7 kB | Without provenance | Details for execution-pipeline-0.2.0.tar.gz |
Release files / execution-pipeline-0.2.0.tar.gz
| Download URL | execution-pipeline-0.2.0.tar.gz |
|---|---|
| Size | 6.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6c2c2997c6ba2f8f3ebe80c98b4a80a73ca334f657968a57162c547985c7511c
|
|
BLAKE2b-256 checksum How to use checksums |
2cae04c2b59b5c6d500933d26934a45a8c2785ca386b2124fb4c787eeea40f0a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
Python-urllib/3.6
|
Security for execution-pipeline 0.2.0
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.