Re: help with generators

2005-05-19 Thread Steven Bethard
George Sakkis wrote: > "Steven Bethard" wrote: > >>py> def bin(n): >>... s = [] >>... def bin(n): >>... if n == 0: >>... yield s >>... else: >>... s.append(0) >>... for s1 in bin(n - 1): >>... yield s1 >>... s.p

Re: help with generators

2005-05-19 Thread Mayer
Thanks a lot! This clarified [I think] my misunderstanding about yield, and I also learned something about efficiency from George's code -- Thanks. So, The function tel(aString) takes a string (or a number) that denote a phone number, using digits or letters, and returns a generator for the set of

Re: help with generators

2005-05-19 Thread George Sakkis
"Steven Bethard" wrote: > It would help if you explained what you expected. But here's code that > prints about the same as your non-generator function. > > py> def bin(n): > ... s = [] > ... def bin(n): > ... if n == 0: > ... yield s > ... else: > ...

Re: help with generators

2005-05-18 Thread Steven Bethard
Mayer wrote: > Hello: > > I need some help in understanding generators. I get them to work in > simple cases, but the following example puzzles me. Consider the > non-generator, "ordinary" procedure: > > def foo(n): > s = [] > def foo(n): > if n == 0: > print s >

help with generators

2005-05-18 Thread Mayer
Hello: I need some help in understanding generators. I get them to work in simple cases, but the following example puzzles me. Consider the non-generator, "ordinary" procedure: def foo(n): s = [] def foo(n): if n == 0: print s else: s.append(0)

Re: guarding for StopIteration (WAS: Help with generators outside of loops.)

2004-12-08 Thread David Eppstein
In article <[EMAIL PROTECTED]>, Steven Bethard <[EMAIL PROTECTED]> wrote: > David Eppstein wrote: > > I've made it a policy in my own code to always surround explicit calls > > to next() with try ... except StopIteration ... guards. > > > > Otherwise if you don't guard the call and you get an u

Re: Help with generators outside of loops.

2004-12-08 Thread David Eppstein
In article <[EMAIL PROTECTED]>, Andrea Griffini <[EMAIL PROTECTED]> wrote: > Isn't the handling of StopIteration confined in the very moment of > calling .next() ? This was what I expected... and from a simple test > looks also what is happening... Not if someone farther back in the call chain i

Re: Help with generators outside of loops.

2004-12-08 Thread Steven Bethard
Christopher J. Bottaro wrote: Wow, good advice. One question, how is the generator class implemented so that if assigned to a tuple/list, it knows what to do? Is it possible to overload the assignment operator kinda like in C++? Tuple unpacking works with generators because generators implement t

Re: Help with generators outside of loops.

2004-12-08 Thread Christopher J. Bottaro
Steven Bethard wrote: > I don't do much with SQL/databases stuff, but if you really know the > result will be a single row, you can take advantage of tuple unpacking > and do something like: > > row, = obj.ExecSQLQuery(sql, args) > > or > > [row] = obj.ExecSQLQuery(sql, args) > > This has the a

Re: guarding for StopIteration (WAS: Help with generators outside of loops.)

2004-12-08 Thread Steven Bethard
Steven Bethard wrote: Just to clarify here, the only time code raising a StopIteration will cause a for-loop to exit silently is if the StopIteration is raised in an __iter__ method, e.g.: That was a little imprecise. What I should have said is "the only time code raising a StopIteration will c

guarding for StopIteration (WAS: Help with generators outside of loops.)

2004-12-08 Thread Steven Bethard
David Eppstein wrote: I've made it a policy in my own code to always surround explicit calls to next() with try ... except StopIteration ... guards. Otherwise if you don't guard the call and you get an unexpected exception from the next(), within a call chain that includes a for-loop over anoth

Re: Help with generators outside of loops.

2004-12-08 Thread Andrea Griffini
David Eppstein wrote: In article <[EMAIL PROTECTED]>, "Robert Brewer" <[EMAIL PROTECTED]> wrote: But I'm guessing that you can't index into a generator as if it is a list. row = obj.ExecSQLQuery(sql, args).next() I've made it a policy in my own code to always surround explicit calls to next()

Re: Help with generators outside of loops.

2004-12-07 Thread Steven Bethard
Robert Brewer wrote: Christopher J. Bottaro wrote: I have a generator that works like this: for row in obj.ExecSQLQuery(sql, args): # process the row Now there are some querys that run where I know the result will only be a single row. Is there anyway to get that single row from the genera

Re: Help with generators outside of loops.

2004-12-07 Thread David Eppstein
In article <[EMAIL PROTECTED]>, "Robert Brewer" <[EMAIL PROTECTED]> wrote: > > But I'm guessing that you can't index into a generator as if > > it is a list. > > row = obj.ExecSQLQuery(sql, args).next() I've made it a policy in my own code to always surround explicit calls to next() with try

RE: Help with generators outside of loops.

2004-12-07 Thread Robert Brewer
Christopher J. Bottaro wrote: > I have a generator that works like this: > > for row in obj.ExecSQLQuery(sql, args): > # process the row > > Now there are some querys that run where I know the result > will only be a > single row. Is there anyway to get that single row from the generato

Help with generators outside of loops.

2004-12-07 Thread Christopher J. Bottaro
I have a generator that works like this: for row in obj.ExecSQLQuery(sql, args): # process the row Now there are some querys that run where I know the result will only be a single row. Is there anyway to get that single row from the generator without having to use it in a for loop? I wa