Skip to content

Instantly share code, notes, and snippets.

@ianb
Created Jun 18, 2013
Embed
What would you like to do?
class Detector:
def __init__(self):
self.detectors = []
self._pending = []
self.matches = []
def feed_input(self, obj, index):
"""After instantiating Detector and adding individual detector coroutines, call
feed_input(obj, index) over and over until you are done. You should end with a
sentinal obj. Look in .matches to get the results.
Detectors should be implemented like:
def detector(data, obj):
next_obj = yield if_obj_was_okay
next_obj2 = yield if_next_obj_was_okay
return # returning means successful conclusion
# Add info to the data dict at any time
"""
for checker, data in list(self._pending):
try:
result = checker.send(obj)
except StopIteration:
# Success!
self.matches.append(data)
result = False
if not result:
self._pending.remove((checker, data))
for detector in self.detectors:
data = {"index": index, "detector": detector}
checker = detector(data, obj)
# Then we have to prime the checker...
result = next(checker)
if not result:
self._pending.append((checker, data))
def three_clicks(data, ev):
ev = yield ev.type == "click"
ev = yield ev.type == "click"
ev = yield ev.type == "click"
def menu_click_then_other_click(data, ev):
ev = yield ev.type == "click" and ev.target.name == "Menu"
ev = yield ev.type == "click" and ev.target != "Menu"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment