[ create a new paste ] login | about

Link: http://codepad.org/SnDqZCcx    [ raw code | output | fork ]

Python, pasted on Feb 22:
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()


Output:
1
2
3
4
5
Bueller, Bueller, Bueller, Bueller.

Random quotes from Ferris Bueller's Day Off are fun!

Closing the coroutine.


Create a new paste based on this one


Comments: