dictionary 'hash'

G

gyro

Hi,
I have a collection of objects that I am currently trying to manage.
Each object is initialized with a dictionary. The dictionaries have the
same keys but may have different values. The initialization process is
time consuming, so I don't want to create an object if I have already
instantiated one using a dictionary with the 'same' items.

I would like to collect the objects in a dictionary with the objects as
values and some identifier or hash of the initializing dictionary as the
keys.


For instance:


idicts = [{'k1':val1,'k2':val2},
{'k1':val3,'k2':val4},
{'k1':val1,'k2':val2}]

for idict in idicts:
dhash = somehashingfunction(idict)
if not objectstoragedict.has_key(dhash):
newobj = MyClass(idict)
objectstoragedict[dhash] = newobj


Any ideas for 'somehashingfunction'?
The values in idict may be strings, integers, or instances of other
classes I have created.

Presently, I am considering something like

def somehashingfunction(rdict):
import sha
dkeys = rdict.keys()
dkeys.sort()
rvals = [repr(rdict[key]) for key in dkeys]
rstr = '_'.join(rvals)
dhash = sha.new(rstr).hexdigest()
return dhash


I'd appreciate any suggestions or comments.

Thanks.

-g
 
A

anton muhin

gyro said:
Hi,
I have a collection of objects that I am currently trying to manage.
Each object is initialized with a dictionary. The dictionaries have the
same keys but may have different values. The initialization process is
time consuming, so I don't want to create an object if I have already
instantiated one using a dictionary with the 'same' items.

I would like to collect the objects in a dictionary with the objects as
values and some identifier or hash of the initializing dictionary as the
keys.


For instance:


idicts = [{'k1':val1,'k2':val2},
{'k1':val3,'k2':val4},
{'k1':val1,'k2':val2}]

for idict in idicts:
dhash = somehashingfunction(idict)
if not objectstoragedict.has_key(dhash):
newobj = MyClass(idict)
objectstoragedict[dhash] = newobj


Any ideas for 'somehashingfunction'?
The values in idict may be strings, integers, or instances of other
classes I have created.

Presently, I am considering something like

def somehashingfunction(rdict):
import sha
dkeys = rdict.keys()
dkeys.sort()
rvals = [repr(rdict[key]) for key in dkeys]
rstr = '_'.join(rvals)
dhash = sha.new(rstr).hexdigest()
return dhash


I'd appreciate any suggestions or comments.

Thanks.

-g

Actually, it seems that the problem might be bypassed by other design,
but you know it better. If you really need it, you approach seems
resonable. However, sometimes you can store data as a tuple or list and
map keys to inidices---in this case you can use standard comparision
operators. A word of caution: if store mutable objects, there might be
some problems.

hth,
anton.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top