|
| 1 | +# coding: utf-8 |
| 2 | +# Borrowed from https://github.com/amirziai/flatten |
| 3 | + |
| 4 | +# python 2 and python 3 compatibility library |
| 5 | +import six |
| 6 | + |
| 7 | + |
| 8 | +class Flatten(object): |
| 9 | + |
| 10 | + def __init__(self, nested_dict, separator=".", start_index=1, root_keys_to_ignore=None, replace_separators=None): |
| 11 | + """ |
| 12 | + :param nested_dict: dictionary we want to flatten |
| 13 | + :param start_index: start index for list param |
| 14 | + :param separator: string to separate dictionary keys by |
| 15 | + :param root_keys_to_ignore: set of root keys to ignore from flattening |
| 16 | + :param str replace_separators: Replace separators within keys |
| 17 | + """ |
| 18 | + self.nested_dict = nested_dict |
| 19 | + self.separator = separator |
| 20 | + self.start_index = start_index |
| 21 | + self.root_keys_to_ignore = root_keys_to_ignore |
| 22 | + self.replace_separators = replace_separators |
| 23 | + |
| 24 | + def flat(self): |
| 25 | + if not isinstance(self.nested_dict, dict): |
| 26 | + raise TypeError("flatten requires a dictionary input") |
| 27 | + |
| 28 | + if not isinstance(self.separator, six.string_types): |
| 29 | + raise TypeError("separator must be string") |
| 30 | + |
| 31 | + if self.root_keys_to_ignore is None: |
| 32 | + root_keys_to_ignore = set() |
| 33 | + |
| 34 | + if len(self.nested_dict) == 0: |
| 35 | + return {} |
| 36 | + |
| 37 | + # This global dictionary stores the flattened keys and values and is |
| 38 | + # ultimately returned |
| 39 | + flattened_dict = dict() |
| 40 | + |
| 41 | + def _flatten(object_, key): |
| 42 | + """ |
| 43 | + For dict, list and set objects_ calls itself on the elements and for |
| 44 | + other types assigns the object_ to |
| 45 | + the corresponding key in the global flattened_dict |
| 46 | + :param object_: object to flatten |
| 47 | + :param key: carries the concatenated key for the object_ |
| 48 | + :return: None |
| 49 | + """ |
| 50 | + # Empty object can't be iterated, take as is |
| 51 | + if not object_: |
| 52 | + flattened_dict[key] = object_ |
| 53 | + # These object types support iteration |
| 54 | + elif isinstance(object_, dict): |
| 55 | + for object_key in object_: |
| 56 | + if not (not key and object_key in root_keys_to_ignore): |
| 57 | + _flatten( |
| 58 | + object_[object_key], |
| 59 | + self._construct_key( |
| 60 | + key, |
| 61 | + self.separator, |
| 62 | + object_key, |
| 63 | + replace_separators=self.replace_separators)) |
| 64 | + elif isinstance(object_, (list, set, tuple)): |
| 65 | + for index, item in enumerate(object_): |
| 66 | + _flatten( |
| 67 | + item, |
| 68 | + self._construct_key( |
| 69 | + key, |
| 70 | + self.separator, |
| 71 | + index + self.start_index, |
| 72 | + replace_separators=self.replace_separators)) |
| 73 | + # Anything left take as is |
| 74 | + else: |
| 75 | + flattened_dict[key] = object_ |
| 76 | + |
| 77 | + _flatten(self.nested_dict, None) |
| 78 | + return flattened_dict |
| 79 | + |
| 80 | + def _construct_key(self, previous_key, separator, new_key, replace_separators=None): |
| 81 | + """ |
| 82 | + Returns the new_key if no previous key exists, otherwise concatenates |
| 83 | + previous key, separator, and new_key |
| 84 | + :param previous_key: |
| 85 | + :param separator: |
| 86 | + :param new_key: |
| 87 | + :param str replace_separators: Replace separators within keys |
| 88 | + :return: a string if previous_key exists and simply passes through the |
| 89 | + new_key otherwise |
| 90 | + """ |
| 91 | + if replace_separators is not None: |
| 92 | + new_key = str(new_key).replace(separator, replace_separators) |
| 93 | + if previous_key: |
| 94 | + return u"{}{}{}".format(previous_key, separator, new_key) |
| 95 | + else: |
| 96 | + return new_key |
0 commit comments