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

Class ColumnPrinter

example_code/item_071.py:198–223  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

196
197print("Example 8")
198class ColumnPrinter:
199 def __init__(self):
200 self.columns = []
201
202 def append(self, data):
203 self.columns.append(data)
204
205 def __str__(self):
206 row_count = 1
207 for data in self.columns:
208 row_count = max(row_count, len(data.splitlines()) + 1)
209
210 rows = [""] * row_count
211 for j in range(row_count):
212 for i, data in enumerate(self.columns):
213 line = data.splitlines()[max(0, j - 1)]
214 if j == 0:
215 padding = " " * (len(line) // 2)
216 rows[j] += padding + str(i) + padding
217 else:
218 rows[j] += line
219
220 if (i + 1) < len(self.columns):
221 rows[j] += " | "
222
223 return "\n".join(rows)
224
225
226columns = ColumnPrinter()

Callers 1

item_071.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected