Skip to content

Instantly share code, notes, and snippets.

@kkew3
Created August 10, 2018 06:10
Show Gist options
  • Select an option

  • Save kkew3/afb5442c7cd8d8d7ae610852d74ac301 to your computer and use it in GitHub Desktop.

Select an option

Save kkew3/afb5442c7cd8d8d7ae610852d74ac301 to your computer and use it in GitHub Desktop.
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))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment