From 36e8ba249ed7597669e11b6dde147a8472fe7f1c Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 9 Mar 2024 14:09:19 -0700 Subject: [PATCH 01/11] first tests --- tests/feature/test_val_unc_fsml.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/feature/test_val_unc_fsml.py b/tests/feature/test_val_unc_fsml.py index b824c80c..00431821 100644 --- a/tests/feature/test_val_unc_fsml.py +++ b/tests/feature/test_val_unc_fsml.py @@ -34,6 +34,11 @@ def test_fixed(self): ("!3f", "123.456 ± 0.789"), ("!4f", "123.4560 ± 0.7890"), ("!2f()", "123.46(79)"), + (".1f", "123.5 ± 0.8"), + (".2f", "123.46 ± 0.79"), + (".3f", "123.456 ± 0.789"), + (".4f", "123.4560 ± 0.7890"), + (".2f()", "123.46(79)"), ], ), ( @@ -65,6 +70,20 @@ def test_fixed(self): ("!5f()", "0.79(123.46)"), ("!6f()", "0.789(123.456)"), ("!7f()", "0.7890(123.4560)"), + (".1f", "0.8 ± 123.5"), + (".2f", "0.79 ± 123.46"), + (".3f", "0.789 ± 123.456"), + (".4f", "0.7890 ± 123.4560"), + (".5f", "0.78900 ± 123.45600"), + (".6f", "0.789000 ± 123.456000"), + (".7f", "0.7890000 ± 123.4560000"), + (".1f()", "0.8(123.5)"), + (".2f()", "0.79(123.46)"), + (".3f()", "0.789(123.456)"), + (".4f()", "0.7890(123.4560)"), + (".5f()", "0.78900(123.45600)"), + (".6f()", "0.789000(123.456000)"), + (".7f()", "0.7890000(123.4560000)"), ], ), ( From 1b760123799772076c843db5cc33c54a3cfca7bd Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 9 Mar 2024 14:09:30 -0700 Subject: [PATCH 02/11] no warn --- tests/feature/test_val_unc_formatter.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/feature/test_val_unc_formatter.py b/tests/feature/test_val_unc_formatter.py index 9e714295..5ed580b1 100644 --- a/tests/feature/test_val_unc_formatter.py +++ b/tests/feature/test_val_unc_formatter.py @@ -422,10 +422,6 @@ def test_pdg_sig_figs(self): self.run_val_unc_formatter_cases(cases_list) - def test_dec_place_warn(self): - formatter = Formatter(round_mode="dec_place") - self.assertWarns(Warning, formatter, 42, 24) - def test_left_pad_matching(self): formatter = Formatter(left_pad_matching=True) result = formatter(123, 0.123) From 2ff49599cb1bf71bd70d2a462ff554e73103d8c0 Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 9 Mar 2024 14:10:25 -0700 Subject: [PATCH 03/11] enable dec_place rounding --- src/sciform/format_utils/rounding.py | 5 +++-- src/sciform/formatting/number_formatting.py | 10 ++-------- tests/unit/format_utils/test_rounding_utils.py | 1 + 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/sciform/format_utils/rounding.py b/src/sciform/format_utils/rounding.py index 54f9c8f3..0895293a 100644 --- a/src/sciform/format_utils/rounding.py +++ b/src/sciform/format_utils/rounding.py @@ -84,6 +84,7 @@ def round_val_unc( val: Decimal, unc: Decimal, ndigits: int | type[AutoDigits], + round_mode: RoundModeEnum, *, use_pdg_sig_figs: bool = False, ) -> tuple[Decimal, Decimal, int]: @@ -91,7 +92,7 @@ def round_val_unc( if unc.is_finite() and unc != 0: round_digit = get_round_dec_place( unc, - RoundModeEnum.SIG_FIG, + round_mode, ndigits, pdg_sig_figs=use_pdg_sig_figs, ) @@ -103,7 +104,7 @@ def round_val_unc( elif val.is_finite(): round_digit = get_round_dec_place( val, - RoundModeEnum.SIG_FIG, + round_mode, ndigits, pdg_sig_figs=False, ) diff --git a/src/sciform/formatting/number_formatting.py b/src/sciform/formatting/number_formatting.py index 2b2d865c..5664319c 100644 --- a/src/sciform/formatting/number_formatting.py +++ b/src/sciform/formatting/number_formatting.py @@ -5,7 +5,6 @@ from dataclasses import replace from decimal import Decimal from typing import TYPE_CHECKING, cast -from warnings import warn from sciform.api.formatted_number import FormattedNumber from sciform.format_utils.exponents import get_exp_str, get_val_unc_exp @@ -190,13 +189,6 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str ) raise NotImplementedError(msg) - if options.round_mode is RoundModeEnum.DEC_PLACE: - msg = ( - "Precision round mode not available for value/uncertainty formatting. " - "Rounding is always applied as significant figures for the uncertainty." - ) - warn(msg, stacklevel=2) - unc = abs(unc) if exp_mode is ExpModeEnum.PERCENT: val *= 100 @@ -220,12 +212,14 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str val, unc, options.ndigits, + options.round_mode, use_pdg_sig_figs=options.pdg_sig_figs, ) val_rounded, unc_rounded, round_digit = round_val_unc( val_rounded, unc_rounded, options.ndigits, + options.round_mode, use_pdg_sig_figs=options.pdg_sig_figs, ) diff --git a/tests/unit/format_utils/test_rounding_utils.py b/tests/unit/format_utils/test_rounding_utils.py index 3902de5b..1838c779 100644 --- a/tests/unit/format_utils/test_rounding_utils.py +++ b/tests/unit/format_utils/test_rounding_utils.py @@ -308,6 +308,7 @@ def test_round_val_unc(self): val, unc, ndigits, + RoundModeEnum.SIG_FIG, use_pdg_sig_figs=use_pdg_sig_figs, ) with self.subTest( From 308ef7e922ae52a316cae86c855a6f5422795c3f Mon Sep 17 00:00:00 2001 From: Justin Date: Sat, 9 Mar 2024 14:39:21 -0700 Subject: [PATCH 04/11] more test cases with non-zero exp --- tests/feature/test_val_unc_fsml.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/feature/test_val_unc_fsml.py b/tests/feature/test_val_unc_fsml.py index 00431821..408bc100 100644 --- a/tests/feature/test_val_unc_fsml.py +++ b/tests/feature/test_val_unc_fsml.py @@ -215,6 +215,11 @@ def test_engineering_shifted(self): ("#!3r", "(0.123456 ± 0.000789)e+03"), ("#!4r", "(0.1234560 ± 0.0007890)e+03"), ("#!2r()", "0.12346(79)e+03"), + ("#.1r", "(0.1 ± 0.0)e+03"), + ("#.2r", "(0.12 ± 0.00)e+03"), + ("#.3r", "(0.123 ± 0.000)e+03"), + ("#.4r", "(0.1234 ± 0.0008)e+03"), + ("#.2r()", "0.12(00)e+03"), ], ), ( From df8fbf678f651721323353ea98447e7a2e109ff4 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 12 Mar 2024 21:39:40 -0600 Subject: [PATCH 05/11] Multiple updates --- src/sciform/format_utils/make_strings.py | 24 +++-- src/sciform/formatting/number_formatting.py | 94 +++++++++++++------- tests/feature/test_val_unc_fsml.py | 6 +- tests/unit/format_utils/test_make_strings.py | 36 ++++---- 4 files changed, 97 insertions(+), 63 deletions(-) diff --git a/src/sciform/format_utils/make_strings.py b/src/sciform/format_utils/make_strings.py index 7ef69ad0..ea2c0aec 100644 --- a/src/sciform/format_utils/make_strings.py +++ b/src/sciform/format_utils/make_strings.py @@ -3,7 +3,7 @@ from __future__ import annotations import decimal -from typing import TYPE_CHECKING +from decimal import Decimal from sciform.format_utils.numbers import ( get_top_dec_place, @@ -14,9 +14,6 @@ SignModeEnum, ) -if TYPE_CHECKING: # pragma: no cover - from decimal import Decimal - def get_sign_str(num: Decimal, sign_mode: SignModeEnum) -> str: """Get the format sign string.""" @@ -95,11 +92,19 @@ def construct_num_str( return f"{sign_str}{pad_str}{abs_num_str}" +def str_is_zero(unc_str: str) -> bool: + is_zero = True + for i in range(1, 10): + if str(i) in unc_str: + is_zero = False + return is_zero + + def construct_val_unc_str( # noqa: PLR0913 val_mantissa_str: str, unc_mantissa_str: str, - val_mantissa: Decimal, - unc_mantissa: Decimal, + val: Decimal, + unc: Decimal, decimal_separator: DecimalSeparatorEnums, *, paren_uncertainty: bool, @@ -115,9 +120,10 @@ def construct_val_unc_str( # noqa: PLR0913 else: if ( paren_uncertainty_trim - and unc_mantissa.is_finite() - and val_mantissa.is_finite() - and 0 < unc_mantissa < abs(val_mantissa) + and unc.is_finite() + and val.is_finite() + and unc < abs(val) + and not str_is_zero(unc_mantissa_str) ): """ Don't strip the unc_mantissa_str if val_mantissa is non-finite. diff --git a/src/sciform/formatting/number_formatting.py b/src/sciform/formatting/number_formatting.py index 5664319c..0adda907 100644 --- a/src/sciform/formatting/number_formatting.py +++ b/src/sciform/formatting/number_formatting.py @@ -24,6 +24,7 @@ from sciform.formatting.parser import parse_val_unc_from_input from sciform.options.conversion import finalize_populated_options, populate_options from sciform.options.option_types import ( + AutoDigits, AutoExpVal, ExpFormatEnum, ExpModeEnum, @@ -37,6 +38,49 @@ from sciform.options.input_options import InputOptions +def re_round_mantissa_exp_decomposition( + number: Number, + exp_mode: ExpModeEnum, + input_exp_val: int | type(AutoExpVal), + round_mode: RoundModeEnum, + ndigits: int | type(AutoDigits), +) -> tuple[Decimal, int, int, int]: + """Decompose a number into a mantissa and exponent using repeated rounding.""" + first_mantissa, first_exp_val, base = get_mantissa_exp_base( + number, + exp_mode, + input_exp_val, + ) + first_round_digit = get_round_dec_place(first_mantissa, round_mode, ndigits) + first_mantissa_rounded = round(first_mantissa, -first_round_digit) + + number_rounded = first_mantissa_rounded * base ** Decimal(first_exp_val) + + """ + Repeat mantissa + exponent discovery after rounding in case rounding + altered the required exponent. + """ + second_mantissa, exp_val, _ = get_mantissa_exp_base( + number_rounded, + exp_mode, + input_exp_val + ) + round_digit = get_round_dec_place(second_mantissa, round_mode, ndigits) + mantissa = round(second_mantissa, -round_digit) + mantissa = cast(Decimal, mantissa) + + if mantissa == 0: + """ + This catches an edge case involving negative ndigits when the + resulting mantissa is zero after the second rounding. This + result is technically correct (e.g. 0e+03 = 0e+00), but sciform + always presents zero values with an exponent of zero. + """ + exp_val = 0 + + return mantissa, exp_val, base, round_digit + + def format_from_options( value: Number, uncertainty: Number | None = None, @@ -116,38 +160,19 @@ def format_num(num: Decimal, options: FinalizedOptions) -> str: num *= 100 num = num.normalize() - exp_val = options.exp_val - round_mode = options.round_mode - exp_mode = options.exp_mode - ndigits = options.ndigits - mantissa, temp_exp_val, base = get_mantissa_exp_base(num, exp_mode, exp_val) - round_digit = get_round_dec_place(mantissa, round_mode, ndigits) - mantissa_rounded = round(mantissa, -round_digit) - - """ - Repeat mantissa + exponent discovery after rounding in case rounding - altered the required exponent. - """ - rounded_num = mantissa_rounded * Decimal(base) ** Decimal(temp_exp_val) - mantissa, exp_val, base = get_mantissa_exp_base(rounded_num, exp_mode, exp_val) - round_digit = get_round_dec_place(mantissa, round_mode, ndigits) - mantissa_rounded = round(mantissa, -int(round_digit)) - mantissa_rounded = cast(Decimal, mantissa_rounded) - - if mantissa_rounded == 0: - """ - This catches an edge case involving negative ndigits when the - resulting mantissa is zero after the second rounding. This - result is technically correct (e.g. 0e+03 = 0e+00), but sciform - always presents zero values with an exponent of zero. - """ - exp_val = 0 + mantissa, exp_val, base, round_dec_place = re_round_mantissa_exp_decomposition( + num, + options.exp_mode, + options.exp_val, + options.round_mode, + options.ndigits + ) left_pad_char = options.left_pad_char.value mantissa_str = construct_num_str( - mantissa_rounded.normalize(), + mantissa.normalize(), options.left_pad_dec_place, - round_digit, + round_dec_place, options.sign_mode, left_pad_char, ) @@ -165,7 +190,7 @@ def format_num(num: Decimal, options: FinalizedOptions) -> str: exp_str = get_exp_str( exp_val=exp_val, - exp_mode=exp_mode, + exp_mode=options.exp_mode, exp_format=options.exp_format, capitalize=options.capitalize, superscript=options.superscript, @@ -230,6 +255,11 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str options.exp_val, ) + if options.round_mode is RoundModeEnum.SIG_FIG: + ndigits = -round_digit + exp_val + else: + ndigits = options.ndigits + val_mantissa, _, _ = get_mantissa_exp_base( val_rounded, exp_mode=exp_mode, @@ -248,8 +278,6 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str left_pad_matching=options.left_pad_matching, ) - ndigits = -round_digit + exp_val - """ We will format the val and unc mantissas * using decimal place rounding mode with the ndigits calculated @@ -288,8 +316,8 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str val_unc_str = construct_val_unc_str( val_mantissa_str=val_mantissa_str, unc_mantissa_str=unc_mantissa_str, - val_mantissa=val_mantissa, - unc_mantissa=unc_mantissa, + val=val, + unc=unc, decimal_separator=options.decimal_separator, paren_uncertainty=options.paren_uncertainty, pm_whitespace=options.pm_whitespace, diff --git a/tests/feature/test_val_unc_fsml.py b/tests/feature/test_val_unc_fsml.py index 408bc100..2b137035 100644 --- a/tests/feature/test_val_unc_fsml.py +++ b/tests/feature/test_val_unc_fsml.py @@ -217,9 +217,9 @@ def test_engineering_shifted(self): ("#!2r()", "0.12346(79)e+03"), ("#.1r", "(0.1 ± 0.0)e+03"), ("#.2r", "(0.12 ± 0.00)e+03"), - ("#.3r", "(0.123 ± 0.000)e+03"), - ("#.4r", "(0.1234 ± 0.0008)e+03"), - ("#.2r()", "0.12(00)e+03"), + ("#.3r", "(0.123 ± 0.001)e+03"), + ("#.4r", "(0.1235 ± 0.0008)e+03"), + ("#.2r()", "0.12(0.00)e+03"), ], ), ( diff --git a/tests/unit/format_utils/test_make_strings.py b/tests/unit/format_utils/test_make_strings.py index 1c28edf0..8b26ad3d 100644 --- a/tests/unit/format_utils/test_make_strings.py +++ b/tests/unit/format_utils/test_make_strings.py @@ -143,8 +143,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123.456", "unc_mantissa_str": "0.123", - "val_mantissa": Decimal("123.456"), - "unc_mantissa": Decimal("0.123"), + "val": Decimal("123.456"), + "unc": Decimal("0.123"), "decimal_separator": ".", "paren_uncertainty": False, "pm_whitespace": True, @@ -156,8 +156,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123.456", "unc_mantissa_str": "0.123", - "val_mantissa": Decimal("123.456"), - "unc_mantissa": Decimal("0.123"), + "val": Decimal("123.456"), + "unc": Decimal("0.123"), "decimal_separator": ".", "paren_uncertainty": False, "pm_whitespace": False, @@ -169,8 +169,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456", "unc_mantissa_str": "0,123", - "val_mantissa": Decimal("123.456"), - "unc_mantissa": Decimal("0.123"), + "val": Decimal("123.456"), + "unc": Decimal("0.123"), "decimal_separator": ",", "paren_uncertainty": False, "pm_whitespace": True, @@ -182,8 +182,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123.456", "unc_mantissa_str": "0.123", - "val_mantissa": Decimal("123.456"), - "unc_mantissa": Decimal("0.123"), + "val": Decimal("123.456"), + "unc": Decimal("0.123"), "decimal_separator": ".", "paren_uncertainty": False, "pm_whitespace": True, @@ -195,8 +195,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456", "unc_mantissa_str": "0,123", - "val_mantissa": Decimal("123.456"), - "unc_mantissa": Decimal("0.123"), + "val": Decimal("123.456"), + "unc": Decimal("0.123"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -208,8 +208,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456", "unc_mantissa_str": "0,123", - "val_mantissa": Decimal("123.456"), - "unc_mantissa": Decimal("0.123"), + "val": Decimal("123.456"), + "unc": Decimal("0.123"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -221,8 +221,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456,789.123_456", "unc_mantissa_str": "0.123_456", - "val_mantissa": Decimal("123456789.123456"), - "unc_mantissa": Decimal("0.123456"), + "val": Decimal("123456789.123456"), + "unc": Decimal("0.123456"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -234,8 +234,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456,789.123_456", "unc_mantissa_str": "0.123_456", - "val_mantissa": Decimal("123456789.123456"), - "unc_mantissa": Decimal("0.123456"), + "val": Decimal("123456789.123456"), + "unc": Decimal("0.123456"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -247,8 +247,8 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "0.123", "unc_mantissa_str": "123,456.456", - "val_mantissa": Decimal("0.123"), - "unc_mantissa": Decimal("123456.456"), + "val": Decimal("0.123"), + "unc": Decimal("123456.456"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, From 91bd30688054fa7c8e59ca5a8be64885408ca23d Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 12 Mar 2024 21:41:09 -0600 Subject: [PATCH 06/11] ruff --- src/sciform/format_utils/make_strings.py | 1 + src/sciform/formatting/number_formatting.py | 10 ++-------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/src/sciform/format_utils/make_strings.py b/src/sciform/format_utils/make_strings.py index ea2c0aec..ebc48f3f 100644 --- a/src/sciform/format_utils/make_strings.py +++ b/src/sciform/format_utils/make_strings.py @@ -93,6 +93,7 @@ def construct_num_str( def str_is_zero(unc_str: str) -> bool: + """Check if a string contains no non-zero integers.""" is_zero = True for i in range(1, 10): if str(i) in unc_str: diff --git a/src/sciform/formatting/number_formatting.py b/src/sciform/formatting/number_formatting.py index 0adda907..233b7d71 100644 --- a/src/sciform/formatting/number_formatting.py +++ b/src/sciform/formatting/number_formatting.py @@ -61,9 +61,7 @@ def re_round_mantissa_exp_decomposition( altered the required exponent. """ second_mantissa, exp_val, _ = get_mantissa_exp_base( - number_rounded, - exp_mode, - input_exp_val + number_rounded, exp_mode, input_exp_val ) round_digit = get_round_dec_place(second_mantissa, round_mode, ndigits) mantissa = round(second_mantissa, -round_digit) @@ -161,11 +159,7 @@ def format_num(num: Decimal, options: FinalizedOptions) -> str: num = num.normalize() mantissa, exp_val, base, round_dec_place = re_round_mantissa_exp_decomposition( - num, - options.exp_mode, - options.exp_val, - options.round_mode, - options.ndigits + num, options.exp_mode, options.exp_val, options.round_mode, options.ndigits ) left_pad_char = options.left_pad_char.value From be8020f17542438bf71fe7463045f6f87a10b19e Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 12 Mar 2024 22:41:10 -0600 Subject: [PATCH 07/11] try to simplify uncertainty trimming logic --- src/sciform/format_utils/make_strings.py | 19 +------------------ src/sciform/formatting/number_formatting.py | 13 ++++++++++--- tests/unit/format_utils/test_make_strings.py | 20 +------------------- 3 files changed, 12 insertions(+), 40 deletions(-) diff --git a/src/sciform/format_utils/make_strings.py b/src/sciform/format_utils/make_strings.py index ebc48f3f..d0f0df36 100644 --- a/src/sciform/format_utils/make_strings.py +++ b/src/sciform/format_utils/make_strings.py @@ -92,20 +92,9 @@ def construct_num_str( return f"{sign_str}{pad_str}{abs_num_str}" -def str_is_zero(unc_str: str) -> bool: - """Check if a string contains no non-zero integers.""" - is_zero = True - for i in range(1, 10): - if str(i) in unc_str: - is_zero = False - return is_zero - - def construct_val_unc_str( # noqa: PLR0913 val_mantissa_str: str, unc_mantissa_str: str, - val: Decimal, - unc: Decimal, decimal_separator: DecimalSeparatorEnums, *, paren_uncertainty: bool, @@ -119,13 +108,7 @@ def construct_val_unc_str( # noqa: PLR0913 pm_symb = f" {pm_symb} " val_unc_str = f"{val_mantissa_str}{pm_symb}{unc_mantissa_str}" else: - if ( - paren_uncertainty_trim - and unc.is_finite() - and val.is_finite() - and unc < abs(val) - and not str_is_zero(unc_mantissa_str) - ): + if paren_uncertainty_trim: """ Don't strip the unc_mantissa_str if val_mantissa is non-finite. Don't strip the unc_mantissa_str if unc_mantissa == 0 (because then the diff --git a/src/sciform/formatting/number_formatting.py b/src/sciform/formatting/number_formatting.py index 233b7d71..82cfbf59 100644 --- a/src/sciform/formatting/number_formatting.py +++ b/src/sciform/formatting/number_formatting.py @@ -2,6 +2,7 @@ from __future__ import annotations +import re from dataclasses import replace from decimal import Decimal from typing import TYPE_CHECKING, cast @@ -307,15 +308,21 @@ def format_val_unc(val: Decimal, unc: Decimal, options: FinalizedOptions) -> str val_mantissa_str = parse_mantissa_from_ascii_exp_str(val_mantissa_exp_str) unc_mantissa_str = parse_mantissa_from_ascii_exp_str(unc_mantissa_exp_str) + paren_uncertainty_trim = ( + options.paren_uncertainty_trim + and val.is_finite() + and unc.is_finite() + and unc < abs(val) + and re.search(r"[1-9]", unc_mantissa_str) is not None + ) + val_unc_str = construct_val_unc_str( val_mantissa_str=val_mantissa_str, unc_mantissa_str=unc_mantissa_str, - val=val, - unc=unc, decimal_separator=options.decimal_separator, paren_uncertainty=options.paren_uncertainty, pm_whitespace=options.pm_whitespace, - paren_uncertainty_trim=options.paren_uncertainty_trim, + paren_uncertainty_trim=paren_uncertainty_trim, ) if val.is_finite() or unc.is_finite() or options.nan_inf_exp: diff --git a/tests/unit/format_utils/test_make_strings.py b/tests/unit/format_utils/test_make_strings.py index 8b26ad3d..7c1c152d 100644 --- a/tests/unit/format_utils/test_make_strings.py +++ b/tests/unit/format_utils/test_make_strings.py @@ -143,8 +143,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123.456", "unc_mantissa_str": "0.123", - "val": Decimal("123.456"), - "unc": Decimal("0.123"), "decimal_separator": ".", "paren_uncertainty": False, "pm_whitespace": True, @@ -156,8 +154,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123.456", "unc_mantissa_str": "0.123", - "val": Decimal("123.456"), - "unc": Decimal("0.123"), "decimal_separator": ".", "paren_uncertainty": False, "pm_whitespace": False, @@ -169,8 +165,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456", "unc_mantissa_str": "0,123", - "val": Decimal("123.456"), - "unc": Decimal("0.123"), "decimal_separator": ",", "paren_uncertainty": False, "pm_whitespace": True, @@ -182,8 +176,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123.456", "unc_mantissa_str": "0.123", - "val": Decimal("123.456"), - "unc": Decimal("0.123"), "decimal_separator": ".", "paren_uncertainty": False, "pm_whitespace": True, @@ -195,8 +187,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456", "unc_mantissa_str": "0,123", - "val": Decimal("123.456"), - "unc": Decimal("0.123"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -208,8 +198,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456", "unc_mantissa_str": "0,123", - "val": Decimal("123.456"), - "unc": Decimal("0.123"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -221,8 +209,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456,789.123_456", "unc_mantissa_str": "0.123_456", - "val": Decimal("123456789.123456"), - "unc": Decimal("0.123456"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -234,8 +220,6 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "123,456,789.123_456", "unc_mantissa_str": "0.123_456", - "val": Decimal("123456789.123456"), - "unc": Decimal("0.123456"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, @@ -247,12 +231,10 @@ def test_construct_val_unc_str(self): { "val_mantissa_str": "0.123", "unc_mantissa_str": "123,456.456", - "val": Decimal("0.123"), - "unc": Decimal("123456.456"), "decimal_separator": ",", "paren_uncertainty": True, "pm_whitespace": True, - "paren_uncertainty_trim": True, + "paren_uncertainty_trim": False, }, "0.123(123,456.456)", ), From 7a22cac62bdabe65e72f707e74698290ed18d8a6 Mon Sep 17 00:00:00 2001 From: Justin Date: Tue, 28 May 2024 07:00:57 -0600 Subject: [PATCH 08/11] comment --- src/sciform/format_utils/make_strings.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sciform/format_utils/make_strings.py b/src/sciform/format_utils/make_strings.py index d0f0df36..2bf91868 100644 --- a/src/sciform/format_utils/make_strings.py +++ b/src/sciform/format_utils/make_strings.py @@ -110,10 +110,10 @@ def construct_val_unc_str( # noqa: PLR0913 else: if paren_uncertainty_trim: """ - Don't strip the unc_mantissa_str if val_mantissa is non-finite. - Don't strip the unc_mantissa_str if unc_mantissa == 0 (because then the - empty string would remain). - Don't left strip the unc_mantissa_str if unc_mantissa >= val_mantissa + Strip out all leading zeros and separators except the + decimal separator if it has non-zero numbers to the left. + (000_000.004 567) -> (4567) + (0_123.456 789) -> (123.456) """ for separator in SeparatorEnum: if separator != decimal_separator: From 67e016f0dcfe64cc107c8ef644abf591f2e76fc6 Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 16 Jun 2024 14:54:51 -0600 Subject: [PATCH 09/11] import --- src/sciform/format_utils/make_strings.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/sciform/format_utils/make_strings.py b/src/sciform/format_utils/make_strings.py index 2bf91868..55792575 100644 --- a/src/sciform/format_utils/make_strings.py +++ b/src/sciform/format_utils/make_strings.py @@ -3,7 +3,7 @@ from __future__ import annotations import decimal -from decimal import Decimal +from typing import TYPE_CHECKING from sciform.format_utils.numbers import ( get_top_dec_place, @@ -14,6 +14,9 @@ SignModeEnum, ) +if TYPE_CHECKING: + from decimal import Decimal + def get_sign_str(num: Decimal, sign_mode: SignModeEnum) -> str: """Get the format sign string.""" From 66d06ec3fd454cf8c4c74372d3e00a02cd05aeff Mon Sep 17 00:00:00 2001 From: Justin Date: Sun, 16 Jun 2024 14:56:05 -0600 Subject: [PATCH 10/11] nocover --- src/sciform/format_utils/make_strings.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sciform/format_utils/make_strings.py b/src/sciform/format_utils/make_strings.py index 55792575..eeb1e782 100644 --- a/src/sciform/format_utils/make_strings.py +++ b/src/sciform/format_utils/make_strings.py @@ -14,7 +14,7 @@ SignModeEnum, ) -if TYPE_CHECKING: +if TYPE_CHECKING: # pragma: no cover from decimal import Decimal From 2f1b6dc6de34f9347e08ff107ebabe40557b5302 Mon Sep 17 00:00:00 2001 From: Justin Gerber Date: Tue, 6 Aug 2024 23:22:25 -0600 Subject: [PATCH 11/11] some more tests --- tests/feature/test_val_unc_formatter.py | 16 ++++++++++++++++ tests/feature/test_val_unc_fsml.py | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/tests/feature/test_val_unc_formatter.py b/tests/feature/test_val_unc_formatter.py index ef1b18b2..3be210a4 100644 --- a/tests/feature/test_val_unc_formatter.py +++ b/tests/feature/test_val_unc_formatter.py @@ -95,6 +95,14 @@ def test_paren_unc_invalid_unc(self): [ (Formatter(paren_uncertainty=True), "0(0)"), (Formatter(paren_uncertainty=True, ndigits=3), "0(0)"), + ( + Formatter( + paren_uncertainty=True, + round_mode="dec_place", + ndigits=3, + ), + "0.000(0.000)", + ), ], ), ( @@ -102,6 +110,14 @@ def test_paren_unc_invalid_unc(self): [ (Formatter(paren_uncertainty=True), "0(inf)"), (Formatter(paren_uncertainty=True, ndigits=3), "0(inf)"), + ( + Formatter( + paren_uncertainty=True, + round_mode="dec_place", + ndigits=3, + ), + "0.000(inf)", + ), ], ), ] diff --git a/tests/feature/test_val_unc_fsml.py b/tests/feature/test_val_unc_fsml.py index 2b137035..fbcb7858 100644 --- a/tests/feature/test_val_unc_fsml.py +++ b/tests/feature/test_val_unc_fsml.py @@ -34,6 +34,10 @@ def test_fixed(self): ("!3f", "123.456 ± 0.789"), ("!4f", "123.4560 ± 0.7890"), ("!2f()", "123.46(79)"), + (".-3f", "0 ± 0"), + (".-2f", "100 ± 0"), + (".-1f", "120 ± 0"), + (".0f", "123 ± 1"), (".1f", "123.5 ± 0.8"), (".2f", "123.46 ± 0.79"), (".3f", "123.456 ± 0.789"), @@ -77,6 +81,10 @@ def test_fixed(self): (".5f", "0.78900 ± 123.45600"), (".6f", "0.789000 ± 123.456000"), (".7f", "0.7890000 ± 123.4560000"), + (".-3f()", "0(0)"), + (".-2f()", "0(100)"), + (".-1f()", "0(120)"), + (".0f()", "1(123)"), (".1f()", "0.8(123.5)"), (".2f()", "0.79(123.46)"), (".3f()", "0.789(123.456)"),