python - Pickle serialization order mystery -


update 6/8/17

though 3 years passed, pr still pending temporary solution enforcing output order. stream-framework might reconsider design on using content key notifications. github issue #153 references this.

question

see following sample:

import pickle x = {'order_number': 'x', 'deal_url': 'j'}  pickle.dumps(x) pickle.dumps(pickle.loads(pickle.dumps(x))) pickle.dumps(pickle.loads(pickle.dumps(pickle.loads(pickle.dumps(x))))) 

results:

(dp0\ns'deal_url'\np1\ns'j'\np2\nss'order_number'\np3\ns'x'\np4\ns. (dp0\ns'order_number'\np1\ns'x'\np2\nss'deal_url'\np3\ns'j'\np4\ns. (dp0\ns'deal_url'\np1\ns'j'\np2\nss'order_number'\np3\ns'x'\np4\ns. 

clearly, serialized output changes every dump. when remove character of keys, doesn't happen. discovered stream-framework use pickled output key storage of notifications on k/v store. pull request if better understanding going on here. have found 2 solutions prevent it:

a - convert dictionary after sorting (yes, somehow provides intended side effect)

import operator sorted_x = dict(sorted(x.iteritems(), key=operator.itemgetter(1))) 

b - remove underscores (but not sure if works)

so causes mystery under dictionary sorting pickle?

proof calling sort on dict provides dump produce same result:

import operator x = dict(sorted(x.iteritems(), key=operator.itemgetter(1)))  pickle.dumps(x) "(dp0\ns'order_number'\np1\ns'x'\np2\nss'deal_url'\np3\ns'j'\np4\ns."  x = pickle.loads(pickle.dumps(x)) x = dict(sorted(x.iteritems(), key=operator.itemgetter(1)))  pickle.dumps(x) "(dp0\ns'order_number'\np1\ns'x'\np2\nss'deal_url'\np3\ns'j'\np4\ns." 

dictionaries unsorted data structures. means order arbitrary , pickle store them are. can use collections.ordereddict if want use sorted dictionary.

any order think see when you're playing around in interpreter interpreter playing nice you.

from documentation of dict:

it best think of dictionary unordered set of key: value pairs, requirement keys unique (within 1 dictionary)

remember functions dict.keys(), dict.values() , dict.items() return respective values in arbitrary order.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -