Re: [PATCH] Re: frozenset() without arguments should return a singleton

2005-02-13 Thread Raymond Hettinger
ods, both set() and frozenset() share the same underlying code and data structure. In this respect, they differ from list() and tuple() which have no shared code and has two substantially different data structures. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: [PATCH] Re: frozenset() without arguments should return a singleton

2005-02-12 Thread Raymond Hettinger
ribly opposed to the idea. I just find the case for it to be somewhat weak. Also, I'm not sure it warrants the effort, the code clutter, or introducing issues like having a semantic difference between the result of frozenset() and the result of frozenset([]). Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: listerator clonage

2005-02-12 Thread Raymond Hettinger
for elem in s: bag[elem] = bag.get(elem, 0) + 1 print [elem for elem, count in bag.iteritems() if count>1] Raymond Hettinger P.S. Extra credit problem: make the itertools solution output values in the order seen. -- http://mail.python.org/mailman/listinfo/python-list

Re: "Collapsing" a list into a list of changes

2005-02-09 Thread Raymond Hettinger
end up with [0,1,2,3,2,4,5]. >>> import itertools >>> [k for k, v in itertools.groupby(lst)] [0, 1, 2, 3, 2, 4, 5] Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: returning True, False or None

2005-02-04 Thread Raymond Hettinger
return True in s or s == set([None]) and None Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: returning True, False or None

2005-02-04 Thread Raymond Hettinger
True in lst: > return True > elif False in lst: > return False > else: > return None > > This has a light code smell for me though -- can anyone see a simpler > way of writing this? return max(lst) Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: set, dict and other structures

2005-01-31 Thread Raymond Hettinger
(there is probably a much greater demand for combinatorics, numeric/numarray, or financial modules). If the appeal is not broad, it has little chance of making it into the standard library. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: is this sort method the same as the one in python 2.4

2005-01-30 Thread Raymond Hettinger
d in lib/test/test_builtins.py The key= and reverse= parameters were introduced to list.sort() in Py2.4. Consequently, the above code won't provide the desired functionality in Py2.3 and prior. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: is this sort method the same as the one in python 2.4

2005-01-30 Thread Raymond Hettinger
l and note that stability is not preserved: >>> data = [('a', 1), ('a', 2), ('b', 3)] >>> data.sort(key=lambda record: record[0]) >>> data.reverse() >>> data [('b', 3), ('a', 2), ('a', 1)] Here's another way of accomplishing the original sort and preserving stability: >>> data = [('a', 1), ('a', 2), ('b', 3)] >>> sorted(data, cmp = lambda x,y: cmp(y[0], x[0])) [('b', 3), ('a', 1), ('a', 2)] Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: is this sort method the same as the one in python 2.4

2005-01-29 Thread Raymond Hettinger
thing but is implemented a bit differently. A custom key wrapper is applied to each object so that only the key value gets compared (no need for a full tuple with a tie breaker value). Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: generator expressions: performance anomaly?

2005-01-19 Thread Raymond Hettinger
[Steve Holden] > Since it doesn't yet optimize 2+5 to a constant-folded 7 you should > realize that you are suggesting a large increase in the compiler's > analytical powers. FWIW, limited constant folding is already in CVS for Py2.5. Raymond Hettinger -- http://mail

Re: generator expressions: performance anomaly?

2005-01-17 Thread Raymond Hettinger
docstring for Lib/test/test_iterlen.py . Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: generator expressions: performance anomaly?

2005-01-16 Thread Raymond Hettinger
[Raymond Hettinger] > >List slice assignment is an example of a tool with a special case optimization > >for inputs that know their own length -- that enables the tool to pre-allocate > >its result rather than growing and resizing in spurts. Other such tools include > &g

Re: generator expressions: performance anomaly?

2005-01-16 Thread Raymond Hettinger
tor. It would make me examine how 'a' is being used and check whether the surrounding code can use the iterator or an new object. > Comments, clues, ... please. As a point of interest, note that Py2.4 improved some of its built-in iterators to report their length if req

Re: why does UserDict.DictMixin use keys instead of __iter__?

2005-01-04 Thread Raymond Hettinger
__(). Still, if __iter__() is provided, UserDict.DictMixin will take advantage of it. The same is also true for __contains__(), and iteritems(). Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: PEP 288 ponderings

