def generic_codeable_to_dict(codeable, prefix=""): "Convert dict/list/str contains code data to a flat dictionary" if isinstance(codeable, float) and math.isnan(codeable): return {} if isinstance(codeable, list): return concat_dicts( [generic_codeable_to_dict(d, prefix) for d in codeable]) if not isinstance(codeable, dict): return {prefix: codeable} codeable, prefix = flatten_and_find_prefix(codeable, prefix) # Recurse pre-processed value is not a dictionary (but a list for example) if not isinstance(codeable, dict): return generic_codeable_to_dict(codeable, prefix) def prefixer(dictionary): return prefix_dict_keys(dictionary, prefix) # TODO: Add test case for valueString # if 'valueString' in codeable: # return prefixer(value_string_to_dict(codeable)) # TODO: Add test case for single value that is a url # keys = codeable.keys() # if len(keys) == 1 and 'url' in codeable: # return prefixer({key: system_to_column(codeable[key]) + '+'}) result = prefixer( concat_dicts( [generic_codeable_to_dict(v, k) for k, v in codeable.items()])) return result
def expand_address_attr(key, attr_value): if type(attr_value) is dict: return generic_codeable_to_dict(attr_value, key) if type(attr_value) is list: return concat_dicts([ expand_address_attr(f"{key}_{index}", value) for index, value in enumerate(attr_value) ]) return {key: attr_value}
def test_concat_dicts_with_prefix(): assert concat_dicts([{ "a": 1 }, { "b": 2 }, { "c": 3 }], prefix="tag") == { "tag_a": 1, "tag_b": 2, "tag_c": 3, }
def test_concat_dicts(): assert concat_dicts([{ "a": 1 }, { "b": 2 }, { "c": 3 }]) == { "a": 1, "b": 2, "c": 3, }
def test_concat_dicts_with_duplicates(): assert concat_dicts([{ "a": 1 }, { "a": 2 }, { "a": 3, "b": 0 }]) == { "a": 1, "a_1": 2, "a_2": 3, "b": 0, }
def expand_name_value(value): if type(value) is not list or len(value) == 0: return {} primary_name = first(filter(lambda v: v.get("use") == "official", value)) or first(value) other_names = list(filter(lambda name: name != primary_name, value)) other_attrs = {"other_names": other_names} if len(other_names) > 0 else {} return { **concat_dicts( [ __expand_key_value(key, value) for key, value in primary_name.items( ) if key not in NAME_BLACKLIST_KEYS ], "name", ), **other_attrs, }
def expand_address_value(value): if type(value) is not list: return {} primary_address = first(filter(lambda v: v.get("use") != "old", value)) or first(value) other_addresses = list( filter(lambda address: address != primary_address, value)) other_attrs = ({ "other_addresses": other_addresses } if len(other_addresses) > 0 else {}) return { **concat_dicts([ expand_address_attr(f"address_{key}", item_value) for key, item_value in primary_address.items( ) if key != "text" ]), **other_attrs, }
def merge_codeable_and_prefix(codeable, prefix): if isinstance(codeable, dict): return prefix_dict_keys(codeable, prefix) else: return concat_dicts(codeable, prefix)