Created
November 11, 2014 03:36
-
-
Save adoc/da9b80ca6de4315972e2 to your computer and use it in GitHub Desktop.
py: Freeze a nested dict, tuple and/or list. Used to check whether a given state has changed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def freeze(obj): | |
""" | |
""" | |
def _freeze(obj): | |
def do_list(): | |
for i in obj: | |
yield _freeze(i) | |
def do_dict(): | |
for k, v in obj.items(): | |
yield k, _freeze(v) | |
if isinstance(obj, tuple): | |
return tuple(do_list()) | |
elif isinstance(obj, list): | |
return tuple(do_list()) | |
elif isinstance(obj, dict): | |
return tuple(sorted(dict(do_dict()).items())) | |
else: | |
return obj | |
return frozenset(_freeze(obj)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment