Package: marx-workflows
marx=====Framework/tools for python that get work done, make code easy to test,and turn spaghetti logic back into something you can love.The modeling here provides clear contracts, state encapsulation,and strict typing.Example-------You'll find the [full example](./tests/workflow/example_1.py) in the tests/ directory."""This defines a workflow around the imperative: "Throw a Pie".In a simple system this is about 5 lines of code. But in others,where business logic requires the interaction of many systems,logic is smeared around and duplicated, and not well encapsulated.The approach here splits execution into discrete units which can becomposed into other workflows encouraging reuse, testability, etc."""from marx.workflow.step import LogicUnit, ArgSpec, Step, ResultSpecfrom marx.workflow.flow import Workflowfrom marx.workflow.context import DefaultContext, Fieldfrom tests.workflow.example_objects import User, PermissionDeniedErrorclass IsUserAuthorized(LogicUnit):"""Checks permission for a user+action, and notifies authoritiesif it fails."""user = ArgSpec(User, docs="The user performing the action")def __init__(self, action):"""@param action: The action that will be checked."""self.action = actiondef __call__(self, user):"""@param user: The user performing the action."""if self.is_authorized(user):returnself.notify_authorities(user, self.action)raise PermissionDeniedError(self.action)def is_authorized(self, user):return user.name in ("bob", "mary")def notify_authorities(self, user, action):print "AUTHORITIES!!!", user, " attempted illegal action", self.actionclass MakePie(LogicUnit):"""Makes the pie."""maker = ArgSpec(User, docs="The person making pie.")pie = ResultSpec(basestring, docs="Kind of pie")def __call__(self, maker):maker.increment("pies_made", 1)self.result.pie = 'lemon'class ThrowThing(LogicUnit):"""Subject Object (Verb) Indirect-object"""actor = ArgSpec(User)hit = ResultSpec(bool, default=False, docs="Did we get 'em?")# we omit target and thing here, because we don't# need to enumerate/type constrain the values in this exampledef __call__(self, actor, thing, target):actor.increment("things_throw")print "Throwing", thingself.result.hit = actor.can_throw()# we don't need to return, but we can.return self.resultclass ThrowPieContext(DefaultContext):"""The execution context for the ThrowPieWorkflow."""thrower = Field(User, docs="Somebody has to throw it")target = Field(User, docs="At somebody")pie = Field(str, docs="A pie, which we make along the way")was_hit = Field(bool, docs="Success of the throwing event")""" A workflow is a series of steps."""IsUserAuthorizedStep = Step(IsUserAuthorized("throw_pie"),# we bind from the context to the arguments of the method.arg_map={IsUserAuthorized.USER: ThrowPieContext.THROWER})MakePieStep = Step(MakePie(),arg_map={MakePie.MAKER: ThrowPieContext.THROWER},# we bind from the returned result back to the contextresult_map=MakePie.ResultMap(ThrowPieContext))ThrowThingStep = Step(ThrowThing(),arg_map=ThrowThing.AutoMap({ThrowThing.ACTOR: ThrowPieContext.THROWER,ThrowThing.THING: ThrowPieContext.PIE}),result_map={ThrowPieContext.WAS_HIT: 'hit'})""" There are a few ways to build up a workflow. By constructor..."""ThrowPieWorkflowA = Workflow(steps=[IsUserAuthorizedStep,MakePieStep,ThrowThingStep,])""" Or using add_step..."""ThrowPieWorkflowB = Workflow().add_step(IsUserAuthorizedStep).add_step(MakePieStep).add_step(ThrowThingStep)""" Or using the overloaded addition operator."""EmptyWorkflow = Workflow()ThrowPieWorkflowC_A = EmptyWorkflow + IsUserAuthorizedStep + MakePieStepThrowPieWorkflowC_B = EmptyWorkflow + ThrowThingStepThrowPieWorkflowC = ThrowPieWorkflowC_A + ThrowPieWorkflowC_Bdef run():"""To execute a workflow, prepare a context, and pass it through."""ctx = ThrowPieContext()ctx.thrower = User("bob")ctx.target = User("frank")for WorkflowType in (ThrowPieWorkflowA,ThrowPieWorkflowB,ThrowPieWorkflowC):try:WorkflowType(ctx)assert ctx.was_hit is not Noneassert ctx.pie == 'lemon'return ctxexcept PermissionDeniedError:assert False# Ensure that our ThrowPieWorkflowC did not modify its componentsassert EmptyWorkflow.steps == []assert ThrowThingStep not in ThrowPieWorkflowC_A.steps