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

Support __format__ #115

Open
perhyps opened this issue Jul 6, 2024 · 0 comments
Open

Support __format__ #115

perhyps opened this issue Jul 6, 2024 · 0 comments

Comments

@perhyps
Copy link

perhyps commented Jul 6, 2024

Hi, I think it would be very helpful to support the formatting of measures by adding the following definition to the MeasureBase class:

def __format__(self, format_spec):
    return '%s %s' % (
        format(getattr(self, self._default_unit), format_spec),
        self.unit
    )

This would allow the user to write something like this (e.g. in Python 3.6+):

>>> from measurement.measures import Distance
>>> x = Distance(cm=5/7)
>>> print(x)
0.7142857142857143 cm
>>> print(f"{x:.3f}")
0.714 cm
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