Skip to content

Instantly share code, notes, and snippets.

@Chris2048
Created October 1, 2014 21:24
Show Gist options
  • Save Chris2048/3f6a41fc325c488673f2 to your computer and use it in GitHub Desktop.
Save Chris2048/3f6a41fc325c488673f2 to your computer and use it in GitHub Desktop.
Controller ideas
### Controller ideas
# Subcontroller takes the lead
class Controller(object):
def go(self):
a = self.aye()
yield a
b = self.bee(a)
yield b
c = self.cea(b)
yield c
def aye(self):
return "A\n"
def bee(self, a):
return "B\n"
def cea(self, b):
return "C\n"
print [i for i in Controller().go()]
class SubController(Controller):
def go(self):
controller = super(SubController, self).go()
yield controller.next()
b = controller.next()
yield b
d = self.dee(b)
yield d
# missing 'yield from' ;;
for i in controller:
yield i
def dee(self, b):
return "D\n"
print [i for i in SubController().go()]
# controller will execute any function it is passed
# ( could 'yield from' passed-in generator if there are lots of functions )
class Controller2(object):
def go(self):
l = [self.aye, self.bee, self.cea]
v = None
for i in l:
v = i(v) if v else i()
r = yield v
while r:
v = r(v)
r = yield v
def aye(self):
return "A\n"
def bee(self, a):
return "B\n"
def cea(self, b):
return "C\n"
print [i for i in Controller2().go()]
class SubController2(Controller2):
def go(self):
controller = super(SubController2, self).go()
yield controller.next()
yield controller.next()
yield controller.send(self.dee)
for i in controller:
yield i
def dee(self, b):
return "D\n"
print [i for i in SubController2().go()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment