-
Notifications
You must be signed in to change notification settings - Fork 475
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
Behavior of custom formatter #2085
Comments
could you try your formatter on standard pint quuantitys or units, without importing pint-pandas? I think your issue may be related to setting the ureg for pint-pandas |
I have removed the Inputimport pint
from pint.delegates.formatter.plain import DefaultFormatter
ureg = pint.UnitRegistry()
ureg.formatter.default_format = "P#~"
class CustomFormatter(DefaultFormatter):
default_format = ""
def format_unit(self, unit, uspec = "", sort_func = None, **babel_kwds) -> str:
return super().format_unit(unit, uspec, sort_func, **babel_kwds)
acceleration = 1.0 * ureg.meter / ureg.second
print("Default formatter:", acceleration)
print("Default formatter:", acceleration.units)
ureg.formatter = CustomFormatter()
ureg.formatter._registry = ureg
ureg.formatter.default_format = "P#~"
print("\nCustom formatter:", acceleration)
print("Custom formatter:", acceleration.units) Output
|
It seems that the difference is coming from what is considered the "default" formatter. The initial formatter is Inputimport pint
from pint.delegates.formatter.plain import DefaultFormatter
ureg = pint.UnitRegistry()
ureg.formatter.default_format = "P#~"
acceleration = 1.0 * ureg.meter / ureg.second
print("Type:", type(ureg.formatter))
print("Default formatter:", acceleration)
print("Default formatter:", acceleration.units)
ureg.formatter = DefaultFormatter()
ureg.formatter._registry = ureg
ureg.formatter.default_format = "P#~"
print("\nType:", type(ureg.formatter))
print("Custom formatter:", acceleration)
print("Custom formatter:", acceleration.units) Output
|
It seems like |
So if you use |
I checked that. Deriving directly from Inputimport pint
from pint.delegates.formatter import Formatter
ureg = pint.UnitRegistry()
ureg.formatter.default_format = "P#~"
class CustomFormatter(Formatter):
default_format = ""
def format_unit(self, unit, uspec = "", sort_func = None, **babel_kwds) -> str:
return super().format_unit(unit, uspec, sort_func, **babel_kwds)
acceleration = 1.0 * ureg.meter / ureg.second
print("Default formatter:", acceleration)
print("Default formatter:", acceleration.units)
ureg.formatter = CustomFormatter()
ureg.formatter._registry = ureg
ureg.formatter.default_format = "P#~"
print("\nCustom formatter:", acceleration)
print("Custom formatter:", acceleration.units) Output
|
I've looked into this more. The behaviour of printing the units is different as import pint
from pint.delegates.formatter.plain import DefaultFormatter
ureg = pint.UnitRegistry()
ureg.formatter.default_format = "D#~"
class CustomFormatter(DefaultFormatter):
default_format = ""
def format_unit(self, unit, uspec = "", sort_func = None, **babel_kwds) -> str:
uspec = uspec or self.default_format
return super().format_unit(unit, uspec, sort_func, **babel_kwds)
acceleration = 1.0 * ureg.meter / ureg.second
print("Default formatter:", acceleration)
print("Default formatter:", acceleration.units)
ureg.formatter = CustomFormatter()
ureg.formatter._registry = ureg
ureg.formatter.default_format = "D#~"
print("\nCustom formatter:", acceleration)
print("Custom formatter:", acceleration.units)
I think it would be worth adding |
Description
I'm trying to understand how to write a custom formatter using pint 0.24. I am trying to format the units portion of a quantity. There is something about the behavior I do not understand. I wrote a test that passes every to the super class (
DefaultFormatter
) but the results are different than without the custom formatter. Am I missing something?Input
Output
Expected Behavior
Since the
CustomFormatter
just passes everything to the super class, I would expect the results to be the same as the default formatter. Am I misunderstanding how this is intended to work?The text was updated successfully, but these errors were encountered: