Created
April 22, 2018 19:00
-
-
Save zeekay/4c287f1120adb36d303ad803bc4b1e63 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
from tabulate import tabulate | |
from collections import Iterable | |
def attr(obj, a): | |
""" | |
Returns value of attribute on an object, calling attribute if necessary. | |
""" | |
v = getattr(obj, a, None) | |
if callable(v): | |
return v() | |
return v | |
def table(data, *attrs): | |
""" | |
Takes an object or list of objects and prints specified attributes in a | |
nicely formatted table. | |
""" | |
# Ensure data is iterable | |
if not isinstance(data, Iterable): | |
data = [data] | |
# Generate headers from attributes specified | |
headers = [a.upper().replace('_', ' ') for a in attrs] | |
# Generate tabular data from list of objects | |
table = [[attr(o, a) for a in attrs] for o in data] | |
# Pretty print table and headers | |
print(tabulate(table, headers=headers)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment