Skip to content

Commit 9b39f20

Browse files
committed
docs: Improve documentation of Testplan.get_percentage
Also, add two error conditions (for fractions that don't make sense). Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
1 parent 0a7cd7a commit 9b39f20

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

src/dvsim/testplan.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -288,12 +288,19 @@ def _create_testplan_elements(kind: str, raw_dicts_list: list, tags: set) -> lis
288288
return items
289289

290290
@staticmethod
291-
def _get_percentage(value, total) -> str:
292-
"""Returns a string representing percentage upto 2 decimal places."""
293-
if total == 0:
294-
return "-- %"
295-
perc = value / total * 100 * 1.0
296-
return f"{round(perc, 2):.2f} %"
291+
def _get_percentage(value: int, total: int) -> str:
292+
"""Format a fraction (value / total) as a float with up to 2 decimal places.
293+
294+
Both arguments should be non-negative and value should be at most equal
295+
to total. If total is zero, this is reported as "-- %".
296+
297+
"""
298+
if value > total:
299+
raise ValueError("Cannot represent fraction over 100%")
300+
if value < 0:
301+
raise ValueError("Cannot represent negative fraction")
302+
303+
return "-- %" if total == 0 else f"{round(100 * value / total, 2):.2f} %"
297304

298305
@staticmethod
299306
def get_dv_style_css() -> str:

0 commit comments

Comments
 (0)