Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. from __future__ import print_function
  2.  
  3. from characteristic import attributes
  4. from effect import Effect, perform, ConstantIntent, base_dispatcher, sync_performer
  5. from effect.dispatcher import TypeDispatcher, ComposedDispatcher
  6.  
  7.  
  8. @attributes(['name'])
  9. class MyThing(object):
  10. """Intent."""
  11. pass
  12.  
  13.  
  14. @attributes(['scope', 'effect'])
  15. class MyDecorator(object):
  16. """Intent which decorates another Effect."""
  17. pass
  18.  
  19.  
  20.  
  21. def effect_to_box(eff, box):
  22. return eff.on(box.succeed, box.fail)
  23.  
  24.  
  25. def perform_decorator(dispatcher, decorator, box):
  26. print("performing a", decorator)
  27. def scoped_performer(innerdisp, mything, innerbox):
  28. print("performing", mything, "in scope", decorator.scope)
  29. innerbox.succeed(decorator.scope + "." + mything.name)
  30. new_disp = ComposedDispatcher([
  31. TypeDispatcher({MyThing: scoped_performer}),
  32. dispatcher])
  33. perform(new_disp, effect_to_box(decorator.effect, box))
  34.  
  35.  
  36. td = TypeDispatcher({MyDecorator: perform_decorator})
  37.  
  38. disp = ComposedDispatcher([td, base_dispatcher])
  39.  
  40. eff = Effect(
  41. MyDecorator(
  42. scope='a',
  43. effect=Effect(
  44. MyThing(name='foo')
  45. ).on(
  46. lambda r: Effect(
  47. MyThing(name='bar')
  48. ).on(
  49. lambda r2: (r, r2)
  50. )
  51. )
  52. )
  53. ).on(print)
  54.  
  55.  
  56. perform(disp, eff)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement