Last active
October 12, 2023 07:13
-
-
Save cmower/6b280456be76788143a4af8b6bc9f472 to your computer and use it in GitHub Desktop.
Print layout of a python dict/list, i.e. only keys/indexes and the types of the values.
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
def print_type_layout(x, indent=0, incr=2): | |
if isinstance(x, dict): | |
print(" " * indent, "{") | |
for k, v in x.items(): | |
print(" " * indent, f"'{k}':", end="") | |
if isinstance(v, (dict, list)): | |
print("") | |
print_type_layout(v, indent=indent + incr) | |
else: | |
print(f" {type(v)}") | |
print(" " * indent, "}") | |
elif isinstance(x, list): | |
print(" " * indent, "[") | |
for i, v in enumerate(x): | |
print(" " * indent, f"{i}:", end="") | |
if isinstance(v, (dict, list)): | |
print("") | |
print_type_layout(v, indent=indent + incr) | |
else: | |
print(f" {type(v)}") | |
print(" " * indent, "]") | |
else: | |
print(type(x)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment