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

Class ColumnPrinter

example_code/item_075.py:136–161  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

134
135print("Example 4")
136class ColumnPrinter:
137 def __init__(self):
138 self.columns = []
139
140 def append(self, data):
141 self.columns.append(data)
142
143 def __str__(self):
144 row_count = 1
145 for data in self.columns:
146 row_count = max(row_count, len(data.splitlines()) + 1)
147
148 rows = [""] * row_count
149 for j in range(row_count):
150 for i, data in enumerate(self.columns):
151 line = data.splitlines()[max(0, j - 1)]
152 if j == 0:
153 padding = " " * (len(line) // 2)
154 rows[j] += padding + str(i) + padding
155 else:
156 rows[j] += line
157
158 if (i + 1) < len(self.columns):
159 rows[j] += " | "
160
161 return "\n".join(rows)
162
163
164logging.getLogger().setLevel(logging.ERROR)

Callers 1

item_075.pyFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected