Created
August 20, 2024 20:59
-
-
Save alexlenail/5dbfdc95ca0e177cf1b934ee3cad98fa to your computer and use it in GitHub Desktop.
This file contains 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
import pprint | |
from termcolor import colored | |
def inspect_object(obj, _indent=0): | |
"""Recursively prints the attributes of an object with condensed and colored output.""" | |
indent = ' ' * _indent | |
if isinstance(obj, dict): | |
for key, value in obj.items(): | |
if isinstance(value, (dict, list)) or hasattr(value, '__dict__'): | |
print(f"{indent}{colored(key, 'green')}:") | |
recursive_print(value, _indent + 4) | |
else: | |
print(f"{indent}{colored(key, 'green')}: {colored(value, 'black')}") | |
elif isinstance(obj, list): | |
for i, item in enumerate(obj): | |
if isinstance(item, (dict, list)) or hasattr(item, '__dict__'): | |
print(f"{indent}{colored(f'[{i}]', 'green')}:") | |
recursive_print(item, _indent + 4) | |
else: | |
print(f"{indent}{colored(f'[{i}]', 'green')}: {colored(item, 'black')}") | |
elif hasattr(obj, '__dict__'): | |
recursive_print(vars(obj), _indent) | |
else: | |
print(f"{indent}{colored(obj, 'black')}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment