def coroutine(func):
def start(*args,**kwargs):
cr = func(*args,**kwargs)
cr.next()
return cr
return start
@coroutine
def grep(pattern):
space = ", "
print(pattern + space+ pattern+ space+ pattern+ space+ pattern+ ".\n")
try:
while True:
line = (yield)
if pattern in line:
print(line)
except GeneratorExit:
print("Closing the coroutine.")
if __name__ == '__main__':
# - Declaring a generator expression as a coroutine
g = grep("Bueller") # This is where you pass in the pattern
# - Text that doesn't matter
g.send("Whats the score?\n")
g.send("Nothing and Nothing.\n")
g.send("Whos winning?\n")
g.send("The Bears.\n")
#- This text should return a positive match
g.send("Random quotes from Ferris Bueller's Day Off are fun!\n")
g.close()