Python Yield – Make the firehose a little trickle of data

rice-yield-may-decline-by-10pc-1352410048-8056Yield!

Wow – I saw this three line script at gis.stackexchange.com that will list all data in a File Geodatabase and was impressed! Three lines! A thing of beauty! When I looked closer, I saw a yield tool that I wasn’t familiar with, so I set about learning what the heck it is.

Here is a useful explanation: http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained of what is going on.

 

 

def listFcsInGDB():
    ''' set your arcpy.env.workspace to a gdb before calling '''
    for fds in arcpy.ListDatasets('','feature') + ['']:
        for fc in arcpy.ListFeatureClasses('','',fds):
            yield os.path.join(arcpy.env.workspace, fds, fc)

Turns out, it’s like a list, but just feeds your script one item at a time. You can go ahead and generate giant lists and not worry about overloading what ever you are doing. It’s great for this kind of setting where you don’t know how much data is going to be listed.

I haven’t personally used this yet, but wow, I hope I get too because I’ll feel like I moved up a notch on the ladder of Python. Let me know if you have success!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s