Re: What's a call-tip to do?

2008-05-14 Thread Raymond Hettinger
all-tip. I don't know why yours is different. Also, help(itertools.count) does the right thing. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Classmethods are evil

2008-05-19 Thread Raymond Hettinger
to implement alternate constructors. For good examples, see dict.fromkeys() or any of the various datetime constructors. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Optimization: Picking random keys from a dictionary and mutating values

2008-05-28 Thread Raymond Hettinger
On May 28, 4:41 pm, blaine <[EMAIL PROTECTED]> wrote: > 1.  Whats a good way to get the keys? I'm using a loop with > random.choice() at the moment. Why not keep a separate list of keys and use random.sample()? Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: How to add function return value

2008-05-30 Thread Raymond Hettinger
ines(f). Then, append a "return locals()" to the end of the function and run it through exec. 3. Try hacking a tracing/debugging utility. 4. Run the sourcefile through tokenize, make the appropriate insertion, and then untokenize. . . . Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Nasty gotcha/bug in heapq.nlargest/nsmallest

2008-05-30 Thread Raymond Hettinger
. The == arises because nlargest/nsmallest compare decorated tuples. Tuple comparison always starts with equality tests to find the first unequal element and then switches back to testing whichever inequality test was requested. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Merging ordered lists

2008-06-01 Thread Raymond Hettinger
good for removing duplicates: result = [k for k, g in groupby(imerge(*sources))] Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Merging ordered lists

2008-06-02 Thread Raymond Hettinger
have a precise idea of what it means to merge them while preserving order. For example if the inputs are XYZPDQ and bYlmPz, then what does a merged sequence look like once the Y and P duplicates are removed? Is it XZPDQblmz or some intermix of the two like XbZlPmDzQ? If it is the first

Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-04 Thread Raymond Hettinger
dict subclass and should provide all of those methods. Also, it is useful in its current form. > Or would it be better to redefine how defaultdict.fromkeys works, so > that it first creates the defaultdict, and then goes through the keys? > > All comments welcome.  If I ge

Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-04 Thread Raymond Hettinger
s run during the lookup phase and creates your default just-in-time for use: >>> d = defaultdict(list) >>> for k in 'zyzygy': d[k].append(1) # empty list created on lookup if needed >>> d defaultdict(, {'y': [1, 1, 1], 'z': [1, 1], 'g': [1]}) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: marshal.dumps quadratic growth and marshal.dump not allowing file-like objects

2008-06-15 Thread Raymond Hettinger
ssive and grows only in 1MB blocks and giving your observed nasty quadratic behavior. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: marshal.dumps quadratic growth and marshal.dump not allowing file-like objects

2008-06-16 Thread Raymond Hettinger
7;ve just fixed this for Py2.5.3 and Py2.6. No more quadratic behavior. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: 'string'.strip(chars)-like function that removes from the middle?

2008-06-16 Thread Raymond Hettinger
location? (besides .replace() ;) > >>> identity = "".join(map(chr, range(256))) > >>> 'www.example.com'.translate(identity, 'cmowz.') > > 'exaple' And in Py2.6, you'll be able to simplify further: >>> 'abcde'.translate(None, 'bd') 'ace' Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: sorted or .sort() ?

2008-06-16 Thread Raymond Hettinger
as list.sort() except for the step where the input gets copied. For list inputs, that extra time is trivial compared to the cost of actually doing the sort. I wouldn't worry about the negligible performance difference. Use whichever fits best in your program. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: string.Template.delimiter cannot be overriden?

2008-06-16 Thread Raymond Hettinger
s, subclassing is the intended way to produce variants of Template with a different delimiter. >>> import string >>> class PercentTemplate(string.Template): delimiter = "%" >>> s = PercentTemplate('Move %piece to %position') >&

Re: Hamming Distance

2008-06-19 Thread Raymond Hettinger
n to use iteration instead of recursion (the latter is *very* expensive in Python). The fastest way is to write a C function or use Pyrex. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Hamming Distance

2008-06-19 Thread Raymond Hettinger
Non-recursive, 8-bit block table lookup version: def ham(a, b, ht=[hamdist(a,0) for a in range(256)]): x = a ^ b dist = 0 while x: dist += ht[x & 255] x >>= 8 return dist Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: An idiom for code generation with exec

2008-06-20 Thread Raymond Hettinger
lambda packet: packet[5:1:-1] Since function calls are expensive in python, you can also gain speed by parsing multiple fields at a time: header, timetag, counter = parse(packet) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: string.Template.delimiter cannot be overriden?

2008-06-24 Thread Raymond Hettinger
[kretik] >> I've been trying to coax this class to use something other than the > >> default '$' but it seems setting it to something else has no discernible > >> effect. Is it necessary to inherit from the class to do this? [raymond] > > Yes, subcla

Re: Nested generator caveat

2008-07-03 Thread Raymond Hettinger
On Jul 3, 9:20 pm, "Dieter Maurer" <[EMAIL PROTECTED]> wrote: > The apparent reason is that the free variables in > nested generator definitions are not bound (to a value) at invocation > time but only at access time. That's what it is supposed to do. Welcome to

Re: Perfect hashing for Py

2008-07-12 Thread Raymond Hettinger
a winning trade-off. So, the optimize() step would need to be optional, not builtin. Raymond FWIW, I mentioned all this at PyConDue but the message must have gotten lost. -- http://mail.python.org/mailman/listinfo/python-list

Re: Perfect hashing for Py

2008-07-12 Thread Raymond Hettinger
On Jul 12, 10:13 am, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > On Jul 11, 3:01 pm, [EMAIL PROTECTED] wrote: > > > I have found this perfect hash (minimal too) > > implementation:http://burtleburtle.net/bob/hash/perfect.html > > > I have already translated

Re: new itertools functions in Python 2.6

2008-07-14 Thread Raymond Hettinger
] != n - 1: break else: return indices[i] += 1 for j in range(i+1, r): indices[j] = indices[j-1] yield tuple(pool[i] for i in indices) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: __del__ methods

2008-07-18 Thread Raymond Hettinger
op notch Python programmers who have programmed happily for years without ever using __del__. There are ways to get it to work for you, but it may be the smart thing to pretend that you've never heard of it. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: atan2 weirdness

2008-07-19 Thread Raymond Hettinger
mport pi, atan2, radians, degrees, cos, sin for i in range(360): t = radians(i) x = cos(t) y = sin(t) a = degrees(atan2(y, x)) print i, t, x, y, a Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Question

2008-07-19 Thread Raymond Hettinger
On Jul 19, 2:27 am, [EMAIL PROTECTED] wrote: > Why is Perl so much better than python? Because dollar signs are a superior form of punctuation. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: persistent deque (continued)

2008-07-21 Thread Raymond Hettinger
ended queue ( http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/259179 ) and then replace the dict with a shelf. Presto, you've got a persistent deque. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: convert list of lists to list

2008-07-22 Thread Raymond Hettinger
s) For linear performance, you can use itertools: list(itertools.chain(*list_of_lists)) Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: seemingly simple list indexing problem

2008-07-28 Thread Raymond Hettinger
put array: >>> s = [ 108, 58, 68 ] >>> sorted(range(len(s)), key=s.__getitem__) [1, 2, 0] Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Optimizing size of very large dictionaries

2008-07-31 Thread Raymond Hettinger
7;Duplicate:', record elif sha1(record).digest() in possible_dups: seen.add(record) Raymond P.S. If the log entries are one liners, maybe it would be better to use the operating system's sort/uniq filters. -- http://mail.python.org/mailman/listinfo/python-list

Re: random numbers according to user defined distribution ??

2008-08-07 Thread Raymond Hettinger
def linear(x): return 3 * x - 6 lo, hi = 2, 10 r = make_user_distribution(linear, lo, hi) for i in range(20): print r() -- Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: reading dictionary's (key,value) from file

2008-04-07 Thread Raymond Hettinger
ort the files: opt1.py -- opts = { '-cc': '12', '-I': r'/my/path/work/'} opt2.py -- opts = { '-I': r/my/path/work2/'} main.py --- from opts1 import opts as opt1 from opts2 import opts as opt2 Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: list.sort(): heaviest item?

2008-04-08 Thread Raymond Hettinger
On Apr 8, 8:15 am, "Steven Clark" <[EMAIL PROTECTED]> wrote: > If I have a list of items of mixed type, can I put something into it > such that after a list.sort(), is guaranteed to be at the end of the > list? Since the other guys gave you the real answer, how about this: sentinel = object() myl

Re: Little novice program written in Python

2008-04-24 Thread Raymond Hettinger
it stands. If you want to tweak it a bit, you can avoid using a flag like "finished" by using a break-statement. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Raymond Hettinger
print sum(x - mean for x in data)/len(data) See: http://svn.python.org/view/sandbox/trunk/statistics/statistics.py?rev=40981&view=markup Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Feature suggestion: sum() ought to use a compensated summation algorithm

2008-05-05 Thread Raymond Hettinger
2x faster for adding numbers only: Try it with Py2.6 or Py3.0. The sum() function has been optimized and should be at least 6x faster for summing ints and floats. It should even beat psyco optimized sums! Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Overwriting property-> can't set attribute

2008-08-22 Thread Raymond Hettinger
lasses, not instances. So the assignment should be: B.testattr = property(...) Second, the property() call in you example only defines a getter function (the first argument) and not a setter function (the second argument) it defaults to a read-only property. That is why b.testattr='tes

Re: property() usage - is this as good as it gets?

2008-08-24 Thread Raymond Hettinger
of property() that allows you to re-use the same getter/setter code for multiple attributes: http://code.activestate.com/recipes/205126/ Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-list

Re: What is class method?

2008-08-26 Thread Raymond Hettinger
first argument is a class, not an instance. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: use of Queue

2008-08-27 Thread Raymond Hettinger
to know when the producers are > finished?  Yes, there are different approaches like sentinels, but the > absence of a unified approach built into the library really does seem > like a deficiency in the library. See effbot's reply. The task_done and join methods were put there for

Re: Which is faster?

2008-08-30 Thread Raymond Hettinger
peed, then just time both approaches. However, it's a good run of thumb that builtins are hard to beat by simulating them in pure python (unless you're using Psyco as an optimizer). Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: dict.update

2008-09-02 Thread Raymond Hettinger
k properly, can anyone offer any advise? The update() method is not quite right for your purposes. But a simple generator expression will do the trick: >>> dict((k, [v, dict1.get(k, 0)]) for k, v in dict2.items()) {456: [3, 3], 234: [5, 5], 123: [4, 3], 157: [2, 0], 567: [2, 0]} Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Profiling, sum-comprehension vs reduce

2008-09-16 Thread Raymond Hettinger
propriate implementation of addition. The time machine strikes again! Try using Py2.6. The built-in sum() function is much smarter and faster. It does in fact use C addition. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: time series calculation in list comprehension?

2006-03-12 Thread Raymond Hettinger
it has O(n) memory consumption and O(m) running time. In contrast, the other variants do more work than necessary by pulling the whole sequence into memory or by re-summing all n items at every step, resulting in O(m) memory consumption and O(m*n) running time. This recipe gets my vote for

Re: "RuntimeError: dictionary changed size during iteration" ; Good atomic copy operations?

2006-03-12 Thread Raymond Hettinger
to me as odd gambling; retry how often?) Since the app doesn't seem to care when the dump occurs, it might be natural to put it in a while-loop that continuously retries until it succeeds; however, you still run the risk that other threads may never leave the object alone long enough to dump completely. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q's: pythonD and range(1,12)

2006-03-13 Thread Raymond Hettinger
ge(n))). Some of this is a matter of taste and a matter of what you're used to using, so the answer to what is the "more intuitively-useful" varies depending on who is answering the question. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Global Threading Lock 2 - Re: "RuntimeError: dictionary changed size during iteration"..

2006-03-13 Thread Raymond Hettinger
> I have 5 trials max as of now. The error was about once in 3 months in > my case: that should solve the problem for the rest of the universe ... > If not, there is another bug going on. I sure hope your code isn't being used in mission critical apps like air traffic control :=0 Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: "RuntimeError: dictionary changed size during iteration" ; Good atomic copy operations?

2006-03-13 Thread Raymond Hettinger
on to suspending other threads, it has the side-effect of suspending control-break checks. IOW, you won't be able to break out of the dump(). Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Memory visualization

2006-03-14 Thread Raymond Hettinger
ng things that could be done in C by exploiting tp_traverse and by hacking Python's internal object allocator. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: trying to find repeated substrings with regular expression

2006-03-14 Thread Raymond Hettinger
lowed by the target phrase. The first two are in a group and the last is not included in the result group. The any-text section is non-greedy: >>> import re >>> re.findall('(FOO.*?)(?=FOO|$)', s) ['FOO blah1a blah1b ', 'FOO blah2 ', 'FOO blah3a blah3b blah3b'] Raymond -- http://mail.python.org/mailman/listinfo/python-list

TaskQueue

2006-03-21 Thread Raymond Hettinger
I would like to get feedback on an idea I had for simplifying the use of queues with daemon consumer threads Sometimes, I launch one or more consumer threads that wait for a task to enter a queue and then work on the task. A recurring problem is that I sometimes need to know if all of the tasks ha

Re: Good thread pool module

2006-03-22 Thread Raymond Hettinger
to the Queue module that makes it a little bit easier to use a pool of worker threads and then be able to tell when they are all done with processing: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/475160 Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list of objects by multiple attributes

2006-04-11 Thread Raymond Hettinger
[George Young] >> For multiple keys the form is quite analogous: >> >> L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey)) [Noah] > If you are lambda-phobic (as I am) this should also work for an > arbitrary set of attributes: > > attrs = 'attr1 attr2 attr3'.split() > sortlist = [[getat

Re: Manipulating sets from the 2.4 C API?

2006-04-11 Thread Raymond Hettinger
Dave Opstad wrote: > I just looked through the Python/C API reference for 2.4.3 and didn't > see anything about using sets. I'd been expecting things like PySet_New, > PySet_Check etc. In Py2.4, there was not a custom set C API because the module was still ungoing significant development. For 2.4

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
stay, so it's time to get over it. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorting a list of objects by multiple attributes

2006-04-12 Thread Raymond Hettinger
[Noah] > I suggested the above because it wasn't obvious to me how one would > pass the arbitrary set of attributes to the lambda expression (and I > envisioned them being specified as strings in this case, since the set > of attributes will be coming from a web form). > > So what about the followi

Re: list.clear() missing?!?

2006-04-12 Thread Raymond Hettinger
[Steven Bethard] > I think these are all good reasons for adding a clear method, but being > that it has been so hotly contended in the past, I don't think it will > get added without a PEP. Anyone out there willing to take out the best > examples from this thread and turn it into a PEP? Somethin

Re: list.clear() missing?!?

2006-04-12 Thread Raymond Hettinger
[Felipe Almeida Lessa] > > I love benchmarks, so as I was testing the options, I saw something very > > strange: > > > > $ python2.4 -mtimeit 'x = range(10); ' > > 100 loops, best of 3: 6.7 msec per loop > > $ python2.4 -mtimeit 'x = range(10); del x[:]' > > 100 loops, best of 3: 6.35 msec

Re: list.clear() missing?!?

2006-04-13 Thread Raymond Hettinger
> > * the request is inane, the underlying problem is trivial, and the > > relevant idiom is fundamental (api expansions should be saved for rich > > new functionality and not become cluttered with infrequently used > > redundant entries) > > Is this sort of editorialising fair, or just a way of no

Re: list.clear() missing?!?

2006-04-13 Thread Raymond Hettinger
[Dan Christensen] > It's true that this runs at the same speed as the del variants on my > machine. That's not too surprising to me, but I still don't > understand why the del variants are more than 5% faster than the first > version. Understanding it involves looking at implementation specific d

Re: Feature request: String-inferred names

2009-12-01 Thread Raymond Hettinger
to a variable whose name is in another variable?" > The answer is almost always "Don't do that, use a > dictionary.") The proposed syntax works better with class namespaces which automatically provide inheritance logic and method binding. Dictionaries don't provide equivalent support. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: python bijection

2009-12-01 Thread Raymond Hettinger
[Joshua Bronson] > Raymond, do you think there might be any future in including a built- > in bidict data structure in Python? I don't think so. There are several forces working against it: * the recipe is new, so it hasn't had a chance to mature or to gain a fan club.

Re: python bijection

2009-12-05 Thread Raymond Hettinger
oblems with APIs that do not require the user to specify names for the two directions. It is too easy to make mistakes that are hard to see. Which is correct, phonelist['raymond'] or phonelist[raymonds_phonenumber]? There is no way to tell without looking back at the bijection specificat

Re: python bijection

2009-12-05 Thread Raymond Hettinger
(i.e. another table with foreign-key). Too bad a DB wasn't used to solve a DB problem. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Are routine objects guaranteed mutable & with dictionary?

2009-12-05 Thread Raymond Hettinger
On Dec 4, 2:03 am, "Alf P. Steinbach" wrote: > Is this guaranteed to work in Python 3.x? > >  >>> def foo(): pass > ... >  >>> foo.blah = 222 >  >>> foo.blah > 222 Yes, function attributes are guaranteed to be writable: htt

Re: Float precision and float equality

2009-12-05 Thread Raymond Hettinger
re they become large enough to overpower the signal in your data (e.g. the Lorentz equation or some other chaotic sequence). Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Float precision and float equality

2009-12-05 Thread Raymond Hettinger
On Dec 5, 12:56 pm, Mark Dickinson wrote: > On Dec 5, 8:25 pm, Raymond Hettinger wrote: > > > On Dec 5, 7:37 am, Anton81 wrote: > > > > I'd like to do calculations with floats and at some point equality of > > > two number will be checked. > > >

Re: python bijection

2009-12-05 Thread Raymond Hettinger
e :-) > [1]:http://gitorious.org/graphine/pages/GraphineForPythonistas Do you have many users? Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: python bijection

2009-12-05 Thread Raymond Hettinger
On Dec 5, 3:22 pm, geremy condra wrote: > On Sat, Dec 5, 2009 at 4:39 PM, Raymond Hettinger wrote: > > [geremy condra] > >> I actually considered using dependencies as an example on the > >> "graphine for pythonistas"[1] article, but decided to do the maz

Re: Float precision and float equality

2009-12-06 Thread Raymond Hettinger
On Dec 5, 11:42 pm, Tim Roberts wrote: > Raymond Hettinger wrote: > > >   if not round(x - y, 6): ... > > That's a dangerous suggestion.  It only works if x and y happen to be > roughly in the range of integers. Right. Using abs(x-y) < eps is the way to go. Raymo

Re: Float precision and float equality

2009-12-10 Thread Raymond Hettinger
und() was along the lines of performing a quantize or snap-to-grid operation after each step in the calculation. That approach parallels the recommendation for how to use the decimal module for fixed point calculations: http://docs.python.org/library/decimal.html#decimal-faq Raymond -- http://mail

Efficient Running Median

2010-01-15 Thread Raymond Hettinger
of an IndexableSkiplist is similar to a B+tree but the implementation in pure python is much simpler. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Raymond Hettinger
plementation > dependent, of course). If the iterable is a list already, the list > building is not needed. Good analysis. That is exactly what is going on under the hood. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Sorted dictionary

