Reputation: 1163
It is time to make my first question here. I am facing the following issue:
i am using the pickle module to dump a large dictionary and then load it from the disk back. The problem is that after unpickling the two objects are not the same. Actually this is what I am doing:
In the file A.py
I define my class that has some attributes and methods.
In file B.py
I make a dictionary where the values are instances of the class in A.py
Also in file B.py
I pickle this dictionary and unpickle it again.
The two dicts are not the same. I checked the keys and are the same. The problem lies with the values.
Any ideas?
Upvotes: 0
Views: 438
Reputation: 1163
the instances of the class that are actually the values of the dictionary looks like:
class SchemaObject:
def __init__(self):
self.type = ''
self.name = ''
self.parentdn = ''
self.dn = ''
self.oclass = ''
def initWithXMLNode(self, xmlnode, parentdn):
self.type = ''
self.name = ''
self.parentdn = parentdn
if xmlnode.nodeName=='fragments':
self.dn = parentdn
if xmlnode.nodeName=='fragment':
self.initFragment(xmlnode)
elif xmlnode.nodeName=='configGroupLdap':
self.initGroup(xmlnode)
elif xmlnode.nodeName=='configObjectLdap':
self.initObject(xmlnode)
def initWithFragment(self, dn, parentdn, name):
self.type = 'object'
self.name = name
self.parentdn = parentdn
self.dn = dn
self.oclass = name
def initFragment(self, xmlnode):
self.type = 'fragment'
self.dn = 'fsFragmentId=' + xmlnode.firstChild.nodeValue + ',' + self.parentdn
self.oclass = 'FSFragment'
def initGroup(self, xmlnode):
self.type = 'group'
self.name = 'group-' + xmlnode.getAttribute('name')
self.dn = xmlnode.getAttribute('dn')
self.oclass = 'FSFragment'
def initObject(self, xmlnode):
self.type = 'object'
self.name = xmlnode.getAttribute('name')
self.oclass = self.name
if not xmlnode.hasAttribute('rdnname'):
self.type = 'no_rdnname'
return
else:
rdnname = xmlnode.getAttribute('rdnname')
parts = rdnname.split(',')
if xmlnode.getAttribute('multiple')!='true':
dn = self.parentdn
for part in reversed(parts):
dn = 'fsFragmentId=' + part + ',' + dn
self.dn = dn
else:
self.type = ''
self.dn = 'fsFragmentId=' + parts[len(parts)-1] + ',' + self.parentdn
dynamicStatics.append(self.oclass)
and in file B.py i create a dictionary that its values are based on this class. the dict is the my_dict. and i have also checked that the keys are equal. only when i try to compare two values between these dictionaries it fails.
so i try to pickle it with:
with open('my_dumped.pkl','wb') as schema:
pickle.dump(my_dict,schema)
and when trying to restore it from disk:
with open('my_dumped.pkl','rb') as schema:
b = pickle.load(schema)
if now i issue my_dumped == b shouldn't this return True?i do not care for identity.just for equality.that as marteanu said the keys are all there,and each key has the correct value.
the above equality returns False unfortunately.
Upvotes: 0
Reputation: 123473
Two different dictionaries with the same keys & values would not be considered identical:
>>> d1 = {'k1': 12345}
>>> d2 = {'k1': 12345}
>>> d1 is d2
False
>>> d1['k1'] is d2['k1']
False
Even if you actually copy the values from one to the other:
>>> d1['k1'] = d2['k1']
>>> d1['k1'] is d2['k1']
True
>>> d1 is d2
False
That's because each one is a separate container. On the other hand, you could check if all the keys and values have equal values rather than literally being identical objects using an expression like this:
(set(d1) == set(d2)) and all(d1[k] == d2[k] for k in d1)
The first subexpression makes sure each has keys with the same values and the second that the values associated with each of these keys are equal.
When you reconstitute the dictionary from the saved data a new one is created. It won't be the same dictionary but all its keys and values should be equal to the original. If that is indeed not the case, please provide us with at least the code you're using to determine they are not the same.
Upvotes: 2