Re: beginner, idiomatic python

2007-08-27 Thread Paul Rubin
Neil Cerutti <[EMAIL PROTECTED]> writes: > i = 0 > while i < self.parent.GetPageCount(): > # do stuff > i += 1 Alternatively: from itertools import count for i in count(): if i >= self.parent.GetPageCount(): break ... -- http://mail.python.org/mailman/listinfo

Re: beginner, idiomatic python

2007-08-27 Thread Neil Cerutti
On 2007-08-27, Neil Cerutti <[EMAIL PROTECTED]> wrote: > This sort of suggests a direct solution: > > for i in xrange(self.parent.GetPageCount()): > if i >= self.parent.GetPageCount(): > break > # do stuff > > At least that way you're spared the manual manipulation of i. On second thought,

Re: beginner, idiomatic python

2007-08-27 Thread Neil Cerutti
On 2007-08-27, bambam <[EMAIL PROTECTED]> wrote: > Thank you, I have been through the tutorial several times, I > guess I'm just not smart enough. Perhaps I have been led astray > by what I read here? > > My code started like this: > > for i in range(self.parent.GetPageCount()): > > I was asked: >

Re: beginner, idiomatic python

2007-08-27 Thread Bruno Desthuilliers
bambam a écrit : > Thank you, I have been through the tutorial several times, I guess > I'm just not smart enough. Perhaps I have been led astray by what > I read here? > > My code started like this: > > for i in range(self.parent.GetPageCount()): > > I was asked: > >> Does page count change? i

Re: beginner, idiomatic python

2007-08-26 Thread bambam
Thank you. I figured the set would probably be faster, but the lists are small, and I'm concerned that the code is going to look Byzantine if I keep swapping between lists, sets and dictionaries :~). At the moment there are no sets or dictionaries in the entire code base I am working with. I'm not

Re: beginner, idiomatic python

2007-08-26 Thread bambam
Thank you. Steve. "Alex Martelli" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > bambam <[EMAIL PROTECTED]> wrote: > >> Is it safe to write >> >> A = [x for x in A if x in U] >> >> or is that undefined? I understand that the slice operation > > It's perfectly safe and well-defined

Re: beginner, idiomatic python

2007-08-26 Thread Paul Rubin
"bambam" <[EMAIL PROTECTED]> writes: > Is it safe to write > A = [x for x in A if x in U] > or is that undefined? I understand that the slice operation > can be used to make a temporary copy, so I could write > A=[x for x in A[:] if x in U] > but I've just copied that without any understanding. Yo

Re: beginner, idiomatic python

2007-08-26 Thread Alex Martelli
bambam <[EMAIL PROTECTED]> wrote: ... > Bags don't seem to be built in to my copy of Python, and A "bag" is a collections.defaultdict(int) [[you do have to import collections -- it's in the standard library, NOT built-in]]. Alex -- http://mail.python.org/mailman/listinfo/python-list

Re: beginner, idiomatic python

2007-08-26 Thread Alex Martelli
bambam <[EMAIL PROTECTED]> wrote: > Is it safe to write > > A = [x for x in A if x in U] > > or is that undefined? I understand that the slice operation It's perfectly safe and well-defined, as the assignment rebinds the LHS name only AFTER the RHS list comprehension is done. Alex -- http://

Re: beginner, idiomatic python

2007-08-26 Thread bambam
Is it safe to write A = [x for x in A if x in U] or is that undefined? I understand that the slice operation can be used to make a temporary copy, so I could write A=[x for x in A[:] if x in U] but I've just copied that without any understanding. Steve. "bambam" <[EMAIL PROTECTED]> wrote in

Re: beginner, idiomatic python

2007-08-26 Thread bambam
Thank you, I have been through the tutorial several times, I guess I'm just not smart enough. Python is quite different from the languages I am familiar with. My code sample started like this: >>for i in range(self.parent.GetPageCount()): I was asked: >Does page count change? i.e. is it necessa

Re: beginner, idiomatic python

2007-08-26 Thread bambam
Thank you, I have been through the tutorial several times, I guess I'm just not smart enough. Perhaps I have been led astray by what I read here? My code started like this: for i in range(self.parent.GetPageCount()): I was asked: >Does page count change? i.e. is it necessary to retrieve it in e

Re: beginner, idiomatic python

2007-08-26 Thread Scott David Daniels
bambam wrote: > That is, is it defined what Python does for > for i in f() > I'm sure it must be, but I haven't seen it yet. If I have > a user defined function returning a range, is it defined > that the range function is called on every loop? If I > have a function returning a range taking a

Re: beginner, idiomatic python

2007-08-26 Thread Gabriel Genellina
En Sun, 26 Aug 2007 22:58:35 -0300, bambam <[EMAIL PROTECTED]> escribi�: > Ok, many environments are capable of cached evaluation > of functions without variable parameters so > range(5) > is cached, but > range(v) is re-evaluated every time. Is this defined > behaviour? The range builtin

Re: beginner, idiomatic python

2007-08-26 Thread bambam
Ok, many environments are capable of cached evaluation of functions without variable parameters so range(5) is cached, but range(v) is re-evaluated every time. Is this defined behaviour? That is, is it defined what Python does for for i in f() I'm sure it must be, but I haven't seen it

