Re: how can this iterator be optimized?

2009-02-16 Thread Basilisk96
On Feb 14, 10:19 am, Paul McGuire pt...@austin.rr.com wrote: If func is expensive, you could try memoizing it.  Then subsequent calls just do arg lookups.  Michele Simianato has posted a good memoizing decorator on the Python wiki. -- Paul That's the trick! I almost forgot about that one.

Re: how can this iterator be optimized?

2009-02-14 Thread josh logan
On Feb 13, 7:44 pm, Basilisk96 basilis...@gmail.com wrote: On Feb 12, 1:15 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: I usually strive for comprehensions if a for loop can be reduced to such. Any particular reason? Only two. 1.) I was impressed by their clarity

Re: how can this iterator be optimized?

2009-02-14 Thread Paul McGuire
On Feb 11, 7:22 pm, Basilisk96 basilis...@gmail.com wrote: ... where func is a single-argument function that returns either a string or None, but is an expensive call. I am pretty sure that the sorted() construct cannot be improved much further, but... ...does anyone have ideas on improving

Re: how can this iterator be optimized?

2009-02-13 Thread Basilisk96
On Feb 12, 1:15 am, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: I usually strive for comprehensions if a for loop can be reduced to such. Any particular reason? Only two. 1.) I was impressed by their clarity and conciseness when I first discovered them. 2.) I also read now

Re: how can this iterator be optimized?

2009-02-12 Thread josh logan
On Feb 11, 8:22 pm, Basilisk96 basilis...@gmail.com wrote: Hello all, I have the following function that uses an intermediate iterator rawPairs: def MakePairs(path):     import os     import operator     join = os.path.join     rawPairs = (         (join(path, s), func(s))         for

how can this iterator be optimized?

2009-02-11 Thread Basilisk96
Hello all, I have the following function that uses an intermediate iterator rawPairs: def MakePairs(path): import os import operator join = os.path.join rawPairs = ( (join(path, s), func(s)) for s in os.listdir(path) if func(s) is not None and

Re: how can this iterator be optimized?

2009-02-11 Thread Gabriel Genellina
En Wed, 11 Feb 2009 23:22:26 -0200, Basilisk96 basilis...@gmail.com escribió: def MakePairs(path): import os import operator join = os.path.join rawPairs = ( (join(path, s), func(s)) for s in os.listdir(path) if func(s) is not None and

Re: how can this iterator be optimized?

2009-02-11 Thread Basilisk96
Don't use a generator then. If you're going to finally return a list (and   sorted does exactly that), build a list right from the start: Good point. However, for the sake of argument, what if I removed the sorting requirement and simply wanted the generator? I usually strive for comprehensions

Re: how can this iterator be optimized?

2009-02-11 Thread Terry Reedy
Basilisk96 wrote: Don't use a generator then. If you're going to finally return a list (and sorted does exactly that), build a list right from the start: Good point. However, for the sake of argument, what if I removed the sorting requirement and simply wanted the generator? Write a

Re: how can this iterator be optimized?

2009-02-11 Thread Steven D'Aprano
On Wed, 11 Feb 2009 20:26:12 -0800, Basilisk96 wrote: Don't use a generator then. If you're going to finally return a list (and sorted does exactly that), build a list right from the start: Good point. However, for the sake of argument, what if I removed the sorting requirement and simply