Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
python:yield [2015-11-07] – created dcaipython:yield [2020-04-19] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +====== Python yield ======
  
 +=== Iterables ===
 +<code>
 +>>> list = [1,2,3,4,5,6]
 +list is iterable
 +>>> list = [x for x in range(7)]
 +Iterables are handy but stores all values in memory
 +</code>
 +=== Generators ===
 +<code>
 +>>> listgenerator = (x for x in range(7))
 +>>> for i in listgenerator: print(i)
 +</code>
 +Generators are iterators, but you can only iterate over them once. It's because they do not store all the values in memory, they generate the values on the fly.
 +=== yield ===
 +Yield is a keyword that is used like return, except the function will return a generator.
 +
 +<code python>
 +def createGenerator():
 +    print("not running until next() called")
 +    for i in range(7):
 +       yield i*i
 +
 +x = createGenerator()
 +for t in x:
 +    print(t)
 +</code>
 +
 +=== Practical example ===
 +
 +tail -f access.log
 +<code python>
 +import time
 +def follow(thefile):
 +    thefile.seek(0,2) # Go to the end of the file
 +    while True:
 +        line = thefile.readline()
 +        if not line:
 +            time.sleep(0.1) # Sleep briefly
 +            continue
 +        yield line
 +
 +logfile = open("access-log")
 +for line in follow(logfile):
 +    print(line)
 +</code>
 +=== Pipeline ===
 +
 +<code python>
 +def grep(pattern,lines):
 +    for line in lines:
 +        if pattern in line:
 +            yield line
 +# Set up a processing pipe : tail -f | grep python
 +logfile = open("access-log")
 +loglines = follow(logfile)
 +pylines = grep("python",loglines)
 +# Pull results out of the processing pipeline
 +for line in pylines:
 +    print line,
 +
 +</code>