Re: [Tutor] What is this an example of (and how can i use it?)
On Wed, Sep 23, 2009 at 12:58 AM, kevin parks wrote: > It appears it is not so impenetrable as i initially > though. Well iterators > aren't maybe, but generator do look tricky. So interators iterate over > lists, tuples, strings, dictionaries > and any data type that is iterable, and generators are ways to make new > iterables? Exactly. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sep 21, 2009, at 9:52 AM, Kent Johnson wrote: Calling a generator function gives you something that can be iterated. You can create a list out of it (by passing it to the list() function) or you can iterate the items in it directly with a for loop. Using the example above, you could say for item in roundrobin('abc', [], range(4), (True,False)): print item I kinda understand conceptually what iterators and generators do and why they are "a honking good idea" (why create 100 of x when we just want the 100th, etc.) what i don't get is the syntax and how they are used in real life. How generator and iterators behave in the wild. It's really not that bad. They are just a generalization of what you have already been doing with lists. Even the Lutz is too terse and generally poor on these two complex and relatively new constructs. They are a dark and obscure magic. No, really they are not difficult. Read my essay and ask questions if you don't understand. Thanks. I have some time today and will read up on what you sent me and revisit the lutz and other docs. It appears it is not so impenetrable as i initially though. Well iterators aren't maybe, but generator do look tricky. So interators iterate over lists, tuples, strings, dictionaries and any data type that is iterable, and generators are ways to make new iterables? Anyway, i will brew some coffee and hit those links. Thanks, -kevin ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sun, Sep 20, 2009 at 1:27 PM, kevin parks wrote: > > On Sep 21, 2009, at 1:32 AM, Alan Gauld wrote: > >> kevin parks wrote: >> >>> called, and what it is an example of. I guess there are generators and >>> iterators now and it seems this might be an example of one of those new >> >> This is a generator expression. > > That's unfortunate news for me. Why? > So... then to call (by call i mean use/execute/doit) i would do, what? > foo.next() Are you asking how to use a generator function? There is a simple example on the recipe page (sorry, I omitted the link earlier) http://code.activestate.com/recipes/528936/ >>> list(roundrobin('abc', [], range(4), (True,False))) ['a', 0, True, 'b', 1, False, 'c', 2, 3] Calling a generator function gives you something that can be iterated. You can create a list out of it (by passing it to the list() function) or you can iterate the items in it directly with a for loop. Using the example above, you could say for item in roundrobin('abc', [], range(4), (True,False)): print item > I kinda understand conceptually what iterators and generators do and why > they are "a honking good idea" (why create 100 of x when we just want the > 100th, etc.) what i don't get is the syntax and how they are used in real > life. How generator and iterators behave in the wild. It's really not that bad. They are just a generalization of what you have already been doing with lists. > Even the Lutz is too > terse and generally poor on these two complex and relatively new constructs. > They are a dark and obscure magic. No, really they are not difficult. Read my essay and ask questions if you don't understand. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
kevin parks wrote: This is a generator expression. That's unfortunate news for me. It is like a list comprehension (you know about those right?) Yes. I know and use and love them daily. If you can grok comprehensions then you are only a step away from generator expressions. Most of the time you don't need to think about the hairy CS stuff underneath, just use them. I found the jump to using comprehensions much bigger than the jump from them to generator expressions! You could think of a list as a list constructed using a generator expression. should have been a list comprehension as a list nexts = cycle(iter(it).next for it in iterables) note this is storing the next methods not the results of them. while pending: for next in nexts: yield next() So the yield calls the stored method and returns the result. So... then to call (by call i mean use/execute/doit) i would do, what? foo.next() That depends on what next() actually returns. e started off with a list of iterables (eg files strings, lists, tuples - possibly a mixture of them. We then build a list of their next methods. So when we call this methods we get back an instance of whatever was in the original list of iterators. Hee isa slightly less complex example: >>> its = [[1,2,3],'abc'] >>> nxts = [iter(n).next for n in its] >>> for repeats in range(2): for n in nxts: print n() 1 a 2 b So the first for n loop printed out the first item in each iterable, the next time round we printed the second item etc. Each iteration prints a number and a letter So when your yield returns its value you have to do with it whatever is appropriate! I kinda understand conceptually what iterators and generators do and why they are "a honking good idea" (why create 100 of x when we just want the 100th, etc.) what i don't get is the syntax and how they are used in real life. ignore the memory management magic and just think of them returning the next item in a list on demand. Have you ever used xrange() instead of range()? Its the same thing. bummed since generators have methods, which means they are OO So do lists but you said you use lists every day! You use objects everywhere in Python, everything is an object and has methods. try: >>> dir(6) don't be freaked by objects, you use them already every day. means i am i'd be in for 16 years of computer science study and super arcane, obscure and opaque concepts like what to do with __self__ and You can do all sorts of clever black magic type stuff in Python even without OOP but mostly you don't need to, just keep it simple. Anyway i needed a pea shooter that does a round robin. This one does it, but i don't know how to use it. Actually the example you quoted is a fairly sophisticated bit of code that probably does more than you need. What exactly are you trying to achieve, there is probably an easier way! I read up on gennies and itties and see if i can get my head around it. They are really poorly addressed nearly everywhere i look. They are explained so that really smart folks who know a lot of CS and are fluent in 15 computer languages can understand them, but not us mortals. Even They are difficult conceptually, but if you concentrate on the examples of use and don't try to be too ambitious initially they will grow on you like comprehensions did. And remember, you never need them. There are always alternative ways to do it, just using a bit more code usually. HTH, Alan G. http://www.alan-g.me.uk ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sep 21, 2009, at 1:32 AM, Alan Gauld wrote: kevin parks wrote: called, and what it is an example of. I guess there are generators and iterators now and it seems this might be an example of one of those new This is a generator expression. That's unfortunate news for me. It is like a list comprehension (you know about those right?) Yes. I know and use and love them daily. Even if there were implemented backwards :) [for x in range(10) x**2] would have been easier than: [x**2 for x in range(10)] But i am used to it now. except it doesn't create the list it just returns each item on demand. You could think of a list as a list constructed using a generator expression. def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) note this is storing the next methods not the results of them. while pending: try: for next in nexts: yield next() So the yield calls the stored method and returns the result. So... then to call (by call i mean use/execute/doit) i would do, what? foo.next() I kinda understand conceptually what iterators and generators do and why they are "a honking good idea" (why create 100 of x when we just want the 100th, etc.) what i don't get is the syntax and how they are used in real life. How generator and iterators behave in the wild. I am also bummed since generators have methods, which means they are OO which means i am i'd be in for 16 years of computer science study and super arcane, obscure and opaque concepts like what to do with __self__ and all that junk before i can use them. Anyway i needed a pea shooter that does a round robin. This one does it, but i don't know how to use it. I read up on gennies and itties and see if i can get my head around it. They are really poorly addressed nearly everywhere i look. They are explained so that really smart folks who know a lot of CS and are fluent in 15 computer languages can understand them, but not us mortals. Even the Lutz is too terse and generally poor on these two complex and relatively new constructs. They are a dark and obscure magic. I'll try the links Kent pointed me to first and see how that goes. thanks, -kp-- ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
kevin parks wrote: called, and what it is an example of. I guess there are generators and iterators now and it seems this might be an example of one of those new This is a generator expression. It is like a list comprehension (you know about those right?) except it doesn't create the list it just returns each item on demand. You could think of a list as a list constructed using a generator expression. def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) note this is storing the next methods not the results of them. while pending: try: for next in nexts: yield next() So the yield calls the stored method and returns the result. HTH, Alan G. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] What is this an example of (and how can i use it?)
On Sun, Sep 20, 2009 at 9:10 AM, kevin parks wrote: > I am afraid that in the long layoff in python has meant some new constructs > have passed me by. In googling around I found some nice little code I want > to use, but i don't quite understand it, how it is called, and what it is an > example of. I guess there are generators and iterators now and it seems this > might be an example of one of those new constructs. Can anyone explain what > it is i am looking at, how it is called, and what it is an example of so > that I can look it up: > > def roundrobin(*iterables): > "roundrobin('ABC', 'D', 'EF') --> A D E B F C" > # Recipe credited to George Sakkis The original is here, with an explanation of what it does. > pending = len(iterables) > nexts = cycle(iter(it).next for it in iterables) cycle() is part of itertools: http://docs.python.org/library/itertools.html#itertools.cycle You can read about iter() and iterators here: http://docs.python.org/library/functions.html#iter http://docs.python.org/glossary.html#term-iterator (iter(it).next for it in iterables) is a generator expression. It creates an iterator. The above statement as a whole makes an iterator which will return the iterators of the arguments in turn, repeatedly. > while pending: > try: > for next in nexts: > yield next() The yield statement makes this into a generator function. It's return value is a generator - a kind of iterator. > except StopIteration: next() will raise StopIteration when its underlying iterable is exhausted. > pending -= 1 > nexts = cycle(islice(nexts, pending)) This is kind of tricky - it makes a new cycle of iterators that omits the one that just finished. I have a writeup of iterators and generators here: http://personalpages.tds.net/~kent37/kk/4.html HTH, Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] What is this an example of (and how can i use it?)
I am afraid that in the long layoff in python has meant some new constructs have passed me by. In googling around I found some nice little code I want to use, but i don't quite understand it, how it is called, and what it is an example of. I guess there are generators and iterators now and it seems this might be an example of one of those new constructs. Can anyone explain what it is i am looking at, how it is called, and what it is an example of so that I can look it up: def roundrobin(*iterables): "roundrobin('ABC', 'D', 'EF') --> A D E B F C" # Recipe credited to George Sakkis pending = len(iterables) nexts = cycle(iter(it).next for it in iterables) while pending: try: for next in nexts: yield next() except StopIteration: pending -= 1 nexts = cycle(islice(nexts, pending)) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor