Skip to content

Instantly share code, notes, and snippets.

@alexlenail
Created August 20, 2024 20:59
Show Gist options
  • Save alexlenail/5dbfdc95ca0e177cf1b934ee3cad98fa to your computer and use it in GitHub Desktop.
Save alexlenail/5dbfdc95ca0e177cf1b934ee3cad98fa to your computer and use it in GitHub Desktop.
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