Skip to content

docs: Improve documentation of Testplan.get_percentage#222

Merged
rswarbrick merged 1 commit into
lowRISC:masterfrom
rswarbrick:get-percentage-docs
May 26, 2026
Merged

docs: Improve documentation of Testplan.get_percentage#222
rswarbrick merged 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).

@rswarbrick rswarbrick requested a review from AlexJones0 May 21, 2026 12:52
Comment thread src/dvsim/testplan.py Outdated
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good point about the rounding! Let's keep the multiplication by 100 (for now, at least). It's possibly more explicit anyway: I'm a bit ambivalent about using format specifiers that change the number.

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

Signed-off-by: Rupert Swarbrick <rswarbrick@lowrisc.org>
@rswarbrick rswarbrick force-pushed the get-percentage-docs branch from 9b39f20 to 28ebe03 Compare May 26, 2026 09:20
@rswarbrick rswarbrick added this pull request to the merge queue May 26, 2026
Merged via the queue into lowRISC:master with commit d7e7814 May 26, 2026
6 checks passed
@rswarbrick rswarbrick deleted the get-percentage-docs branch May 26, 2026 09:25
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