Skip to content
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

Inconsistent behaviour of Measurement string representation methods with default_format #1363

Closed
mgab opened this issue Aug 4, 2021 · 1 comment

Comments

@mgab
Copy link
Contributor

mgab commented Aug 4, 2021

Instances of theMeasurement class have 4 methods to produce a formatted string: __str__, __repr__, _repr_html_ and _repr_latex_. As far as I understand, all of them should follow the format specifications in ureg.default_formatting, or in measurement.default_formatting in case they differ, in a consistent manner.

However, __str__ and __repr__ ignore the value of measurement.default_format and instead follow what ureg.default_format specifies. On the other hand _repr_html_ and _repr_latex_ follow what it is specified in measurement.default_format (which might or might not be the same than what ureg.default_format specifies).

For Quantity instances the behaviour of the four formatting methods is not consistent, either, but in a different way (#1299).

import pint
ureg = pint.UnitRegistry()

def test_reprs(obj):
    reprs = ['__str__', '__repr__', '_repr_html_', '_repr_latex_']
    for repr_ in reprs:
        print(f"{repr_}: '{getattr(obj, repr_)()}'")

ureg.default_format = ''
m = ureg.Measurement(1, 0.1, 'meter')
ureg.default_format = '~P'
m.default_format = ''
test_reprs(m)
# __str__: '(1.00 +/- 0.10) m'
# __repr__: '<Measurement(1.0, 0.1, m)>'
# _repr_html_: '(1.00 &plusmn; 0.10) meter'
# _repr_latex_: '$\left(1.00 \pm 0.10\right)\ \mathrm{meter}$'

ureg.default_format = ''
m = ureg.Measurement(1, 0.1, 'meter')
ureg.default_format = ''
m.default_format = '~P'
test_reprs(m)
# __str__: '(1.00 +/- 0.10) meter'
# __repr__: '<Measurement(1.0, 0.1, meter)>'
# _repr_html_: '(1.00 &plusmn; 0.10) m'
# _repr_latex_: '$\left(1.00 \pm 0.10\right)\ \mathrm{m}$'
@mgab
Copy link
Contributor Author

mgab commented Oct 4, 2024

Closed as it is no longer relevant.

Current version 0.24.3 measurements do not care about Measurement.default_format directly on the instance, nor the documentation mentions it, so the inconsistency is gone.

Thanks for the great library!

@mgab mgab closed this as completed Oct 4, 2024
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

No branches or pull requests

1 participant