Last active
October 15, 2020 11:37
Revisions
-
andreastt revised this gist
Oct 15, 2020 . 1 changed file with 24 additions and 15 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,15 +1,24 @@ import json class pathdict(dict): def get(self, path, default=None): return get(self, path, default=default) def get(data, path, default=None): cur, _, rest = path.partition(".") if cur in data: if len(rest) > 0: return get(data[cur], rest) else: return data[cur] return default def load(fp, *args, **kwargs): return json.load(fp, *args, object_hook=pathdict, **kwargs) def loads(s, *args, **kwargs): return json.loads(s, *args, object_hook=pathdict, **kwargs) -
andreastt renamed this gist
Oct 15, 2020 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
andreastt created this gist
Oct 15, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ def get_path(json, path, default=None): cur, _, rest = path.partition(".") if cur in json: if len(rest) > 0: return get_path(json[cur], rest) else: return json[cur] return default if __name__ == "__main__": assert get_path({"foo": {"bar": 42}}, "foo.bar") == 42 assert get_path({"foo": "bar"}, "foo") == "bar" assert get_path({}, "foo") == None assert get_path({}, "foo", "bar") == "bar"