Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from __future__ import print_function
- from characteristic import attributes
- from effect import Effect, perform, ConstantIntent, base_dispatcher, sync_performer
- from effect.dispatcher import TypeDispatcher, ComposedDispatcher
- @attributes(['name'])
- class MyThing(object):
- """Intent."""
- pass
- @attributes(['scope', 'effect'])
- class MyDecorator(object):
- """Intent which decorates another Effect."""
- pass
- def effect_to_box(eff, box):
- return eff.on(box.succeed, box.fail)
- def perform_decorator(dispatcher, decorator, box):
- print("performing a", decorator)
- def scoped_performer(innerdisp, mything, innerbox):
- print("performing", mything, "in scope", decorator.scope)
- innerbox.succeed(decorator.scope + "." + mything.name)
- new_disp = ComposedDispatcher([
- TypeDispatcher({MyThing: scoped_performer}),
- dispatcher])
- perform(new_disp, effect_to_box(decorator.effect, box))
- td = TypeDispatcher({MyDecorator: perform_decorator})
- disp = ComposedDispatcher([td, base_dispatcher])
- eff = Effect(
- MyDecorator(
- scope='a',
- effect=Effect(
- MyThing(name='foo')
- ).on(
- lambda r: Effect(
- MyThing(name='bar')
- ).on(
- lambda r2: (r, r2)
- )
- )
- )
- ).on(print)
- perform(disp, eff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement