Re: hashkey/digest for a complex object

2010-10-10 Thread kj
In <4cb1dc9a$0$29970$c3e8da3$54964...@news.astraweb.com> Steven D'Aprano writes: >Reading the source code is also a good approach. Every time I have attempted to answer a question by looking at the Python C source, all I've achieved was wasting time, sometimes a *lot* of time, so by now I've de

Re: hashkey/digest for a complex object

2010-10-10 Thread Hrvoje Niksic
Arnaud Delobelle writes: > I have learnt too that hash(-1) is not (-1), and that it seems that a > hash value is not allowed to be (-1). There is one thing left to find > out. Why can't it be (-1)? Because -1 has a special meaning in the C function equivalent to Python's hash(). PyObject_Hash

Re: hashkey/digest for a complex object

2010-10-10 Thread Arnaud Delobelle
kj writes: > In <87hbgu8irb@gmail.com> Arnaud Delobelle writes: [...] >>And, in fact, >>(-1) is the only int such that hash(x) != x. > > Arnaud, how did you determine that -1 is the only such int? I > can't imagine any other approach other than a brute-force check of > all ints... When I

Re: hashkey/digest for a complex object

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 13:49:09 +, kj wrote: >>> to give only two of an infinite number of counter-examples. > > (Infinite???) I was mistaken. Given Arnaud's specification that we look only at the Python 2.x ints, I believe that there is only one counter-example, namely -1. However, in Pytho

Re: hashkey/digest for a complex object

2010-10-10 Thread Steven D'Aprano
On Sun, 10 Oct 2010 11:06:00 +0100, Arnaud Delobelle wrote: > Steven D'Aprano writes: > >> On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote: >> >>> 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) >>> hold for any hashable x (this is a simple consequence of the fact t

Re: hashkey/digest for a complex object

2010-10-10 Thread kj
In <87hbgu8irb@gmail.com> Arnaud Delobelle writes: >Steven D'Aprano writes: >> On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote: >> >>> 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold >>> for any hashable x (this is a simple consequence of the fact that >>>

Re: hashkey/digest for a complex object

2010-10-10 Thread Arnaud Delobelle
Steven D'Aprano writes: > On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote: > >> 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold >> for any hashable x (this is a simple consequence of the fact that >> hash(x) == x for any int x (by 'int' I mean 2.X int)). > > It'

Re: hashkey/digest for a complex object

2010-10-10 Thread Hrvoje Niksic
Steven D'Aprano writes: > On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote: > >> 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold >> for any hashable x (this is a simple consequence of the fact that >> hash(x) == x for any int x (by 'int' I mean 2.X int)). > > It'

Re: hashkey/digest for a complex object

2010-10-09 Thread Steven D'Aprano
On Sat, 09 Oct 2010 21:39:51 +0100, Arnaud Delobelle wrote: > 1. hash() is an idempotent function, i.e. hash(hash(x)) == hash(x) hold > for any hashable x (this is a simple consequence of the fact that > hash(x) == x for any int x (by 'int' I mean 2.X int)). It's a beautiful theory, but, alas, it

Re: hashkey/digest for a complex object

