Skip to content

docs: Improve documentation of Testplan.get_percentage#222

Open
rswarbrick wants to merge 1 commit into
lowRISC:masterfrom
rswarbrick:get-percentage-docs
Open

docs: Improve documentation of Testplan.get_percentage#222
rswarbrick wants to merge 1 commit into
lowRISC:masterfrom
rswarbrick:get-percentage-docs

Conversation

@rswarbrick
Copy link
Copy Markdown
Contributor

Also, add two error conditions (for fractions that don't make sense).

Also, add two error conditions (for fractions that don't make sense).

Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
@rswarbrick rswarbrick requested a review from AlexJones0 May 21, 2026 12:52
Comment thread src/dvsim/testplan.py
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants