Last active
November 17, 2015 18:10
-
-
Save adamv/f3d7dcee1c9d6f881903 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
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Meh, just made render a standalone function.