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
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