Re: [Python-Dev] itertools addition: getitem()

2007-07-11 Thread Walter Dörwald
Giovanni Bajo wrote: > On 09/07/2007 21.23, Walter Dörwald wrote: > >> >>> from ll.xist import parsers, xfind >> >>> from ll.xist.ns import html >> >>> e = parsers.parseURL("http://www.python.org";, tidy=True) >> >>> print e.walknode(html.h2 & xfind.hasclass("news"))[-1] >> Google Adds Python

Re: [Python-Dev] itertools addition: getitem()

2007-07-10 Thread Giovanni Bajo
On 09/07/2007 21.23, Walter Dörwald wrote: > >>> from ll.xist import parsers, xfind > >>> from ll.xist.ns import html > >>> e = parsers.parseURL("http://www.python.org";, tidy=True) > >>> print e.walknode(html.h2 & xfind.hasclass("news"))[-1] > Google Adds Python Support to Google Calendar Dev

Re: [Python-Dev] itertools addition: getitem()

2007-07-09 Thread Walter Dörwald
Raymond Hettinger wrote: > From: "Guido van Rossum" <[EMAIL PROTECTED]> >> But doesn't the very same argument also apply against islice(), which >> you just offered as an alternative? > > Not really. The use cases for islice() typically do not involve > repeated slices of an iterator unless it i

Re: [Python-Dev] itertools addition: getitem()

2007-07-09 Thread Raymond Hettinger
From: "Guido van Rossum" <[EMAIL PROTECTED]> > But doesn't the very same argument also apply against islice(), which > you just offered as an alternative? Not really. The use cases for islice() typically do not involve repeated slices of an iterator unless it is slicing off the front few elements

Re: [Python-Dev] itertools addition: getitem()

2007-07-09 Thread Walter Dörwald
Guido van Rossum wrote: > On 7/9/07, Raymond Hettinger <[EMAIL PROTECTED]> wrote: >> Also, as a practical matter, I think it is a bad idea to introduce >> __getitem__ style access to itertools because the starting point >> moves with each consecutive access: >> >> # access items 0, 2, 5, 9, 14,

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Guido van Rossum
On 7/9/07, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > Also, as a practical matter, I think it is a bad idea to introduce > __getitem__ style access to itertools because the starting point > moves with each consecutive access: > > # access items 0, 2, 5, 9, 14, 20, ... > for i in range(1

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Walter Dörwald
Raymond Hettinger wrote: > [Walter Dörwald] >> I'd like to propose the following addition to itertools: A function >> itertools.getitem() which is basically equivalent to the following >> python code: >> >> _default = object() >> >> def getitem(iterable, index, default=_default): >>try: >>

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Raymond Hettinger
[Walter Dörwald] > I'd like to propose the following addition to itertools: A function > itertools.getitem() which is basically equivalent to the following > python code: > > _default = object() > > def getitem(iterable, index, default=_default): >try: > return list(iterable)[index] >

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Walter Dörwald
Guido van Rossum wrote: > On 7/8/07, Walter Dörwald <[EMAIL PROTECTED]> wrote: > [quoting Guido] >> > But I still want to hear of a practical use case for the default here. >> >> In most cases >> >> foo = getitem(iterable, 0, None) >> if foo is not None: >>... >> >> is simpler than

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Steven Bethard
On 7/8/07, Kevin Jacobs <[EMAIL PROTECTED]> <[EMAIL PROTECTED]> wrote: > Also vaguely apropos: > > def ilen(seq): > 'Return the length of the hopefully finite sequence' > n = 0 > for x in seq: > n += 1 > return n Also known as:: sum(1 for _ in iterable) That's always been simple

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Kevin Jacobs <[EMAIL PROTECTED]>
On 7/8/07, Guido van Rossum <[EMAIL PROTECTED]> wrote: Ahem. I hope you have a better use case for getitem() than that (regardless of the default issue). I find it clearer to write that as try: compid = root[ns.company_id].next() except StopIteration: compid = None else: compid = int(comp

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Guido van Rossum
On 7/8/07, Walter Dörwald <[EMAIL PROTECTED]> wrote: [quoting Guido] > > But I still want to hear of a practical use case for the default here. > > In most cases > > foo = getitem(iterable, 0, None) > if foo is not None: >... > > is simpler than: > > try: >foo = getitem(

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Walter Dörwald
Guido van Rossum wrote: > On 7/8/07, Georg Brandl <[EMAIL PROTECTED]> wrote: >> Guido van Rossum schrieb: >>> How important is it to have the default in this API? __getitem__() >>> doesn't have a default; instead, there's a separate API get() that >>> provides a default (and I find defaulting to No

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Guido van Rossum
On 7/8/07, Georg Brandl <[EMAIL PROTECTED]> wrote: > Guido van Rossum schrieb: > > How important is it to have the default in this API? __getitem__() > > doesn't have a default; instead, there's a separate API get() that > > provides a default (and I find defaulting to None more manageable than > >

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Georg Brandl
Guido van Rossum schrieb: > How important is it to have the default in this API? __getitem__() > doesn't have a default; instead, there's a separate API get() that > provides a default (and I find defaulting to None more manageable than > the "_default = object()" pattern). getattr() has a default

Re: [Python-Dev] itertools addition: getitem()

2007-07-08 Thread Guido van Rossum
How important is it to have the default in this API? __getitem__() doesn't have a default; instead, there's a separate API get() that provides a default (and I find defaulting to None more manageable than the "_default = object()" pattern). --Guido On 7/8/07, Walter Dörwald <[EMAIL PROTECTED]> wr