Conveyor pipline handling library for python 3.5 and later
Project description
conveyr_py
Conveyor pipline handling library for python 3.5 and later
Automatic implementation of pattern "Builder"
Requirements:
- Python >= 3.5
Usage:
install conveyr:
pip install conveyr
Define common interfaces:
from conveyr import Conveyor
#### define interfaces
```py
class IEntity():
id=None
class IHasNameEntity():
name = None
class IHasSurnameEntity():
surname = None
class IHasDesctiptionEntity():
description = None
class IHasAgeEntity():
age=0
Define payloads:
class INamePayload:
pass
class ISurnamePayload:
pass
class IDescriptionPayload:
pass
class IHasAgePayload():
age=0
Define concrete entity class an dpayload class:
class Entity(IEntity,IHasNameEntity,IHasSurnameEntity,IHasDesctiptionEntity,IHasAgeEntity):
pass
class Payload(INamePayload,ISurnamePayload,IDescriptionPayload,IHasAgePayload):
def __init__(self,name,surname,description,age=0):
self.name = name
self.surname = surname
self.description = description
self.age = age
Define handlers per interface:
@Conveyor.handler(order=5)
async def entity_name_handler(entity:IHasNameEntity,payload:INamePayload):
entity.name = payload.name
@Conveyor.handler(order=4)
def entity_surname_handler(entity:IHasSurnameEntity,payload:ISurnamePayload):
entity.surname = payload.surname
@Conveyor.handler(order=3)
def entity_id_handler(entity:IEntity):
entity.id = uuid.uuid4().hex
@Conveyor.handler(order=2)
def entity_description_handler(entity:IHasDesctiptionEntity,payload:IDescriptionPayload):
entity.description = payload.description
#you can define class handler, wich contains `process` method
@Conveyor.handler(order=1)
class PersonAgeHandler:
def process(self,entity:IHasAgeEntity,payload:IHasAgePayload):
entity.age = payload.age
Run conveyor
simple mode:
conveyor = Conveyor()
entity = Entity()
payload = Payload('evgeniy','fetisov','some description',30)
results = conveyor.process(entity,payload)
Conveyor.process_async
method supports to defineasync
await
handlers. In async mode you can define simple synchronous handlers too.
async mode:
import asyncio
conveyor = Conveyor()
loop = asyncio.get_event_loop()
entity = Entity()
payload = Payload('evgeniy','fetisov','some description',30)
results = loop.run_until_complete(conveyor.process_async(entity,payload))
loop.close()
#in python 3.6 and later: results = asyncio.run(conveyor.process_async(entity,payload))
print(results) # dict of results if handlers returns anything
print(entity.id) # some guid id
print(entity.name) # 'evgeniy'
print(entity.surname) # 'fetisov'
print(entity.description) #'some description'
print(entity.age) # 30
Static usage without Conveyor instance:
from conveyr import Conveyor
entity = Entity()
payload = Payload('evgeniy','fetisov','some description',30)
results = Conveyor.process(entity,payload)
Advanced:
You can group handlers to several 'layers'
and manage handling orders
@Conveyor.handler(order=5,group="aftercommit")
async def some_handler(entity:IHasNameEntity,payload:INamePayload):
entity.name = payload.name
#...
results = loop.run_until_complete(conveyor.process(entity,payload,"aftercommit")) # handlers with such group only executed
If you are using class handlers, its possible to set custom class handler initializer (or dependency injector)
def some_class_handler_manager(cls):
return cls()
conveyr = Conveyor(class_handler_manager = some_class_handler_manager)
#...
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
conveyr-1.1.1.tar.gz
(5.1 kB
view details)
File details
Details for the file conveyr-1.1.1.tar.gz
.
File metadata
- Download URL: conveyr-1.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.5.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | e41226ed9db67d958a54386c5b4f394d573af0fb19b1de797e5f096cbfeee005 |
|
MD5 | e128f37b89c7a7980a67a705b566be47 |
|
BLAKE2b-256 | 1142867d1f08df3c58bf7caeb9fb31edfc2ca1460f8c85f40de2faac35040968 |