开发者

Python: Compare dict with a static reference?

开发者 https://www.devze.com 2023-01-18 02:30 出处:网络
I have to开发者_如何学JAVA check if a dictionary is the same as it was yesterday, if it has changed.

I have to开发者_如何学JAVA check if a dictionary is the same as it was yesterday, if it has changed.

In PHP I could have serialized an array and compared the resulting strings from yesterday and today. However, I don't know how to do it in Py. I've read a little about Pickle and maybe it could be done with md5 somehow?

So basically I need a way to dismantle a dict into a comparable, storable value that is not a file and can be hardcoded into .py file.

Thanks, A.R.


The problem with dictionaries is their undefined order. You must make sure you always get the same result of equal dictionaries (if you want to compare them as strings).

You could do it in multiple ways:

1) Python hash (only for checking equality; hash implementation might be specific to the Python version!)

print hash(str(sorted({1 : 2, 3 : 4}.items())))

2) MD5 (best if you only want to check equality)

import hashlib
print hashlib.md5(str(sorted({1 : 2, 3 : 4}.items()))).hexdigest()

3) Pickling (serialization)

import pickle
serializedString = pickle.dumps({1 : 2, 3 : 4})

The pickle module has different protocols (and I think it doesn't sort the dictionary items), so you can't do string comparison. You have to unpickle the data to a dictionary and then compare the old and new dictionary directly (d = pickle.loads(serializedString)).

4) Item tuple representation (serialization)

According to your comment, you want something embeddable into Python source code. As S.Lott suggested, you can use the object representation of someDictionary.items(), which is a list containing all (key, value) combinations as tuples (most probably unsorted):

>>> repr({1 : 2, 3 : 4}.items())
'[(1, 2), (3, 4)]'

You can copy-and-paste this representation into your source code if you want the object serialized as a string .


You could use JSON to get what you are asking for.

from django.utils import simplejson as json

dict_1 = {'a': 1, 'b': 2, 'c': 3}
dict_2 = {'a': 2, 'b': 7, 'd': 9}

dict_1_str = json.dumps(dict_1, sort_keys=True)
dict_2_str = json.dumps(dict_2, sort_keys=True)

if dict_1_str == dict_2_str:
   # do something with the new dict...
   pass

The dict_X_str variables will contain a serialized version of the dict. You can store it in memcahce or the datastore for later comparison.


pickle is definitely what you're looking for.


pickle.dumps(set(yourdict.items()))


By using pickle you could create a new DictProperty allowing you to put the dict in the datastore and retrieve it later for comparison.

Here is one implementation: Can I store a python dictionary in google's BigTable datastore without serializing it explicitly?


If you do this, you get an object which is consistent and comparable. It has a well-defined and predictable order. You can use difflib to find differences. Further, you can trivially rebuild the high-performance dictionary from it.

static = list(sorted(some_dict.items()))

a_dict= dict( static )
0

精彩评论

暂无评论...
验证码 换一张
取 消