2010-01-21 Thread Raymond Hettinger
it takes to iterate the list in the first place: s = SortedDict(items) list(s) # O(n log n) to sort and O(n) to iterate s[newkey] = newval list(s) # close to O(n) my two cents, Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Consume an iterable

2010-01-22 Thread Raymond Hettinger
), maxlen=0) > > What is the advantage of using a collections.deque against, say, the > following code? > > def consume(iterator, n): >     for _ in islice(iterator, n): pass The deque version is faster. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: list.pop(0) vs. collections.dequeue

2010-01-23 Thread Raymond Hettinger
e reasons were part of Guido's rationale. Tim Peters and I also participated in the conversation and did not disagree. So, collections.deque() was born and the problem stopped being an issue. Also, Daniel Stuzbach has published a blist implementation that offers high performance insertions

Re: Consume an iterable

2010-01-24 Thread Raymond Hettinger
while ((item = PyIter_Next(it)) != NULL) { Py_DECREF(item); } Py_DECREF(it); if (PyErr_Occurred()) return NULL; Py_RETURN_NONE; } This code consumes an iterator to exhaustion. It is short, sweet, and hard to beat

Re: Is defaultdict thread safe?

2010-01-25 Thread Raymond Hettinger
off. class A: def __init__(self): . . . my_dict = defaultdict(A) # not thread-safe. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: flow control and nested loops

2009-09-26 Thread Raymond Hettinger
if test1(x,y,z): return frobnicate(x,y,z) for x in X: f(x) Or you can write a little state machine with flags and a single loop but that isn't very readable or satisfying. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Business issues regarding adapting Python

2009-09-28 Thread Raymond Hettinger
s to become a decent C++ programmer. Python is distinct because it is not that hard to learn. So, if I were hiring, I would focus on general programming skills and knowledge of the problem domain. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Raymond Hettinger
ctly) be recast as key functions. All that being said, for many everyday uses, a key function is simpler to write and faster to run than a cmp function approach. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Fast decimal arithmetic module released

2009-10-02 Thread Raymond Hettinger
[Stefan Krah] > today I have released the following packages for fast arbitrary precision > decimal arithmetic: Nice. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
with a cmp function but not a key function? t = [[9,6],[7,1]],[[2,5],[4,3]] # inputs t.sort(mycmp) # what would the cmp function be? print t # what would the output be Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
the nodes within a single > tree. Can you give an example of a list of trees and a cmp function that recursively compares them? Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
ssive nodes to be compared. >From the sound of it, the trees are static during the sort and would get a nice O(n log n) --> O(n) speed-up if a key function were allowed to flatten them in a single pass. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-04 Thread Raymond Hettinger
f_lists[1]: ... >> Are the trees user defined classes? > > Not in general. They might be nested tuples, lists, or dictionaries. > Or they could come from a non-extensible library-defined class, like > from cElementTree Are there any of these trees that cannot be transformed (by a key function) into an equivalent nested list of lists and values so that the trees can be directly compared to one another using Python's normal built-in recursive comparison order for lists? Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
be precomputed in a key function (perhaps involving some complex interaction between the two compared items like a tree merge or somesuch). Instead of trying to create a hard comparison from first principles, there may already be some research on the subject in the SQL world. It seems to me that SQL&#

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
ot;here's how two objects should be compared"). In contrast, some people who have have had deep experience with cmp functions may tend to first think of cmp solutions and then have a harder time seeing solutions with key functions. If you grew-up on C's qsort() like I did, then a key function may not be the first thing that pops into your head. One other interesting data point: Python uses key functions in min(), max(), heapq.nsmallest(), heapq.nlargest, and itertools.groupby(). Those functions never supported a cmp function argument, nor has one ever been requested. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Raymond Hettinger
on (using other techniques) ;-) Once Kernighan and Ritchie put C's qsort() into the food supply, we were doomed. FWIW, I think the standard library's unittest module is also a code prion -- there's no other explanation for its thriving despite competition from the vastly superior syntax of py.test or nose. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: bug with itertools.groupby?

2009-10-06 Thread Raymond Hettinger
ccount: >     print 'grouped acc: ', acc > > gives output: > > grouped acc:  61 > grouped acc:  64 > grouped acc:  61 > > am I doing something wrong? Try another variant of groupby() that doesn't require the data to be sorted: http://code.activestate.com/recipes/259173/ Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Raymond Hettinger
FWIW, the "additional memory requirements" are typically a set of pointers to the objects being sorted, so the memory overhead is typically very small relative to the size of the objects being sorted. IOW, this isn't much of a consideration in most Python apps. Raymond -- http://ma

Re: Neural networks in python

2009-10-08 Thread Raymond Hettinger
anks in advance :) Take a look at http://code.activestate.com/recipes/496908/ Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: The rap against "while True:" loops

2009-10-12 Thread Raymond Hettinger
well. So, if you hear someone make-up a new "best practice" proscibing "while True", just smile and continue to write programs as you have been. You will not be alone. Many excellent programmers write "while True" whenever it is called for. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: efficient running median

2009-10-13 Thread Raymond Hettinger
would be asymtotically O(n log n) but messy to implement. > > QuickSelect will find the median in O(log n) time. Right. See http://code.activestate.com/recipes/269554/ for a sample implementation in Python. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: The rap against "while True:" loops

2009-10-14 Thread Raymond Hettinger
ng about types and how to compose program components). "Structured Programming with go to Statements" by Donald Knuth has an in-depth comparative analysis of many different looping constructs. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: efficient running median

2009-10-15 Thread Raymond Hettinger
he missing parts. See http://code.activestate.com/recipes/576930/ for working Python code adapted from that paper. Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: slicing return iter?

2009-10-17 Thread Raymond Hettinger
: 'Allow forward or backwards iteration over a subslice' for i in range(start, stop, step): yield seq[i] Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: slicing return iter?

2009-10-18 Thread Raymond Hettinger
[Raymond] > > If it really is a sequence (with len and getitem), you can write your > > own indexing iterator: > > >   def myslice(seq, start, stop, step): > >        'Allow forward or backwards iteration over a subslice' > >        for i in range(sta

Re: Program to compute and print 1000th prime number

2009-11-07 Thread Raymond Hettinger
= 10: primes.extend(values) print primes[1000-1] Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: My own accounting python euler problem

2009-11-11 Thread Raymond Hettinger
-up. Given invoices of [10, 11, 12, 1000, 13, 14], the 1000 should be easy to match-up in any grouping of payments. Likewise, when looking for groupings, start with the most unique values. Given [2, 2, 2, 3, 3, 5, 5, 5, 6.1, 7], start by trying to match the 6.1 since there is only one occurrence.

Re: Does turtle graphics have the wrong associations?

2009-11-12 Thread Raymond Hettinger
t calling it Raptor Graphics that will please everyone ;-) Raymond -- http://mail.python.org/mailman/listinfo/python-list

<    2   3   4   5   6   7   8   9   >