From f9ce0bc32db3bdd8a5947a18039c557c750f2957 Mon Sep 17 00:00:00 2001 From: Frederick Muriuki Muriithi Date: Fri, 9 Sep 2022 06:44:59 +0300 Subject: Refactor: Add tests and handle edge case Remove mutation of state, and handle the edge case where the sub-sequence could be an empty sequence. --- .../tests/unit/wqflask/show_trait/test_get_max_digits.py | 13 +++++++++++++ wqflask/wqflask/show_trait/show_trait.py | 11 +++++------ 2 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 wqflask/tests/unit/wqflask/show_trait/test_get_max_digits.py diff --git a/wqflask/tests/unit/wqflask/show_trait/test_get_max_digits.py b/wqflask/tests/unit/wqflask/show_trait/test_get_max_digits.py new file mode 100644 index 00000000..509f6c3a --- /dev/null +++ b/wqflask/tests/unit/wqflask/show_trait/test_get_max_digits.py @@ -0,0 +1,13 @@ +import pytest + +from wqflask.show_trait.show_trait import get_max_digits + +@pytest.mark.parametrize( + "trait_vals,expected", + ((( + (0, 1345, 92, 734), + (234253, 33, 153, 5352), + (3542, 24, 135)), + [3, 5, 3]),)) +def test_get_max_digits(trait_vals, expected): + assert get_max_digits(trait_vals) == expected diff --git a/wqflask/wqflask/show_trait/show_trait.py b/wqflask/wqflask/show_trait/show_trait.py index f7dbf8df..ae6cf0cf 100644 --- a/wqflask/wqflask/show_trait/show_trait.py +++ b/wqflask/wqflask/show_trait/show_trait.py @@ -555,13 +555,12 @@ def get_trait_vals(sample_list): return trait_vals def get_max_digits(trait_vals): - max_digits = [] - for these_vals in trait_vals: - max_val = max(these_vals) - digits = len(str(max_val)) - max_digits.append(digits - 1) + def __max_digits__(these_vals): + if not bool(these_vals): + return None + return len(str(max(these_vals))) - 1 - return max_digits + return [__max_digits__(val) for val in trait_vals] def normf(trait_vals): ranked_vals = ss.rankdata(trait_vals) -- cgit v1.2.3