Als ersten Schritt, um die Aufgabe zu lösen, probiere ich, die unterschiedlichen Spaltenbreiten und Anzahl der Spalten zu ermitteln, wobei die Spaltenzahl sich ja ganz einfach aus der Anzahl der Breitenangaben ergibt. Ein bißchen Code zum Ermitteln dieser Breitenangaben habe ich schon:
Code: Alles auswählen
def get_column_widths(items, spacing=2, max_line_width=80):
column_widths = []
filled_width = 0
column_index = 0
for item in items:
item_width = len(item)
if item_width > max_line_width:
# Item would fill more than the whole line length
raise ValueError('Item width too long for line')
if column_index == len(column_widths):
# Current index after last column
if (filled_width + item_width) <= max_line_width:
# New column for item will fit in line
# => Add new column
# XXX: Setting zero-length is needed to see
# the offset in following code (ugly!)
column_widths.append(0)
else:
# New column would be too large for line
# => Fallback to first column
column_index = 0
offset = item_width - column_widths[column_index]
if offset > 0:
# Item wider than column => Enlarge column
column_widths[column_index] += offset
# Enlarge total length with respect to spacing
filled_width += offset + spacing
# Increase index used for next iteration
column_index += 1
return column_widths
Code: Alles auswählen
>>> import columnizer
>>> columnizer.get_column_widths([76 * 'x', 2 * 'x', 78 * 'x'])
[78, 2]
Am Code muss aber noch viel gemacht werden. So klappt folgendes noch nicht wie gewünscht:
Code: Alles auswählen
>>> columnizer.get_column_widths([50 * 'x', 25 * 'x', 60 * 'x'])
[60, 25]
Und abgesehen davon macht die Funktion natürlich nicht das, was man von `ls` erwarten würde, da sie nämlich nach dem Zeilenumbruch das 3. Element unter das 1. Element gesetzt hat (60 in die 50er-Spalte). Eigentlich müsste da erstmal die 25er-Länge drunter rutschen. Wie gesagt: Alles noch im Anfangsstadium.