From ea0602fbb202280c3970009ff0df779617f9785f Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Tue, 1 Oct 2024 19:11:27 -0400 Subject: [PATCH 1/2] fix: evaluate integer percentage (#114) --- calc/calc.py | 19 ++++++++++--------- tests/test_calc.py | 6 +++++- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/calc/calc.py b/calc/calc.py index 1fea477..bc5d01c 100644 --- a/calc/calc.py +++ b/calc/calc.py @@ -79,7 +79,7 @@ } SUFFIXES = { - '%': 0.01, + '%': lambda x: x * 0.01, } @@ -112,24 +112,25 @@ def lower_dict(input_dict): def super_float(text): """ - Like float, but with SI extensions. 1k goes to 1000. + Evaluate suffixes if applicable and apply float. """ - if text[-1] in SUFFIXES: - return float(text[:-1]) * SUFFIXES[text[-1]] - else: - return float(text) + func = SUFFIXES[text[-1]] + return func(float(text[:-1])) def eval_number(parse_result): """ - Create a float out of its string parts. + Returns an int or a float from string parts. + Calls super_float above for suffix evaluation. e.g. [ '7.13', 'e', '3' ] -> 7130 - Calls super_float above. """ + if parse_result[-1] in SUFFIXES: + return super_float("".join(parse_result)) + for item in parse_result: if "." in item or "e" in item or "E" in item: - return super_float("".join(parse_result)) + return float("".join(parse_result)) return int("".join(parse_result)) diff --git a/tests/test_calc.py b/tests/test_calc.py index b43d423..2efccc0 100644 --- a/tests/test_calc.py +++ b/tests/test_calc.py @@ -93,7 +93,11 @@ def test_si_suffix(self): For instance '%' stand for 1/100th so '1%' should be 0.01 """ test_mapping = [ - ('4.2%', 0.042) + ('4.2%', 0.042), + ('1%', 0.01), + ('0.5%', 0.005), + ('70%', 0.7), + ('-50%', -0.5) ] for (expr, answer) in test_mapping: From 9a69f6b3728dc8675771956fc3479856b4002b3b Mon Sep 17 00:00:00 2001 From: Raymond Zhou <56318341+rayzhou-bit@users.noreply.github.com> Date: Thu, 3 Oct 2024 13:52:55 -0400 Subject: [PATCH 2/2] chore: version bump (#115) --- calc/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/calc/__init__.py b/calc/__init__.py index 1a9b5fa..1a2dd23 100644 --- a/calc/__init__.py +++ b/calc/__init__.py @@ -6,4 +6,4 @@ from .calc import * -__version__ = '3.1.0' +__version__ = '3.1.2'