2005-01-02 Thread Raymond Hettinger
e real issue with PEP 288's idea for generator attributes is that the current C implementation doesn't readily accommodate this change. Major surgery would be required :-( The more important part of the PEP is the idea for generator exceptions. The need arises in the context of flushin

Re: Python 3000, zip, *args and iterators

2004-12-29 Thread Raymond Hettinger
le) ... rows = range(len(data)) ... for col in xrange(len(data[0])): ... args = [data[row][col] for rows in rows] ... yield f(*args) Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Python 3000, zip, *args and iterators

2004-12-27 Thread Raymond Hettinger
posting present only toy examples -- real use cases have not yet emerged. If some do emerge, I suspect that each problem will have a better solution (using existing tools) than the one being proposed. If so, then adopting the proposal will have the negative effect of leading folks away from the corre

Re: dot products

2004-12-20 Thread Raymond Hettinger
a,b)) 1.62 sec:sum(x*y for x,y in zip(a,b)) 0.75 sec:sum(imap(mul, a, b)) 1.04 sec:sum(map(mul, a, b)) Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Oddity in 2.4 with eval('None')

2004-12-20 Thread Raymond Hettinger
leton instance of None. That will occur even if you've mucked with None entry in the globals dictionary. Bytecode for eval() doesn't go through the bytecode optimizer so its dictionary lookup is retained (producing the effect in your example). To have made None a literal constant would

Re: List limits

2004-12-20 Thread Raymond Hettinger
all in comparison. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: dot products

2004-12-19 Thread Raymond Hettinger
,y in zip(a,b)) 0.635497577901 sum(imap(mul, a, b)) 0.85894416601 sum(map(mul, a, b)) C:\pydev>python timedot.py 10 2.19490353509 sum(a[i]*b[i] for i in xrange(len(a))) 2.01773998894 sum(x*y for x,y in izip(a,b)) 2.44932533231 sum(x*y for x,y in zip(a,b)) 1.24698871922 sum(imap(mul, a, b)) 1.

Re: while 1 vs while True

2004-12-13 Thread Raymond Hettinger
;None' only just became a constant in 2.4. > > I don't know if 'True' and 'False' are in line for similar treatment (there are > obvious backwards compatibility issues in doing so). It is unlike to before Py3.0. Making them constants would break the reams

Re: Performance (pystone) of python 2.4 lower then python 2.3 ???

2004-12-13 Thread Raymond Hettinger
ist comprehensions got a nice 60% boost on my machine: C:\py24\Lib>\python23\python timeit.py -r9 "[i for i in xrange(1000)]" 100 loops, best of 9: 1.11 msec per loop C:\py24\Lib>\python24\python timeit.py -r9 "[i for i in xrange(1000)]" 1000 loops, best of 9: 417

Re: Zip with a list comprehension

2004-12-11 Thread Raymond Hettinger
omps and genexps. If you want to go the extra distance, itertools.izip() can offer a performance boost and better memory utilization than zip(). It can be used almost anywhere as long as the app doesn't demand a real list. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Replace string except inside quotes?

2004-12-03 Thread Raymond Hettinger
double quotes? For making changes to Python code, I > would also like to avoid changing text in comments, either the '#' or > '""" ... """' kind. The source for the tokenize module covers all these bases. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: efficient intersection of lists with rounding

2004-12-03 Thread Raymond Hettinger
nt? Yes: >>> set((x,round(y)) for x,y in a) & set((x,round(y)) for x,y in b) set([(123, 8.0), (123, 2.0), (123, 1.0)]) > I'm using python 2.3. >>> from sets import Set as set >>> set([(x,round(y)) for x,y in a]) & set([(x,round(y)) for x,y in b]) set([(123, 8.0), (123, 2.0), (123, 1.0)]) Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: Decimal printing without the exponent

2004-11-28 Thread Raymond Hettinger
((0, (1, 0, 1), 1))) > '1.01E+3' > >>> > > how do you make the 2nd example print 1010? The quantize method will convert to any desired exponent (zero in your example): >>> d = (Decimal((0, (1, 0, 1), 1))) >>> d Decimal("1.01E+3") >>> d.quantize(Decimal(1)) Decimal("1010") Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

<    3   4   5   6   7   8