From 0ff238a1e57609d8e2eb3e5f4055112b39fe978d Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Thu, 31 Jan 2019 11:45:06 +0100 Subject: add data_parameters (numeric description of JSON-like data) --- lib/data_parameters.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 lib/data_parameters.py (limited to 'lib') diff --git a/lib/data_parameters.py b/lib/data_parameters.py new file mode 100644 index 0000000..8f1fbb0 --- /dev/null +++ b/lib/data_parameters.py @@ -0,0 +1,48 @@ +""" +Utilities for parameter extraction from data layout. + +Parameters include the amount of keys, length of strings (both keys and values), +length of lists, ane more. +""" + +def _string_value_length(json): + if type(json) == str: + return len(json) + + if type(json) == dict: + return sum(map(_string_value_length, json.values())) + + if type(json) == list: + return sum(map(_string_value_length, json)) + + return 0 + +def _string_key_length(json): + if type(json) == dict: + return sum(map(len, json.keys())) + sum(map(_string_key_length, json.values())) + + return 0 + +def _num_keys(json): + if type(json) == dict: + return len(json.keys()) + sum(map(_num_keys, json.values())) + + return 0 + +def _num_objects(json): + if type(json) == dict: + return 1 + sum(map(_num_objects, json.values())) + + return 0 + +def json_to_param(json): + """Return numeric parameters describing the structure of JSON data.""" + + ret = dict() + + ret['strlen_keys'] = _string_key_length(json) + ret['strlen_values'] = _string_value_length(json) + ret['num_keys'] = _num_keys(json) + ret['num_objects'] = _num_objects(json) + + return ret -- cgit v1.2.3