Created
August 10, 2018 06:10
-
-
Save kkew3/afb5442c7cd8d8d7ae610852d74ac301 to your computer and use it in GitHub Desktop.
Inspect complex recursive python data structure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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