From 6d91a555c368574ebf17f18536cc52108dff73c2 Mon Sep 17 00:00:00 2001 From: martin <maschu09@gmail.com> Date: Thu, 19 Dec 2019 18:15:00 +0100 Subject: [PATCH] First version of synop.py --- synop.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 synop.py diff --git a/synop.py b/synop.py new file mode 100644 index 0000000..4897144 --- /dev/null +++ b/synop.py @@ -0,0 +1,71 @@ +"""SynopReader class + +reads a nested JSON structure and flattens it for easier access""" + +import json +from collections import OrderedDict + +class SynopReader(): + + def __init__(self, filename): + """ + initializes object with filename and reads the JSON file + """ + # ToDo: interpret URL as alternative to filename and access JSON from URL if filename startswith http + with open(filename, 'r') as f: + content = f.read() + self.filename = filename + self.content = json.loads(content) + ## print(self.content) + + @staticmethod + def condense(d, keys=None): + """ + converts a "DWD dct" with key, value, units tags into dict entry key: (value, units) + :param d: the input dictionary + :param keys: existing key values (to avoid overwriting) + :return: dict with {key: (value, units)} + """ + print(d) + key = d.get('key', 'undefined_key') + value = d.get('value') + units = d.get('units') + # ToDo: improve this logic to make result more readable + if keys is not None and key in keys: + suffix = '123456789abcdefghijklmnopqrstuvwxyz' + key_c = [key+'_'+x for x in suffix] + for k in key_c: + if not k in keys: + key = key_c + break + return {key: (value, units)} + + def process_one(self, element): + """ + browse through one part of the JSON structure and condense all information + :return: condensed dict + """ + res = OrderedDict() + # check if current element is list + if isinstance(element, list): + for item in element: + res.update(self.process_one(item)) + return res + # ToDo: implement solution for list + # assume current element is dict + return self.condense(element, res.keys()) + + def process(self): + """ + browse through the entire JSON structure and condense all information + :return: condensed dict + """ + res = self.process_one(self.content) + print(res) + + + + +if __name__ == "__main__": + TESTDATA = "testdata/snippet_synop_dwd_opendata.json" + SynopReader(TESTDATA).process() \ No newline at end of file -- GitLab