Skip to content

Commit c81bb9a

Browse files
committed
python: consider output header w/ expandable width
Problem: The expandable field prefix ("+:") does not consider the width of the output header name when calculating output width. If all output entries are less than the size of the output header name, then output will not look as expected. Solution: When calculating maximum output width of a field, take into account the header entry name as long as no_header is not set. Update some tests in python/t0024-util.py for changes. Fixes #7030
1 parent 90ac8e6 commit c81bb9a

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/bindings/python/flux/util.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,12 +1004,18 @@ def sentinel_keys():
10041004

10051005
# Save the modified format, index, type, maximum width,
10061006
# observed width, and broken-down spec in lst:
1007+
initialmaxwidth = spec.width or 0
1008+
if sentinels[end] in ("maxwidth", "both"):
1009+
initialmaxwidth = max(
1010+
initialmaxwidth,
1011+
0 if no_header else len(self.headings.get(field, "")),
1012+
)
10071013
lst.append(
10081014
dict(
10091015
fmt=fmt,
10101016
index=index,
10111017
type=sentinels[end],
1012-
maxwidth=spec.width or 0,
1018+
maxwidth=initialmaxwidth,
10131019
width=0,
10141020
spec=spec,
10151021
)

t/python/t0024-util.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ def test_filter(self):
212212
).filter(items)
213213
self.assertEqual(fmt, "{i:>8} {f:8.2}")
214214

215+
# N.B. float has a width of 5 due to "FLOAT" header
215216
fmt = OutputFormat(
216217
"?+:{i:>7} ?:{s:>6} ?+:{f:.2f}", headings=self.headings
217218
).filter(items)
218-
self.assertEqual(fmt, "{i:>8} {f:3.2f}")
219+
self.assertEqual(fmt, "{i:>8} {f:5.2f}")
219220

220221
def test_sort(self):
221222
a = Item("a", 0, 2.2)
@@ -261,11 +262,13 @@ def test_issue6530(self):
261262
b = Item("abcdefghijklmnop", 2, 13)
262263
c = Item("c", 4, 5.0)
263264

265+
# N.B. iinteger has a width of 7 due to "INTEGER" header
266+
# N.B. float has a width of 5 due to "FLOAT" header
264267
items = [a, b, c]
265268
fmt = OutputFormat(
266269
"+:{s:5.5} +:{i:4d} +:{f:.2f}", headings=self.headings
267270
).filter(items)
268-
self.assertEqual(fmt, "{s:16.16} {i:4d} {f:3.2f}")
271+
self.assertEqual(fmt, "{s:16.16} {i:7d} {f:5.2f}")
269272

270273
def test_copy(self):
271274
original = "+:{s:5.5} {i:4d} {f:.2f}"

0 commit comments

Comments
 (0)