Generators in Python

satya - 10/1/2019, 10:47:47 AM

Generators in Python

Generators in Python

Search for: Generators in Python

satya - 10/1/2019, 10:50:50 AM

Generators are iterators. They are closely related to yield

Generators are iterators. They are closely related to yield

satya - 10/1/2019, 10:51:04 AM

In language reference yield is documented here

In language reference yield is documented here

satya - 10/1/2019, 11:06:39 AM

Few essentials

The yield expression is used when defining a generator function or an asynchronous generator function and thus can only be used in the body of a function definition. Using a yield expression in a function?s body causes that function to be a generator, and using it in an async def function?s body causes that coroutine function to be an asynchronous generator

When a generator function is called, it returns an iterator known as a generator. That generator then controls the execution of the generator function. The execution starts when one of the generator?s methods is called.

it appears, the first call that returns the generator DOES NOT RESULT in any execution of the body of the function at all!!

During first call to one of the functions of generator iterator, At that time, the execution proceeds to the first yield expression, where it is suspended again, returning the value of expression_list to the generator?s caller

Understand the "send" later.

satya - 10/1/2019, 12:03:19 PM

Here is an example


#*********************************************
# Just a utility function that converts
# an ISO string to a filename based on the hour
#*********************************************
#Date format: 2019-09-01 00:05:00.000

def getFilename(datetimeInstanceString):
    fileBasename = "somebase-filename-for-"
    #i - instance
    i = datetime.fromisoformat(datetimeInstanceString)

    #see the formating options for padding 
    #integers are padded with 0s
    filePostFix = (
        "{}{:02d}{:02d}{:02d}"
        .format(i.year, i.month,i.day, i.hour)
    )
    return fileBasename + filePostFix + ".csv"

#*********************************************
# An Example Generator
# ********************
# 1. Read each line in a file (like a cursor)
# 2. Eliminate lines that needs eliminating
# 3. Present the results as if this is an iterator
#*********************************************
def SpecialFileGenerator(inFilename):
    #remember filename
    filename = inFilename
    print("opening file:", inFilename)
    inputFileObj = open(inFilename)
    for idx, line in enumerate(inputFileObj) :
        #ignore first line
        if (idx == 0): 
            print("ignoring first line")
            continue
        #for each line: only do 6 lines
        if (idx > 6): 
            print ("returning")
            return
        fieldList = line.split(",")
        isoDateTime = fieldList[0]
        yield getFilename(isoDateTime)

#*********************************************
#test the generator
#*********************************************
#No code in SpecialFileGenerator is executed at this point
filegen = SpecialFileGenerator(inputWuActualWeatherForAWeek)

#You will see this line printed first
print("filegen created")

#The function is now executed from top
#and gets suspended after the first row
#as it encounters the yield after the first row
for line in filegen:
    print("from filegen:", line)

satya - 10/1/2019, 12:03:45 PM

the enumerate function's index is 0 based: fyi

the enumerate function's index is 0 based: fyi