diff options
author | Daniel Friesel <derf@finalrewind.org> | 2019-01-31 11:45:06 +0100 |
---|---|---|
committer | Daniel Friesel <derf@finalrewind.org> | 2019-01-31 11:45:25 +0100 |
commit | 0ff238a1e57609d8e2eb3e5f4055112b39fe978d (patch) | |
tree | c9541ddad97e13089327f7cef6b2887c28e0951a /lib | |
parent | cc239d90f86c843dd7d9edd911c6837dbd6e8208 (diff) |
add data_parameters (numeric description of JSON-like data)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/data_parameters.py | 48 |
1 files changed, 48 insertions, 0 deletions
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 |