[Zope-ZEO] Need for PersistentList
John D. Heintz
jheintz@isogen.com
Fri, 19 Jan 2001 08:53:59 -0600
This is a multi-part message in MIME format.
--------------030506020605070609000305
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
Well, I thought about what you said and now I'm really confused. The
attached script is doing thing that I just don't understand. It seems
that Persistent object that implement a __hash__ method are indeed
hashable, but dictionaries ignore the result of __hash__. Also, it
looks like the hashing is consitent across db connections.
John
Andrew Kuchling wrote:
> On Thu, Jan 18, 2001 at 04:35:37PM -0600, John D. Heintz wrote:
>
>> Finally, we need for Persistent classes to be hashable.
>
>
> Hmm... Does __hash__() not work on an ExtensionClass? Python will use
> the id() of an object, but that's obviously not applicable to a
> persistent object. The OID can't be used either, since no OID is
> assigned until you do a commit. I'd argue that, if a persistent class
> doesn't explicitly define __hash__, it shouldn't be hashable at all
> (in which case I've got some code that will break :) ).
>
> --amk
>
>
>
> _______________________________________________
> Bug reports, feature requests, etc. go in the ZEO Tracker:
> http://www.zope.org/Products/ZEO/Tracker
>
> Conversations etc. can take place in the Wiki:
> http://www.zope.org/Products/ZEO/Wiki
>
> Zope-ZEO maillist - Zope-ZEO@zope.org
> http://lists.zope.org/mailman/listinfo/zope-zeo
--
. . . . . . . . . . . . . . . . . . . . . . . .
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
--------------030506020605070609000305
Content-Type: text/plain;
name="testHashing2.py"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename="testHashing2.py"
import Persistence
import ZODB
from PersistentMapping import PersistentMapping
from ZODB.POSException import ConflictError
import threading
from ZODB import DemoStorage
storage = DemoStorage.DemoStorage()
db = ZODB.DB(storage)
class P1(Persistence.Persistent):
def __init__(self, name):
self.name = name
class P2(Persistence.Persistent):
def __init__(self, name):
self.name = name
def __hash__(self):
return hash(None)
#Show that P1 is not hashable
try:
{P1('a'):1}
except:
print "P1 is not hashable."
else:
print "P1 is hashable!!!!"
# Open initial connection and create a PersistentMapping
# with a two P2 instances
conn1 = db.open()
root1 = conn1.root()
p2A = P2('a')
print hash(p2A)
p2B = P2('b')
print hash(p2B)
root1['p2List'] = (p2A, p2B)
p2Map = PersistentMapping()
p2Map[p2A] = 'a'
p2Map[p2B] = 'b'
assert len(p2Map) == 2
root1['p2Map'] = p2Map
print p2Map._container
get_transaction().commit()
# Get second connection and retrieve maps
conn2 = db.open()
root2 = conn2.root()
p2List = root2['p2List']
p2Map = root2['p2Map']
print p2Map._container
for p2 in p2List:
print hash(p2)
assert p2.name == p2Map[p2]
--------------030506020605070609000305--