2010-10-09 Thread Arnaud Delobelle
kj writes: > In <87y6a9lqnj@gmail.com> Arnaud Delobelle writes: > >>You could do something like this: > >>deep_methods = { >>list: lambda f, l: tuple(map(f, l)), >>dict: lambda f, d: frozenset((k, f(v)) for k, v in d.items()), >>set: lambda f, s: frozenset(map(f, s)), >>

Re: hashkey/digest for a complex object

2010-10-09 Thread kj
In <87y6a9lqnj@gmail.com> Arnaud Delobelle writes: >You could do something like this: >deep_methods = { >list: lambda f, l: tuple(map(f, l)), >dict: lambda f, d: frozenset((k, f(v)) for k, v in d.items()), >set: lambda f, s: frozenset(map(f, s)), ># Add more if needed

Re: hashkey/digest for a complex object

2010-10-07 Thread Arnaud Delobelle
kj writes: > The short version of this question is: where can I find the algorithm > used by the tuple class's __hash__ method? > > Now, for the long version of this question, I'm working with some > complext Python objects that I want to be able to compare for > equality easily. > > These object

Re: hashkey/digest for a complex object

2010-10-07 Thread MRAB
On 07/10/2010 11:42, kj wrote: In Terry Reedy writes: If these two attributes, and hence the dicts, are public, then your instances are mutable. I guess I should have written "immutable among consenting adults." As far as I know, Python does not support private attributes, so I guess the

Re: hashkey/digest for a complex object

2010-10-07 Thread Diez B. Roggisch
kj writes: > In <87pqvmp611@web.de> de...@web.de (Diez B. Roggisch) writes: > >>I tried codesearch first. Not satisfied after 30 seconds with the >>results, I did the most straight forward thing - I downloaded and >>un-packed the python source... and took a look. > >>From that I learned the

Re: hashkey/digest for a complex object

2010-10-07 Thread kj
In <87pqvmp611@web.de> de...@web.de (Diez B. Roggisch) writes: >I tried codesearch first. Not satisfied after 30 seconds with the >results, I did the most straight forward thing - I downloaded and >un-packed the python source... and took a look. >From that I learned the tuplehash function na

Re: hashkey/digest for a complex object

2010-10-07 Thread Diez B. Roggisch
kj writes: > In de...@web.de (Diez B. Roggisch) writes: > >>kj writes: > >>> The short version of this question is: where can I find the algorithm >>> used by the tuple class's __hash__ method? > >>Surprisingly, in the source: > >>http://google.com/codesearch/p?hl=de#-2BKs-LW4I0/trunk/python/sr

Re: hashkey/digest for a complex object

2010-10-07 Thread kj
In de...@web.de (Diez B. Roggisch) writes: >kj writes: >> The short version of this question is: where can I find the algorithm >> used by the tuple class's __hash__ method? >Surprisingly, in the source: >http://google.com/codesearch/p?hl=de#-2BKs-LW4I0/trunk/python/src/Objects/tupleobject.c&

Re: hashkey/digest for a complex object

2010-10-07 Thread kj
In Terry Reedy writes: >If these two attributes, and hence the dicts, are public, then your >instances are mutable. I guess I should have written "immutable among consenting adults." As far as I know, Python does not support private attributes, so I guess the dicts are public no matter what

Re: hashkey/digest for a complex object

2010-10-07 Thread Terry Reedy
On 10/6/2010 2:58 PM, kj wrote: These objects are non-mutable once they are created, See below. like to use a two-step comparison for equality, based on the assumption that I can compute (either at creation time, or as needed and memoized) a hashkey/digest for each object. The test for equa

Re: hashkey/digest for a complex object

2010-10-06 Thread Robert Kern
On 10/6/10 1:58 PM, kj wrote: The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? The function tuplehash() in Objects/tupleobject.c, predictably enough. Now, for the long version of this question, I'm working with some complext Pyt

Re: hashkey/digest for a complex object

2010-10-06 Thread Diez B. Roggisch
kj writes: > The short version of this question is: where can I find the algorithm > used by the tuple class's __hash__ method? Surprisingly, in the source: http://google.com/codesearch/p?hl=de#-2BKs-LW4I0/trunk/python/src/Objects/tupleobject.c&q=python%20tuplehash&sa=N&cd=1&ct=rc > Now, for t

Re: hashkey/digest for a complex object

2010-10-06 Thread geremy condra
On Wed, Oct 6, 2010 at 11:58 AM, kj wrote: > > > The short version of this question is: where can I find the algorithm > used by the tuple class's __hash__ method? >From Objects/tuple.c, line 315 in Python3.2: static long tuplehash(PyTupleObject *v) { register long x, y; register Py_ssiz

Re: hashkey/digest for a complex object

2010-10-06 Thread Duncan Booth
kj wrote: > The short version of this question is: where can I find the algorithm > used by the tuple class's __hash__ method? > http://svn.python.org/view/python/trunk/Objects/tupleobject.c?revision=81029&view=markup static long tuplehash(PyTupleObject *v) { register long x, y; regist

hashkey/digest for a complex object

2010-10-06 Thread kj
The short version of this question is: where can I find the algorithm used by the tuple class's __hash__ method? Now, for the long version of this question, I'm working with some complext Python objects that I want to be able to compare for equality easily. These objects are non-mutable once th