Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
What to do with a huge crop of apples

Last year, you had a good crop of apples on your tree .... what did you do with them? Make apple pies for all the neighbours! How?

• You collect all the apples and bring them into the kitchen.
• You prepare the apples ready for the pies.
• You make the pastry and apple pies.
• You cook the pies
• You distribute them.

This year, you had a huge crop of apples, and you wanted to make apple pies ... not for all the neighbours, but for the whole town. How did you do that? The same way that you did last year? Alas, no. There were far too many appled to bring into the kitchen, all your bowls would have been filled with apples long before you had prepared them all, and your oven has only got limited capacity. Far better to collect a few, prepare a few, make a few pies, and distribute them ... and keep repeating that until you have fed the whole town.


Which is what a generator function does in Python. When you call a generator function (as the list provider in a for statement) in Python, it will yield when it has the first result available, which you can then process. Come back to the for statement, and it will continue to run the generator code ('pick the next batch of apples') and return the next result, time after time, until you're done.

Here's an example that's doing just this - opening a file in a generator function, and returning at each call the next line containing the word "Python". The loop in the main code is then extracting the person's name from the line, printing it out, and going back to the generator for the next "Python" line.

def tap(file):
  fh = open (file,"r")
  for line in fh.xreadlines():
    if (line.find("Python") > 0):
      yield line
 
# Following line just added to show that the tap
# method returns a generator
print tap("../requests.xyz")
for staff in tap("../requests.xyz"):
  els = staff.split(" ")
  print els[0]


Here is the sample output:

earth-wind-and-fire:~/nov08 grahamellis$ python dfg
<generator object at 0x414e0>
hazel
leane
olivia
adam
barry
harry
ken
nigel
rupert
earth-wind-and-fire:~/nov08 grahamellis$


See here for full source code of this example and here for details of our Python course
(written 2008-11-04 18:43:46)

 
Associated topics are indexed under
R104 - Ruby - Control Structures
U114 - Lua - Threading and Coroutines.
Y105 - Python - Functions, Modules and Packages

Back to
Anonymous functions (lambdas) and map in Python
Previous and next
or
Horse's mouth home
Forward to
Optional and named parameters in Python
Some other Articles
Is it worth it?
List Comprehensions in Python
Barack Obama wins US Predidency
Optional and named parameters in Python
What to do with a huge crop of apples
Anonymous functions (lambdas) and map in Python
Liverpool - a friendly city
Domain Renewal Group
What a difference a day made
Debugging and Data::Dumper in Perl
2259 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho