kkew3
8/10/2018 - 6:10 AM

Inspect complex recursive python data structure

Inspect complex recursive python data structure

from boltons.iterutils import remap  # pip install boltons
from pprint import pprint


def inspect_structure(obj):
    """
    >>> inspect_structure([1, 2])
    [<type 'int'>, <type 'int'>]
    >>> import numpy as np
    >>> inspect_structure([1, {3:'hola'}, np.eye(2)])
    [<type 'int'>, {3: <type 'str'>, <type 'numpy.ndarray'>]
    """
    pprint(remap(obj, visit=lambda p, k, v: True
                                            if isinstance(obj, (list, dict, tuple))
                                            else (key, type(value))))