Fixed Re: [ZODB-Dev] Help!
JohnD.Heintz
JohnD.Heintz
Wed, 15 Aug 2001 19:42:12 -0500
After learning a lot more about __cmp__(self, other) I can report back th=
at=20
everything is working.
We had insufficient handling for when __cmp__ was called on newly created=
=20
Persistent objects. We had __hash__ working correctly, but the way BTree=
s=20
worked didn't bother with that.
For everyone benifit I've included our HashablePersistent code. Please l=
et=20
me know if I've messed anything else up, but it seems to be working for u=
s.
John
class HashablePersistent(Persistent):
def __hash__(self):
"""
Trivial __hash__ implementation to get identity
"""
if not self._p_oid:
someGlobal.ensurePid(self)
result =3D hash(self._p_oid)
return result
def __cmp__(self, other):
if self._p_oid is None:
someGlobal.ensurePid(self)
if id(self) =3D=3D id(other):
return 0
if other is None:
return -1
=20
try:
if other._p_oid is None:
someGlobal.ensurePid(other)
return cmp(self._p_oid, other._p_oid)
except AttributeError:
return 1
On Wednesday 15 August 2001 18:07, John D. Heintz wrote:
> The following code raises an AssertionError because the BTree doesn't u=
se
> the Python defined __hash__ methods.
>
> Should I log this as a Collector bug or am I doing something wrong?
>
> John
>
>
> import ZODB
> from BTrees.OOBTree import OOBTree
> from ZODB import Persistence
>
> class HashablePersistent(Persistence.Persistent):
> """
> """
> def __init__(self):
> self.__version =3D "1.0"
> self.hashCalled =3D 0
>
> def __hash__(self):
> """
> Trivial __hash__ implementation to get identity
> """
> self.hashCalled =3D 1
> return 5
>
> def __cmp__(self, other):
> return 0 # I know this needs to be implemented
>
> p =3D HashablePersistent()
> dict =3D {}
> dict[p] =3D p
> assert p.hashCalled
>
> p =3D HashablePersistent()
> btree =3D OOBTree()
> btree[p] =3D p
> assert p.hashCalled # !!This fails!!
--=20
=2E . . . . . . . . . . . . . . . . . . . . . . .
John D. Heintz | Senior Engineer
1016 La Posada Dr. | Suite 240 | Austin TX 78752
T 512.633.1198 | jheintz@isogen.com
w w w . d a t a c h a n n e l . c o m