MCPcopy Create free account
hub / github.com/bslatkin/effectivepython / ColumnPrinter

Class ColumnPrinter

example_code/item_074.py:155–180  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

153
154print("Example 3")
155class ColumnPrinter:
156 def __init__(self):
157 self.columns = []
158
159 def append(self, data):
160 self.columns.append(data)
161
162 def __str__(self):
163 row_count = 1
164 for data in self.columns:
165 row_count = max(row_count, len(data.splitlines()) + 1)
166
167 rows = [""] * row_count
168 for j in range(row_count):
169 for i, data in enumerate(self.columns):
170 line = data.splitlines()[max(0, j - 1)]
171 if j == 0:
172 padding = " " * (len(line) // 2)
173 rows[j] += padding + str(i) + padding
174 else:
175 rows[j] += line
176
177 if (i + 1) < len(self.columns):
178 rows[j] += " | "
179
180 return "\n".join(rows)
181
182
183grid = LockingGrid(5, 9)

Callers 1

item_074.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected