Skip to content

Commit

Permalink
use builtin ljust instead of trying format on to format on to ...
Browse files Browse the repository at this point in the history
  • Loading branch information
wdpypere committed Nov 16, 2023
1 parent 0da014b commit 576211a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 17 deletions.
38 changes: 22 additions & 16 deletions lib/vsc/utils/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@
@author: Caroline De Brouwer (Ghent University)
"""

INDENT_4SPACES = ' ' * 4


class LengthNotEqualException(ValueError):
pass

Expand All @@ -48,28 +45,37 @@ def mk_rst_table(titles, columns):
if title_cnt != col_cnt:
msg = f"Number of titles/columns should be equal, found {int(title_cnt)} titles and {int(col_cnt)} columns"
raise LengthNotEqualException(msg)

table = []
tmpl = []
line = []
separator_blocks = []
title_items = []
column_widths = []

# figure out column widths
for i, title in enumerate(titles):
# figure out column widths
width = max(map(len, columns[i] + [title]))

# make line template
tmpl.append('{{{}:{{c}}<{}}}'.format(i, width))
column_widths.append(width)
separator_blocks.append(f"{'='*width}")
title_items.append(f'{title}'.ljust(width))

line = [''] * col_cnt
line_tmpl = INDENT_4SPACES.join(tmpl)
table_line = line_tmpl.format(*line, c='=')
separator_line = " ".join(separator_blocks)

table.append(table_line)
table.append(line_tmpl.format(*titles, c=' '))
table.append(table_line)
# header
table.extend([
separator_line,
" ".join(title_items),
separator_line
])

# rows
for row in map(list, zip(*columns)):
table.append(line_tmpl.format(*row, c=' '))
row_items = []
for i, item in enumerate(row):
row_items.append((item.ljust(column_widths[i])))
table.append(" ".join(row_items))

table.extend([table_line, ''])
# footer
table.extend([separator_line, ''])

return table
1 change: 0 additions & 1 deletion test/docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ def test_mk_rst_table(self):
'=' * len(t),
'',
]

self.assertEqual(table, check)

def suite():
Expand Down

0 comments on commit 576211a

Please sign in to comment.