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

formats, documentation and custom formats #1357

Closed
keewis opened this issue Jul 26, 2021 · 4 comments · Fixed by #1371 or #1375
Closed

formats, documentation and custom formats #1357

keewis opened this issue Jul 26, 2021 · 4 comments · Fixed by #1371 or #1375

Comments

@keewis
Copy link
Contributor

keewis commented Jul 26, 2021

I think there are several issues with unit formatting right now:

Related to xarray-contrib/pint-xarray#121: whenever I need to format units, rather than the documentation I tend to look through the source code of pint.formatting, especially _FORMATS is useful. This, I think, is because the documentation of the format values is hidden pretty well (the only mention I can find is in the tutorial which is incomplete, i.e. some of the formats and the quantity modifer "#" are never mentioned).

I also think the default format ("" in _FORMATS) should get a actual name: if I set UnitRegistry.default_format = "P", the "default format" as defined in _FORMATS is not accessible anymore until I set the registry's default back to "".

Finally, it would be great to have some kind of mechanism to register custom formats (we already can modify pint.formatting._FORMATS, but that's a implementation detail, right?). The syntax could be

@pint.register_unit_format("xyz")
def format_units_xyz(units, modifier):
    ...

where the register function should make sure it does not overwrite already existing formats.

Not sure if it would be better to open separate issues for each of these, but I though it might make sense to consider them together.

@hgrecco
Copy link
Owner

hgrecco commented Aug 11, 2021

I like this proposal. I was wondering for a time if there should provide some kind of templating engine but it way to broad, I think. So I then thought about allowing default_format to be a method but I like your idea better because it allows you combine with the existing formats. I only think that we might consider having a register_unit_format and register_quantity_format.

@keewis
Copy link
Contributor Author

keewis commented Aug 11, 2021

great, I'll look into implementing register_unit_format, then (register_quantity_format would be a later PR, although I'm not sure what I would use it for). Should we also allow adding dicts like the existing ones in _FORMATS, or do we want to require callables?

@hgrecco
Copy link
Owner

hgrecco commented Aug 12, 2021

Regarding register_quantity_format, I think it could be useful for those wanting to modify the magnitude and its location relative to units.

Right now those dicts are the kw of formatter. My guess is that "the right way" would be to reimplement the current formatters with your proposal (i.e. make any user defined format first class just as the pint ones).

So I think we could do something like this

@pint.register_unit_format("P")
def format_units_pretty(units, modifier):
    ### SOME CODE 
    return formatter(units.items(), 
         as_ratio=True,
        single_denominator=False,
        product_fmt= "·",
        division_fmt= "/",
        power_fmt= "{}{}",
        parentheses_fmt= "({})",
        exp_call= _pretty_fmt_exponent,
    )

(I am not puting here the modifier for simplicity)

This was referenced Aug 14, 2021
@cslominski
Copy link

A related issue/motivation is that if I have a with fractional units 2/3, and then ureg(str(a)) != a because of the default exp_call=lambda x: f"{x:n}".

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 a pull request may close this issue.

3 participants