Skip to content

Instantly share code, notes, and snippets.

@taylor-jones
Last active January 19, 2020 17:57
Show Gist options
  • Save taylor-jones/8891f839b3105b8ff1e1562fb8cd72c1 to your computer and use it in GitHub Desktop.
Save taylor-jones/8891f839b3105b8ff1e1562fb8cd72c1 to your computer and use it in GitHub Desktop.
Displays all the attribute information about a Python object
#!/usr/bin/env python3
class Colors:
CYAN = '\033[96m'
MAGENTA = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
RESET = '\033[0m'
DIM = '\033[2m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def reveal(obj, **kwargs):
"""
Prints all the attributes and values of an object.
Optionally prints:
- the attribute types
- built-in attributes
- private attributes
Optionally specify a list of the only attributes to print the
values of. If empty, all attributes will be printed.
kwargs:
show_types {bool} (default: True)
show_builtins {bool} (default: False)
show_privates {bool} (default: False)
attrs {list|set} (default: list())
"""
show_types = kwargs.get('show_types', True)
show_builtins = kwargs.get('show_builtins', False)
show_privates = kwargs.get('show_privates', True)
show_only = set(kwargs.get('attrs', list()))
has_exclusions = len(show_only)
def is_builtin(attr):
return attr.startswith('__')
def is_private(attr):
return attr.startswith('_')
def key_color(attr):
# Determine the key color based on the key type
if is_builtin(attr):
return Colors.GREEN
elif is_private(attr):
return Colors.YELLOW
else:
return Colors.CYAN
# filter out undesirable content
for attr in dir(obj):
if not show_builtins and is_builtin(attr):
continue
elif not show_privates and is_private(attr):
continue
elif has_exclusions and attr not in show_only:
continue
# add color to the output
attr_key = f'{key_color(attr)}{attr}{Colors.RESET}'
# determine the printabler attribute value
try:
attr_val = getattr(obj, attr)
attr_type = f'{Colors.MAGENTA}{type(attr_val)}{Colors.RESET}' if show_types else ''
attr_equality = f'{Colors.DIM}={Colors.RESET}'
except Exception as e:
attr_val = f'{Colors.RED}Unable to print value{Colors.RESET}'
attr_type = ''
attr_equality = ''
# print the content
print(f'{attr_key} {attr_type} {attr_equality} {attr_val}')
#!/usr/bin/env python2
class Colors(object):
CYAN = u'\033[96m'
MAGENTA = u'\033[95m'
BLUE = u'\033[94m'
GREEN = u'\033[92m'
YELLOW = u'\033[93m'
RED = u'\033[91m'
RESET = u'\033[0m'
DIM = u'\033[2m'
BOLD = u'\033[1m'
UNDERLINE = u'\033[4m'
def reveal(obj, **kwargs):
"""
Prints all the attributes and values of an object.
Optionally prints:
- the attribute types
- built-in attributes
- private attributes
Optionally specify a list of the only attributes to print the
values of. If empty, all attributes will be printed.
kwargs:
show_types {bool} (default: True)
show_builtins {bool} (default: False)
show_privates {bool} (default: False)
attrs {list|set} (default: list())
"""
show_types = kwargs.get('show_types', True)
show_builtins = kwargs.get('show_builtins', False)
show_privates = kwargs.get('show_privates', True)
show_only = set(kwargs.get('attrs', list()))
has_exclusions = len(show_only)
def is_builtin(attr):
return attr.startswith('__')
def is_private(attr):
return attr.startswith('_')
def key_color(attr):
# Determine the key color based on the key type
if is_builtin(attr):
return Colors.GREEN
elif is_private(attr):
return Colors.YELLOW
else:
return Colors.CYAN
# filter out undesirable content
for attr in dir(obj):
if not show_builtins and is_builtin(attr):
continue
elif not show_privates and is_private(attr):
continue
elif has_exclusions and attr not in show_only:
continue
# add color to the output
attr_key = key_color(attr) + attr + Colors.RESET
# determine the printabler attribute value
try:
attr_val = getattr(obj, attr)
attr_type = Colors.MAGENTA + str(type(attr_val)) + Colors.RESET if show_types else ''
attr_equality = Colors.DIM + '=' + Colors.RESET
except Exception as e:
attr_val = Colors.RED + 'Unable to print value' + Colors.RESET
attr_type = ''
attr_equality = ''
# print the content
print str(attr_key) + ' ' + str(attr_type) + ' ' + str(attr_equality) + ' ' + str(attr_val)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment