Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/dvsim/testplan.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,12 +288,19 @@ def _create_testplan_elements(kind: str, raw_dicts_list: list, tags: set) -> lis
return items

@staticmethod
def _get_percentage(value, total) -> str:
"""Returns a string representing percentage upto 2 decimal places."""
if total == 0:
return "-- %"
perc = value / total * 100 * 1.0
return f"{round(perc, 2):.2f} %"
def _get_percentage(value: int, total: int) -> str:
"""Format a fraction (value / total) as a float with up to 2 decimal places.

Both arguments should be non-negative and value should be at most equal
to total. If total is zero, this is reported as "-- %".

"""
if value > total:
raise ValueError("Cannot represent fraction over 100%")
if value < 0:
raise ValueError("Cannot represent negative fraction")

return "-- %" if total == 0 else f"{round(100 * value / total, 2):.2f} %"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure the round here is pointless - that's what the 2 in the format specifier is doing.
Python actually has a nifty format specifier for almost exactly this case:

Suggested change
return "-- %" if total == 0 else f"{round(100 * value / total, 2):.2f} %"
return "--%" if total == 0 else f"{value / total:.2%}"

But then you drop the space between the number and the % symbol. You could do:

Suggested change
return "-- %" if total == 0 else f"{round(100 * value / total, 2):.2f} %"
return "--%" if total == 0 else f"{value / total:.2%}".replace("%", " %")

Though that's about as much effort as the original.


@staticmethod
def get_dv_style_css() -> str:
Expand Down
Loading