-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parse Quantity Strings #824
Conversation
Marenz
commented
Dec 19, 2023
- Fix missing number in Quantity formatting for small values
- Add function to allow parsing of Quantity strings
c53ad85
to
d23cd33
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM otherwise.
d23cd33
to
cebfd65
Compare
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
cebfd65
to
36b0205
Compare
Updated and ready for review |
CI failing with:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like the second commit could be split, other than that (and the CI failure) LGTM.
haha, I had that fixed, I just didn't add it to the commit XD |
1bae2f7
to
1d300b4
Compare
Please re-request a review from me when this is ready for another round. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For "Fix quantity formatting bug with -0/0" it might be good to describe what the bug actually was, is not completely clear to me by just reading the diff.
About the rounding issue when formatting, really tricky, I wonder if it wouldn't be simpler to do a string roundtrip in the formatting function itself, like convert to string, convert again to float, and only then check which exponent is more appropriate, then convert again to string. It might be less efficient (maybe?) but it seems easier to implement and likely more robust.
@pytest.mark.parametrize("quantity_type", [Power, Voltage, Current, Energy, Frequency]) | ||
@pytest.mark.parametrize("exponent", [0, 3, 6, 9]) | ||
@hypothesis.settings(max_examples=1000) | ||
@hypothesis.seed(42) # Seed that triggers a lot of problematic edge cases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you really want to leave the seed fixed? Isn't it more useful to let it change to potentially catch more random cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know for a fact that there are more cases and solving those requires an unreasonable amount of work (at this point).
@sahas-subramanian-frequenz mentioned an imperfect solution is better than no solution at all and at least here I would agree.
There will be cases were it prints slightly weird, maybe using a sub-optimal unit etc, but the printed result will never be straight out wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that sounds completely reasonable and I agree 100%. Can you add a comment summarizing this here? Because for the uninformed casual reader it will be really hard to figure all of this out, they'll probably just try to remove those two lines and be completely puzzled about why everything breaks now. I know future me will attempt that 😆
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be a pain in the ass about this, but I mean something more like "we are setting these fixed because there are still corner cases that will fail if we allow hypothesis to do random tests, this should be removed when all the corner cases are properly handled".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You did see the larger docstring of the function explaining basically that in other words?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, sorry, I expected it in the comments instead of the docstring so my brain stopped parsing after I couldn't find the clarification there.
Yes, I thought about this particular solution as well. It might be less elegant, but it would be pretty reliable and covering pretty much all possibilities. |
Basically it would only print the unit, no number (the strip removed the 0) |
Sure |
Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
1d300b4
to
964669f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One last comment about the comment. Also if you can add this "It would only print the unit, no number" to the "Fix quantity formatting bug with -0/0 " commit while you are it, I think it's good info to have in the commit message.
@pytest.mark.parametrize("quantity_type", [Power, Voltage, Current, Energy, Frequency]) | ||
@pytest.mark.parametrize("exponent", [0, 3, 6, 9]) | ||
@hypothesis.settings(max_examples=1000) | ||
@hypothesis.seed(42) # Seed that triggers a lot of problematic edge cases |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to be a pain in the ass about this, but I mean something more like "we are setting these fixed because there are still corner cases that will fail if we allow hypothesis to do random tests, this should be removed when all the corner cases are properly handled".
It would only print the unit, no number. Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
Certain numbers would be rendered wrongly due to rounding in the python formatter, e.g. Voltage.from_volts(999.9999850988388) would render "1000 V". This results in a faulty round-trip conversion (`num -> str -> num` no longer matching). Signed-off-by: Mathias L. Baumann <mathias.baumann@frequenz.com>
964669f
to
34961ec
Compare
Extended the commit description with the "no unit" explanation |
|