Skip to content

Instantly share code, notes, and snippets.

@chadcatlett
Last active Dec 18, 2015
Embed
What would you like to do?
#!/usr/bin/env python
get_lenlist = lambda input_data: dict([(key,max([len(str(key))]+[len(str(i[key])) for i in input_data])) for key in input_data[0].keys()])
get_rowformat = lambda length_data: " | ".join('%%-%ss' % max_length for key, max_length in length_data.iteritems())
get_divider = lambda length_data: "-|-".join("-"*max_length for key, max_length in length_data.iteritems())
get_header = lambda length_data: get_rowformat(length_data) % tuple(length_data.keys())
records = [
{ 'name':'Billy Bob', 'Hobby': 'Guns'},
{ 'name':'Asshat','Hobby':'Being an ass'},
{ 'name':'Dick Head','Hobby':'Being a dick'}
]
length_list = get_lenlist(records)
print get_header(length_list)
print get_divider(length_list)
for record in records:
print get_rowformat(length_list) % tuple(record[key] for key, max_length in length_list.iteritems())
print get_divider(length_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment