Advertisement
mwchase

Yet more Python code that I'm not sure about

Apr 18th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. class Action:
  2.     def handle(self, to_process):
  3.         raise NotImplementedError
  4.  
  5.  
  6. class Yield(Action):
  7.     def __init__(self, item):
  8.         self.item = item
  9.  
  10.     def handle(self, _to_process):
  11.         yield self.item
  12.  
  13.  
  14. class Extend(Action):
  15.     def __init__(self, iterable):
  16.         self.iterable = iterable
  17.  
  18.     def handle(self, to_process):
  19.         to_process.extend(self.iterable)
  20.         yield from ()
  21.  
  22.  
  23. def handle(action, to_process):
  24.     if action is not None:
  25.         yield from action.handle(to_process)
  26.  
  27.  
  28. def stack_iter(first, process):
  29.     to_process = [first]
  30.     while to_process:
  31.         yield from handle(process(to_process.pop()), to_process)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement