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

The behavior/semantics of extra prefixes may be confusing. #97

Closed
jagerber48 opened this issue Dec 29, 2023 · 2 comments · Fixed by #113
Closed

The behavior/semantics of extra prefixes may be confusing. #97

jagerber48 opened this issue Dec 29, 2023 · 2 comments · Fixed by #113

Comments

@jagerber48
Copy link
Owner

Consider

from sciform import Formatter, GlobalDefaultsContext

with GlobalDefaultsContext(add_c_prefix=True):
    sform = Formatter(
        exp_mode="scientific",
        exp_val=2,
        extra_si_prefixes={1: 'da'},
        exp_format='prefix',
    )
    print(sform(123))
# 1.23e+02

sform = Formatter(
    exp_mode="scientific",
    exp_val=2,
    extra_si_prefixes={1: 'da'},
    exp_format='prefix',
)

with GlobalDefaultsContext(add_c_prefix=True):
    print(sform(123))
# 1.23e+02

In both cases the add_c_prefix does not have the effect of configuring the formatter to use the c prefix. This is because the formatter includes input for extra_si_prefixes. The current behavior is that, like all other options, the global extra_si_prefixes is used only if the local extra_si_prefixes=None.

An alternative, possibly expected, behavior would be that the local extra_si_prefixes are always merged into the global extra_si_prefixes at format time.

@jagerber48
Copy link
Owner Author

When this functionality is pinned it needs testing.

@jagerber48
Copy link
Owner Author

These semantics are not exceptional, they are the norm within sciform:

from sciform import Formatter, GlobalDefaultsContext

sform = Formatter(
    exp_mode="fixed_point",
)

with GlobalDefaultsContext(exp_mode="scientific"):
    print(sform(123456))

I make the following proposals:

  • Confirm that the extra prefixes/parts-per-forms options behave like other options. That is, at format time, any totally unpopulated options are populated with options from the global options.
  • Make sure there are sufficient tests asserting the above behavior
  • Make sure the documentation and examples are clear on this behavior
  • Remove the bare global helper functions like global_add_c_prefix(). These options are redundant with set_global_defaults. Note this is slightly off-topic wrt the original topic of this issue but it contributes to some of the complexity in the code and adds unnecessary mental load to figuring out how these options work. This is the part of the modification that will be breaking.

The upshot is that the extra_si_prefixes, extra_iec_prefixes and extra_parts_per_forms options should be handled exactly like any other options with the small addendum that the add_c_prefix, add_small_si_prefixes and add_ppth_form helper flags exist to populate these dictionary with some special values during __post_init__.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant