Skip to content

Conveyor pipline handling library for python 3.5 and later

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE.txt
Apache-2.0
LICENSE.APACHE2
MIT
LICENSE.mit
Notifications You must be signed in to change notification settings

megafetis/conveyr_py

conveyr_py

PyPI Python Downloads

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 define async 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)

#...

About

Conveyor pipline handling library for python 3.5 and later

Topics

Resources

License

Unknown and 2 other licenses found

Licenses found

Unknown
LICENSE.txt
Apache-2.0
LICENSE.APACHE2
MIT
LICENSE.mit

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages