Re: Minimal binary diff & version control in Python?

2009-04-26 Thread Raymond Hettinger
[Kevin Ar18] > Has anyone ever tried to implement some form of version control in Python (if > so, where)? Yes. Here's the code I use to read and write my old version control files (originally created with TLIB): http://code.activestate.com/recipes/576729/ Raymond -- http://mail.python.org

Geohashing

2009-04-28 Thread Raymond Hettinger
import hashlib def geohash(latitude, longitude, datedow): '''Compute geohash() in http://xkcd.com/426/ >>> geohash(37.421542, -122.085589, b'2005-05-26-10458.68') 37.857713 -122.544543 ''' h = hashlib.md5(datedow).hexdigest() p, q = [('%f' % float.fromhex('0.' + x)) for x

Re: sorting items in a table problematic because of scientific notation

2009-04-29 Thread Raymond Hettinger
[John Machin] > > 'NEAR_DIST'], [('N', 9, 0), ('N', 9, 0), ('F', 19, 11)], [53, 55, ' > > The data type code for the offending column is "F" which is not in the > bog-standard dBase III set of C, N, D, and L. The code that you have used > merely > returns unchanged the character string that finds

Re: Get item from set

2009-04-29 Thread Raymond Hettinger
[bearophileh...@lycos.com] > But some other times you may accept to change the class and the set/ > dict, making it tell apart the keys only when they are different > object: > > class Some(object): >     def __init__(self, y): >         self._y = y >     def __eq__(self, other): >         return s

Re: Generating all combinations

2009-06-03 Thread Raymond Hettinger
> What, no partitions? > > http://en.wikipedia.org/wiki/Partition_of_a_set Seems like you could roll your own (using combinations as a starting point): def pairwise(iterable): a, b = tee(iterable) next(b, None) return izip(a, b) def partition(s): n = len(s) for i in range(n):

Re: Generating all combinations

2009-06-03 Thread Raymond Hettinger
> > What, no partitions? > > >http://en.wikipedia.org/wiki/Partition_of_a_set Simpler version: def partition(s): n = len(s) parts = range(1, n) for i in range(n): for div in combinations(parts, i): print map(s.__getslice__, chain([0], div), chain(div, [n])) >>> pa

Re: Generating all combinations

2009-06-03 Thread Raymond Hettinger
> Nice one! It only does partitions of a sequence. I haven't yet looked at a way to do partitions of a set. Any ideas? > Raymond, as perhaps *the* principle contributor to itertools, do you feel > that the combinatorics-related tools should be in their own module? Or is > that dividing the mod

Re: named tuple mutability

2008-05-14 Thread Raymond Hettinger
> Should tuples be named? Yes. -- http://mail.python.org/mailman/listinfo/python-list

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

2008-05-14 Thread Raymond Hettinger
On May 14, 8:24 am, Tal Einat <[EMAIL PROTECTED]> wrote: > I just ran into this. In IDLE (Python 2.5), the call-tip for > itertools.count is: > "x.__init__(...) initializes x; see x.__class__.__doc__ for signature" My IDLE (1.2.2 running on Python 2.5.2) has the correct call-tip. I don't know why

Re: Classmethods are evil

2008-05-19 Thread Raymond Hettinger
On May 16, 9:01 pm, Ivan Illarionov <[EMAIL PROTECTED]> wrote: > After re-reading "Python is not Java" I finally came to conclusion that > classmethods in Python are a very Bad Thing. Sounds like a serious case of mis-learning. Class methods are the preferred way to implement alternate constructo

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
On May 30, 6:21 pm, HYRY <[EMAIL PROTECTED]> wrote: > Can I write a decorator that it can automately do this conversion > > def func1() >     a = 1 > > ---> > > def func1(): >     a = 1 >     return locals() Not sure why you would want to do this, but there are several ways. 1. Make bytecode hack

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

2008-05-30 Thread Raymond Hettinger
On May 15, 12:06 am, Peter Otten <[EMAIL PROTECTED]> wrote: > According > to my ad hoc test you need <, <=, and == for nlargest()/nsmallest() to > work: In Py2.6 and after, you only need < and ==. I replaced the LE tests with LT to match list.sort() and bisect.bisect(). The == arises because nla

Re: Merging ordered lists

2008-06-01 Thread Raymond Hettinger
On May 31, 10:00 pm, etal <[EMAIL PROTECTED]> wrote: > Here's an algorithm question: How should I efficiently merge a > collection of mostly similar lists, with different lengths and > arbitrary contents, while eliminating duplicates and preserving order > as much as possible? I would do it two s

Re: Merging ordered lists

2008-06-02 Thread Raymond Hettinger
> Thanks for the tip; itertools never ceases to amaze. One issue: > groupby doesn't seem to remove all duplicates, just consecutive ones > (for lists of strings and integers, at least): > > >>> [k for k, g in itertools.groupby(list("asdfdfffdf"))] > > ['a', 's', 'd', 'f', 'd', 'f', 'd', 'f'] That'

Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-04 Thread Raymond Hettinger
On Jun 3, 1:11 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote: > I used defaultdict.fromkeys to make a new defaultdict instance, but I > was surprised by behavior: > >     >>> b = defaultdict.fromkeys(['x', 'y'], list) > >     >>> b >     defaultdict(None, {'y': , 'x': }) > >     >>> b['x'] >     >

Re: defaultdict.fromkeys returns a surprising defaultdict

2008-06-04 Thread Raymond Hettinger
On Jun 3, 1:11 pm, Matthew Wilson <[EMAIL PROTECTED]> wrote: > I used defaultdict.fromkeys to make a new defaultdict instance, but I > was surprised by behavior: >     >>> b = defaultdict.fromkeys(['x', 'y'], list) >     >>> b >     defaultdict(None, {'y': , 'x': }) One other thought: Even after

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

2008-06-15 Thread Raymond Hettinger
On Jun 15, 1:04 am, [EMAIL PROTECTED] wrote: > However it seems that marshal.dumps() for large objects has a > quadratic performance issue which I'm assuming is that it grows its > memory buffer in constant increments. Looking at the source in http://svn.python.org/projects/python/trunk/Python/ma

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

2008-06-16 Thread Raymond Hettinger
On Jun 15, 8:08 am, [EMAIL PROTECTED] wrote: > Indeed. I (the OP) am using a production release which has the 1k > linear growth. > I am seeing the problems with ~5MB and ~10MB sizes. > Apparently this will be improved greatly in Python 2.6, at least up to > the 32MB limit. I've just fixed this fo

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

2008-06-16 Thread Raymond Hettinger
On Jun 16, 10:09 am, Peter Otten <[EMAIL PROTECTED]> wrote: > Ethan Furman wrote: > > The strip() method of strings works from both ends towards the middle. > > Is there a simple, built-in way to remove several characters from a > > string no matter their location? (besides .replace() ;) > >>> iden

Re: sorted or .sort() ?

2008-06-16 Thread Raymond Hettinger
On Jun 16, 5:11 am, Peter Bengtsson <[EMAIL PROTECTED]> wrote: > My poor understanding is that the difference between `sorted(somelist, > key=lambda x:...)` and `somelist.sort(lambda x,y...)` is that one > returns a new list and the other sorts in-place. > > Does that mean that .sort() is more effi

Re: string.Template.delimiter cannot be overriden?

2008-06-16 Thread Raymond Hettinger
On Jun 16, 9:53 pm, kretik <[EMAIL PROTECTED]> wrote: > 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? Yes, subclassing is the intended wa

Re: Hamming Distance

2008-06-19 Thread Raymond Hettinger
On Jun 19, 4:27 pm, godavemon <[EMAIL PROTECTED]> wrote: > I need to calculate the Hamming Distance of two integers.  The hamming > distance is the number of bits in two integers that don't match.  I > thought there'd be a function in math or scipy but i haven't been able > to find one.  This is my

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
On Jun 20, 5:03 am, eliben <[EMAIL PROTECTED]> wrote: > I've rewritten it using a dynamically generated procedure > for each field, that does hard coded access to its data. For example: > > def get_counter(packet): >   data = packet[2:6] >   data.reverse() >   return data > > This gave me a huge sp

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, subclassing is the intended way to produce variants of

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 a dynamic language. Raymond --

Re: Perfect hashing for Py

2008-07-12 Thread Raymond Hettinger
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 part of it to D, and it seems to work well > enough. As discussed in the PyConDue, I think this may be used in >

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
On Jul 14, 1:26 pm, Mensanator <[EMAIL PROTECTED]> wrote: > ##  Combinations with replacement > ##  - > ##  aaa aab aac aad aae abb abc abd abe acc acd ace > ##  add ade aee bbb bbc bbd bbe bcc bcd bce bdd bde > ##  bee ccc ccd cce cdd cde cee ddd dde dee eee > ## > ##  

Re: __del__ methods

2008-07-18 Thread Raymond Hettinger
On Jul 18, 11:31 am, Jason Baker <[EMAIL PROTECTED]> wrote: > I have a class that I need to do some finalization on when it dies.  I > know I can use the __del__ method, but I seem to recall that it > impedes garbage collection.  Is this the case? FWIW, I know a good number of top notch Python pro

Re: atan2 weirdness

2008-07-19 Thread Raymond Hettinger
On Jul 19, 9:12 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: > hi > > atan2 is supposed to return the angle to x-axis when given y and x, I > suppose if I take [x,y] to one full circle, I should get 0-360 degree > back but no, I get 3 full revolutions! > maybe my understanding is wrong. >

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
On Jul 21, 12:08 pm, castironpi <[EMAIL PROTECTED]> wrote: > Some time ago, I was asking about the feasibility of a persistent > deque, a double-ended queue. > > It runs into the typical space allocation problems.   Try starting with a dict-based implementation of a double-ended queue ( http://asp

Re: convert list of lists to list

2008-07-22 Thread Raymond Hettinger
On Jul 22, 8:25 am, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > if there's only one level of recursion, and the lists aren't too long, > you can simply do: > >      sum(list_of_lists, []) > > (this has quadratic performance, so don't use it for large structures) For linear performance, you can use

Re: seemingly simple list indexing problem

2008-07-28 Thread Raymond Hettinger
[Ervan Ensis] > I have a list like [108, 58, 68].  I want to return > the sorted indices of these items in the same order > as the original list.  So I should return [2, 0, 1] One solution is to think of the list indexes being sorted according the their corresponding values in the input array: >

Re: Optimizing size of very large dictionaries

2008-07-31 Thread Raymond Hettinger
> > Are there any techniques I can use to strip a dictionary data > > structure down to the smallest memory overhead possible? Sure. You can build your own version of a dict using UserDict.DictMixin. The underlying structure can be as space efficient as you want. FWIW, dictionaries automaticall

Re: random numbers according to user defined distribution ??

2008-08-07 Thread Raymond Hettinger
On Aug 6, 3:02 pm, Alex <[EMAIL PROTECTED]> wrote: > I wonder if it is possible in python to produce random numbers > according to a user defined distribution? > Unfortunately the random module does not contain the distribution I > need :-( Sure there's a way but it won't be very efficient. Start

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

2008-04-07 Thread Raymond Hettinger
On Apr 7, 9:38 am, [EMAIL PROTECTED] wrote: > Folks, > Is it possible to read hash values from txt file. > I have script which sets options. Hash table has key set to option, > and values are option values. > > Way we have it, we set options in a different file (*.txt), and we > read from that file

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
> What I would like is to receive some criticism to my code to make it more > Python'esque and, possibly, use the resources of the computer in a more > efficient way (the algorithm implemented below is the Sieve of Eratosthenes): It looks like straight-forward code and is fine as it stands. If you

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

2008-05-05 Thread Raymond Hettinger
On May 3, 9:50 am, Szabolcs Horvát <[EMAIL PROTECTED]> wrote: > I did the following calculation:  Generated a list of a million random > numbers between 0 and 1, constructed a new list by subtracting the mean > value from each number, and then calculated the mean again. > > The result should be 0,

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

2008-05-05 Thread Raymond Hettinger
> > > However I find the seccond argument hack ugly. Does the sum way have any > > > performance advantages over the reduce way? > > > Yes, sum() is faster: ... > Not really; you measure the import and the attribute access time in > the second case. sum() is 2x faster for adding numbers only: Try

Re: Overwriting property-> can't set attribute

2008-08-22 Thread Raymond Hettinger
On Aug 22, 5:38 am, Gregor Horvath <[EMAIL PROTECTED]> wrote: > why is this code failing? > > class B(object): >      pass > > B.testattr = property(lambda s:"hallo") > b = B() > b.testattr = "test" First, property() only works when attached to classes, not instances. So the assignment should be:

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
On Aug 24, 5:32 am, Hussein B <[EMAIL PROTECTED]> wrote: > Hi, > I'm familiar with static method concept, but what is the class method? > how it does differ from static method? when to use it? I use them when I need alternative constructors for a class. class Angle(object): def __init__(self,

Re: use of Queue

2008-08-27 Thread Raymond Hettinger
On Aug 27, 4:55 pm, Paul Rubin wrote: > [EMAIL PROTECTED] writes: > > Or make the consumers daemon threads so that when the producers are finished > > an all non-daemon threads exit, the consumers do as well. > > How are the consumers supposed to know when the producers a

Re: Which is faster?

2008-08-30 Thread Raymond Hettinger
On Aug 29, 9:26 pm, cnb <[EMAIL PROTECTED]> wrote: > def av_grade(self): >      return sum(review.grade for review in self.reviews) / \ >               len(self.reviews) Minor point. Consider making the divisor: float(len(self.reviews)). It would be a bummer to throw-off the average because of f

Re: dict.update

2008-09-02 Thread Raymond Hettinger
On Sep 2, 8:04 am, Mike P <[EMAIL PROTECTED]> wrote: > Hi All, > > I have two dictionaries e.g > dict1 = {123:3,234:5,456:3} > dict2 = {123:4,157:2,234:5,456:3,567:2} > > I want to merge these two dictionaries together so i have a resultant > dictionary of: > > dict3 = {123:[4,3],157:[2,0],234:[5,5

Re: Profiling, sum-comprehension vs reduce

2008-09-16 Thread Raymond Hettinger
On Sep 13, 9:27 pm, Hrvoje Niksic <[EMAIL PROTECTED]> wrote: > Note that, despite appearances, it's not as built-in as one might > wish.  sum(seq) is still completely generic and works on all > number-like objects (in fact on all objects that define an __add__ > operation except strings, which are

Re: time series calculation in list comprehension?

2006-03-12 Thread Raymond Hettinger
[Peter Otten] > from __future__ import division > > from itertools import islice, tee, izip . . . > def moving_average2(items, n): > first_items, last_items = tee(items) > accu = sum(islice(last_items, n-1)) > for first, last in izip(first_items, last_items): > accu += last >

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

2006-03-12 Thread Raymond Hettinger
[robert] > In very rare cases a program crashes (hard to reproduce) : > > * several threads work on an object tree with dict's etc. in it. Items > are added, deleted, iteration over .keys() ... ). The threads are "good" > in such terms, that this core data structure is changed only by atomic > oper

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

2006-03-13 Thread Raymond Hettinger
John Savage wrote: > Could > someone please explain the rationale behind python designers' thinking > in deciding the function "range(1,12)" should return the sequence 1 to > 11 rather than the more intuitively-useful 1 to 12?? There are several ways to do this, closed intervals, half-open interva

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

2006-03-13 Thread Raymond Hettinger
[robert] > That queue/passing-through-only-an-extra-global-var communication is > acceptable for thin thread interaction. > ( hope this extra global var is thread-safe in future Python's :-) ) > > But "real" thread-programming should also be possible in Python - and it > is with the usual disciplin

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

2006-03-13 Thread Raymond Hettinger
[robert] > In very rare cases a program crashes (hard to reproduce) : > > * several threads work on an object tree with dict's etc. in it. Items > are added, deleted, iteration over .keys() ... ). The threads are "good" > in such terms, that this core data structure is changed only by atomic > oper

Re: Memory visualization

2006-03-14 Thread Raymond Hettinger
[bearophile] > During the execution of a Icon script there are ways to visualize the > memory: > http://www.cs.arizona.edu/icon/progvis/memmon/memmon.htm . . . > I'd like to know if for Python there is a similar program to > dynamically see the memory in a similar way. > If such tool doesn't exist

Re: trying to find repeated substrings with regular expression

2006-03-14 Thread Raymond Hettinger
[Robert Dodier] > I'm trying to find substrings that look like 'FOO blah blah blah' > in a string. For example give 'blah FOO blah1a blah1b FOO blah2 > FOO blah3a blah3b blah3b' I want to get three substrings, > 'FOO blah1a blah1b', 'FOO blah2', and 'FOO blah3a blah3b blah3b'. No need for regular

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
David Hirschfield wrote: > There isn't a thread pool module in the standard library, but I'm sure > many have been written by people in the python community. > Anyone have a favorite? Is there one particular implementation that's > recommended? Because of the GIL, thread pools are not as useful in

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
Azolex: > Let's push the diagnosis a bit further : the aversion to the keyword > "lambda" has to do with the fact that it ignores the english word used > by all non-geeks to convey the meaning, eg "given" Right. However, Guido has said that lambda is here to stay, so it's time to get over it. R

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
[Gregory Ewing] > >>I just posted to my blog about a feature that I'd like to see added to > >>Python. > > >>http://alphaios.blogspot.com/2009/11/python-string-inferred-names-wor... > > I don't think getattr and setattr are used anywhere near > frequently enough to justify special syntax. Perhaps

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. * there are many approa

Re: python bijection

2009-12-05 Thread Raymond Hettinger
[Me] > > * we've already got one (actually two). > >   The two dictionary approach... [Francis Carr] > Solutions such as bidict just automate the two-dict approach. They do so at the expense of implementing a new API to support it and at the expense with having non-obvious behaviors (i.e. how it

Re: python bijection

2009-12-05 Thread Raymond Hettinger
> >   ...sqlite3 provides another way... > > In many many cases, using a dB (even a lightweight such as sqlite3) is > swatting the fly with a sledgehammer :-) I'm sure it seems that way, but look at the generic description of the problem: "I have a list of n-ary tuples with named fields and would

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: http://www.python.org/dev/peps/pep-0232/ Raymond -- http://mail.python.o

Re: Float precision and float equality

2009-12-05 Thread Raymond Hettinger
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. > What is the best way to make sure that equality of floats will be > detected, where I assume that mismatches beyond a certain point are > due to truncation erro

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
[geremy condra] > I actually considered using dependencies as an example on the > "graphine for pythonistas"[1] article, but decided to do the maze > run instead. In any event, the uses of graphs in general computing > are well enough established that I don't really think that's where > the majorit

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
[Carl Banks] > > You're talking about machine epsilon?  I think everyone else here is > > talking about a number that is small relative to the expected smallest > > scale of the calculation. That was also my reading of the OP's question. The suggestion to use round() was along the lines of perfor

Efficient Running Median

2010-01-15 Thread Raymond Hettinger
I've updated the running median recipe to use a new algorithm with O (log n) updates for a large sliding window traversing a data stream. See http://code.activestate.com/recipes/576930/ The engine is a new collection class called IndexableSkiplist. It is like a regular skiplist as detailed at htt

Re: Performance of lists vs. list comprehensions

2010-01-19 Thread Raymond Hettinger
[Wolfram Hinderer] > Yes, list building from a generator expression *is* expensive. And > join has to do it, because it has to iterate twice over the iterable > passed in: once for calculating the memory needed for the joined > string, and once more to actually do the join (this is implementation >

Re: Sorted dictionary

2010-01-21 Thread Raymond Hettinger
On Jan 20, 5:02 pm, "Jan Kaliszewski" wrote: > Hello, > > Inspired by some my needs as well as some discussions in the net, I've   > implemented a sorted dictionary (i.e. a dict that returns keys, values and   > items always sorted by keys): > > http://code.activestate.com/recipes/576998/ > > Mayb

Re: Consume an iterable

2010-01-22 Thread Raymond Hettinger
On Jan 22, 6:13 am, Muhammad Alkarouri wrote: > In the python help for itertools, the following function is provided: > > def consume(iterator, n): >     "Advance the iterator n-steps ahead. If n is none, consume > entirely." >     collections.deque(islice(iterator, n), maxlen=0) > > What is the a

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

2010-01-23 Thread Raymond Hettinger
[Steve Howell] > Why wouldn't you get a competent C programmer simply make > list_ass_slice smart enough to make list.pop(0) O(1)? When this suggestion was discussed on python-dev years ago, it was rejected. One reason is that it was somewhat common for C code to access the list data structure di

Re: Consume an iterable

2010-01-24 Thread Raymond Hettinger
>      def consume2(iterator, n):  # the approved proposal (see #7764) >          if n is None: >              collections.deque(iterator, maxlen=0) >          else: >              next(islice(iterator, n, n), None) FWIW, the deque() approach becomes even faster in Py2.7 and Py3.1 which has a high

Re: Is defaultdict thread safe?

2010-01-25 Thread Raymond Hettinger
On Jan 25, 12:59 am, "Frank Millman" wrote: > Hi all > > Is defaultdict thread safe? Sometimes. It depends on whether an operation has callbacks to pure Python. > Assume I have - > >     from collections import defaultdict >     my_dict = defaultdict(list) > > If two threads call "my_dict['abc

Re: flow control and nested loops

2009-09-26 Thread Raymond Hettinger
On Sep 25, 12:01 pm, kj wrote: > In Perl, one can label loops for finer flow control.  For example: > > X: for my $x (@X) { >   Y: for my $y (@Y) { >     for my $z (@Z) { >       next X if test1($x, $y, $z); >       next Y if test2($x, $y, $z); >       frobnicate($x, $y, $z); >     } >     glortz(

Re: Business issues regarding adapting Python

2009-09-28 Thread Raymond Hettinger
On Sep 27, 12:13 am, Nash wrote: > Hello everyone, > > I'm a big time python fan and it has helped me write code fast and > push it out quickly. We have a medium sized telecom product written > 90% in Python and 10% in Java. The problem is, in the place where we > work (Pakistan), we can't find Py

Re: Q: sort's key and cmp parameters

2009-10-01 Thread Raymond Hettinger
On Oct 1, 10:08 am, kj wrote: > Challenge: to come up with a sorting task that cannot be achieved > by passing to the sort method (or sorted function) suitable values > for its key and reverse parameters, but instead *require* giving > a value to its cmp parameter. If you're assuming a consistent

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
[Paul Rubin] > Yes, think of sorting tree structures where you have to recursively > compare them til you find an unequal pair of nodes.   I'm not sure what you mean by this. What are the semantics of sorting a tree? Can you outline an example of tree that could be sorted easily with a cmp funct

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
[Paul Rubin] > The idea was that you have a list of trees that you want to sort, and > an ordering relation between trees: > >    def gt(tree1, tree2): ... > > where you recursively descend both trees until you find an unequal > pair of nodes.  You're not trying to sort the nodes within a single >

Re: Q: sort's key and cmp parameters

2009-10-02 Thread Raymond Hettinger
[Paul Rubin] > The idea was that you have a list of trees that you want to sort, and > an ordering relation between trees: > >    def gt(tree1, tree2): ... Are the trees user defined classes? Can the gt() function be added incorporated as __lt__ method so that you can just run a plain sort:

Re: Q: sort's key and cmp parameters

2009-10-04 Thread Raymond Hettinger
[Paul Rubin] > Example of list of trees (nested dicts). In practice you could get > such a list from the simplejson module: > > list_of_trees = [{'value':1, 'left':{'value':3,'left':None,'right':None}, >'right':{'value':7,'left':{'value':5, ...}}}, >

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
> > So, it looks like the relevant comparison values can be stored in > > nested lists: > > >list_of_lists = \ > > [[1, [3, [], []], > > [7, [5, [], []], []]], > > [19, [23, [], []], > > []], > > ] > > Now you've copied all the data out of the original tree,

Re: Q: sort's key and cmp parameters

2009-10-05 Thread Raymond Hettinger
> > >  Say you want to change the numeric comparisons so that > > > even numbers always sort before odd numbers, ie. > > >     -4 < -2 < 0 < 2 < 4 < ... < -999 < ... -1 < 1 < 3 ... > > > This is too easy: > > >     >>> s = [-2, 4, 2, -1, -3, 1, -4, 0, 3] > >     >>> s.sort() > >     >>> s.sort(key=

Re: Q: sort's key and cmp parameters

2009-10-06 Thread Raymond Hettinger
[bearophile] > I love the 'key', it makes my code simpler and it's simpler to > understand. I am not opposed to the idea of keeping cmp, that in some > rare cases may be useful or more natural. > > The problem is that if you allow to use the cmp, lot of programmers > will use it straight away, not

Re: bug with itertools.groupby?

2009-10-06 Thread Raymond Hettinger
On Oct 6, 4:06 pm, Kitlbast wrote: > Hi there, > > the code below on Python 2.5.2: > > from itertools import groupby > > info_list = [ >     {'profile': 'http://somesite.com/profile1', 'account': 61L}, >     {'profile': 'http://somesite.com/profile2', 'account': 64L}, >     {'profile': 'http://som

Re: Q: sort's key and cmp parameters

2009-10-07 Thread Raymond Hettinger
[Hrvoje Niksic] > Note that stable sort has additional memory requirements.  In situations > where you don't need stability, but do need memory-efficient in-place > sorting, an unstable sort might well be preferred.  This is why > libraries such as C++'s STL offer both. FWIW, the "additional memor

Re: Neural networks in python

2009-10-08 Thread Raymond Hettinger
On Oct 7, 10:53 pm, ruchir wrote: > I want to design and train a neural network in python. Can anyone > guide me, from where can I get some useful material/eBook/libraries > etc. for the same. I have no prior experience in neural netwoks and > want to implement it urgently. > Thanks in advance :)

Re: The rap against "while True:" loops

2009-10-12 Thread Raymond Hettinger
[kj] > I use "while True"-loops often, and intend to continue doing this > "while True", but I'm curious to know: how widespread is the > injunction against such loops?  Has it reached the status of "best > practice"? This is the first I've ever heard of such an quasi-injunction. Like you, I use "

<    1   2   3   4   5   6   7   8   >