Re: beginner, idiomatic python

2007-08-26 Thread bambam
c = sorted(set(a)-set(b)) although for me :~( that is another step more obscure than c = list(set(a)-set(b)) c.sort() Bags don't seem to be built in to my copy of Python, and although I'm interested in why lists don't support the difference operation, I don't want to get away from standard Pyth

Re: beginner, idiomatic python

2007-08-26 Thread bambam
That looks good, and perhaps a difference operator would be too simple to be useful anyway. Steve. "Mikael Olofsson" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > > > bambam wrote: >> >> In this case it doesn't matter - my lists don't contain >> duplicate elements this time - but

Re: beginner, idiomatic python

2007-08-25 Thread Paul McGuire
On Aug 23, 11:50 pm, "bambam" <[EMAIL PROTECTED]> wrote: > Thank you, so generallizing: > > (1) Python re-evaluates the loop range on every loop, and > (2) Python does short-circuit evaluation of conditions, in predictable > order. > > Sorry about the bad question. > A beginner would do well to wo

Re: beginner, idiomatic python

2007-08-25 Thread Scott David Daniels
bambam wrote: >> The reason that lists don't have set-like methods is because >> lists aren't sets -- lists can contain duplicate elements and they are ordered. I'd have used sets if I was sure you meant [1,2,3] to mean the same thing as [3,1,2] and no duplicates. > Interesting point -- if that's

Re: beginner, idiomatic python

2007-08-24 Thread Mikael Olofsson
bambam wrote: > > In this case it doesn't matter - my lists don't contain > duplicate elements this time - but I have worked with lists in > money market and in inventory, and finding the intersection > and difference for matching off and netting out are standard > operations. I would use a list

Re: beginner, idiomatic python

2007-08-24 Thread bambam
> This isn't a "cast" in the sense of some less-strongly-typed languages; > it's just a conversion. The `list` function/type iterates over its > argument and turns it into a list. Sets are iterable, so that's all > that's really going on here. oh :~). Learning experience happening here... Tha

Re: beginner, idiomatic python

2007-08-23 Thread Erik Max Francis
bambam wrote: > Excellent. By symmetry, I see that "list" casts the set back into a list. > > I wonder why list has not been extended with the same (difference, > interesection) methods? Casting to set looks a little kludgy: > > c = list(set(a)-set(b)) > I wonder if that is clearer than the exp

Re: beginner, idiomatic python

2007-08-23 Thread bambam
Excellent. By symmetry, I see that "list" casts the set back into a list. I wonder why list has not been extended with the same (difference, interesection) methods? Casting to set looks a little kludgy: c = list(set(a)-set(b)) I wonder if that is clearer than the explicit loop? Steve. "Gabrie

Re: beginner, idiomatic python

2007-08-23 Thread bambam
Wos! Several different thoughts: An object using yield to return only the relevant pages, one at a time. Pop to remove the items from the list. A dictionary to map between the strings and the integers. The dictionary was particularly unexpected. Eventually, I plan to change the string ports to de

Re: beginner, idiomatic python

2007-08-23 Thread bambam
Thank you, so generallizing: (1) Python re-evaluates the loop range on every loop, and (2) Python does short-circuit evaluation of conditions, in predictable order. Sorry about the bad question. "Zentrader" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > Does page count change?

Re: beginner, idiomatic python

2007-08-23 Thread Gabriel Genellina
En Thu, 23 Aug 2007 23:54:14 -0300, bambam <[EMAIL PROTECTED]> escribi�: > After examining your suggestion, I realised that another thing > I am interested in could be generalised: I want the complement > of the set of ports in pages, given a universal set in tempList. > Ignoring the break conditi

Re: beginner, idiomatic python

2007-08-23 Thread bambam
Wos! Several different thoughts: An object using yield to return only the relevant pages, one at a time. Pop to remove the items from the list. A dictionary to map between the strings and the integers. The dictionary was particularly unexpected. Eventually, I plan to change the string ports to de

Re: beginner, idiomatic python

2007-08-23 Thread Zentrader
Does page count change? i.e. is it necessary to retrieve it in every loop or tempList = ['1','2','3','4','5','6','7','8'] sampleList=[] page_count = self.parent.GetPageCount() for i in range(page_count): Also, once pagefound is set to True, all pages following will not be appended to sampleL

Re: beginner, idiomatic python

2007-08-23 Thread Scott David Daniels
bambam wrote: > Would someone like to suggest a replacement for this? It works ok, > but it doesn't look like any of the other code: > > tempList = ['1','2','3','4','5','6','7','8'] > sampleList=[] > for port in tempList: > pagefound = False > for i in range(self.parent.GetPageCount()): >

beginner, idiomatic python

2007-08-23 Thread bambam
Would someone like to suggest a replacement for this? It works ok, but it doesn't look like any of the other code: tempList = ['1','2','3','4','5','6','7','8'] sampleList=[] for port in tempList: pagefound = False for i in range(self.parent.GetPageCount()): page=self.parent.GetPage