[Python-Dev] itertools addition: getitem()

Giovanni Bajo rasky at develer.com
Wed Jul 11 01:01:07 CEST 2007


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 Developer's Guide
> 
> 
> Get the first comment line from a python file:
> 
>  >>> getitem((line for line in open("Lib/codecs.py") if 
> line.startswith("#")), 0)
> '### Registry and builtin stateless codec functions\n'
> 
> 
> Create a new unused identifier:
> 
>  >>> def candidates(base):
> ...     yield base
> ...     for suffix in count(2):
> ...         yield "%s%d" % (base, suffix)
> ...
>  >>> usedids = set(("foo", "bar"))
>  >>> getitem((i for i in candidates("foo") if i not in usedids), 0)
> 'foo2'

You keep posting examples where you call your getitem() function with "0" as 
index, or -1.

getitem(it, 0) already exists and it's spelled it.next(). getitem(it, -1) 
might be useful in fact, and it might be spelled last(it) (or it.last()). Then 
one may want to add first() for simmetry, but that's it:

first(i for i in candidates("foo") if i not in usedids)
last(line for line in open("Lib/codecs.py") if line[0] == '#')

Are there real-world use cases for getitem(it, n) with n not in (0, -1)? I 
share Raymond's feelings on this. And by the way, if you wonder, I have these 
exact feelings as well for islice... :)
-- 
Giovanni Bajo




More information about the Python-Dev mailing list