Skip to content

Instantly share code, notes, and snippets.

@adamv
Last active November 17, 2015 18:10
Show Gist options
  • Save adamv/f3d7dcee1c9d6f881903 to your computer and use it in GitHub Desktop.
Save adamv/f3d7dcee1c9d6f881903 to your computer and use it in GitHub Desktop.
class Table(object):
def __init__(self):
self.rows = []
def add(self, row):
self.rows.append(row)
def render(self):
if not self.rows:
return
sizes = [0] * len(self.rows[0])
for row in self.rows:
for i in range(len(row)):
col_size = len(row[i])
if col_size > sizes[i]:
sizes[i] = col_size
fmt = ""
for i in range(len(sizes)):
if i > 0:
fmt += " "
fmt += "{0[" + str(i) + "]:" + str(sizes[i]) + "}"
for row in self.rows:
print(fmt.format(row))
@adamv
Copy link
Author

adamv commented Nov 17, 2015

Meh, just made render a standalone function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment