def flatten_dict(self, d, delimiter="<STR_LIT:->", intermediates=False, parent_key=None):
items = []<EOL>if isinstance(d, list):<EOL><INDENT>d = dict(enumerate(d))<EOL><DEDENT>for k, v in d.items():<EOL><INDENT>if parent_key:<EOL><INDENT>k = u"<STR_LIT>".format(parent_key, delimiter, k)<EOL><DEDENT>if intermediates:<EOL><INDENT>items.append((k, v))<EOL><DEDENT>if isinstance(v, list):<EOL><INDENT>v = dict(enumerate(v))<EOL><DEDENT>if isinstance(v, collections.Mapping):<EOL><INDENT>items.extend(<EOL>self.flatten_dict(v, delimiter, intermediates, str(k)).items()<EOL>)<EOL><DEDENT>else:<EOL><INDENT>items.append((str(k), v))<EOL><DEDENT><DEDENT>return dict(items)<EOL>
Flatten a dictionary.

Values that are dictionaries are flattened using delimiter in between
(eg. parent-child)

Values that are lists are flattened using delimiter
followed by the index (eg. parent-0)

example:

.. code-block:: python

    {
        'fish_facts': {
            'sharks': 'Most will drown if they stop moving',
            'skates': 'More than 200 species',
        },
        'fruits': ['apple', 'peach', 'watermelon'],
        'number': 52
    }

    # becomes

    {
        'fish_facts-sharks': 'Most will drown if they stop moving',
        'fish_facts-skates': 'More than 200 species',
        'fruits-0': 'apple',
        'fruits-1': 'peach',
        'fruits-2': 'watermelon',
        'number': 52
    }

    # if intermediates is True then we also get unflattened elements
    # as well as the flattened ones.

    {
        'fish_facts': {
            'sharks': 'Most will drown if they stop moving',
            'skates': 'More than 200 species',
        },
        'fish_facts-sharks': 'Most will drown if they stop moving',
        'fish_facts-skates': 'More than 200 species',
        'fruits': ['apple', 'peach', 'watermelon'],
        'fruits-0': 'apple',
        'fruits-1': 'peach',
        'fruits-2': 'watermelon',
        'number': 52
    }