From d8b4673717ca14c4796f3b5c6d705203131df3ea Mon Sep 17 00:00:00 2001 From: Avery Tummons Date: Thu, 15 Jun 2023 11:46:46 -0500 Subject: [PATCH 1/2] Group Negative Digits This changes from isDigit (only finds positive numbers) to casting to int to find all numbers. If an int is found, it converts it back to a string and strips the negative sign from it. It will then group as normal and replace the number portion with the grouped digits and leave the negative sign intact. --- mpfmc/widgets/text.py | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mpfmc/widgets/text.py b/mpfmc/widgets/text.py index 9dcb9cc7..d2757153 100644 --- a/mpfmc/widgets/text.py +++ b/mpfmc/widgets/text.py @@ -250,7 +250,7 @@ def update_text(self, text: str) -> None: if self.config['number_grouping']: # find the numbers in the string - number_list = [s for s in text.split() if s.isdigit()] + number_list = Text.find_numbers(text) # group the numbers and replace them in the string for item in number_list: @@ -306,6 +306,23 @@ def prepare_for_removal(self) -> None: self.mc.events.remove_handler(self._player_var_change) self.mc.events.remove_handler(self._machine_var_change) + @staticmethod + def find_numbers(text: str): + """Sorts through the text string passed in to find all actual digits. + This includes both positive and negative numbers, which requires finding + ints and not just digits + + Args: + text: The incoming string of text + """ + number_list = [s for s in text.split()] + for item in number_list: + try: + number_list[number_list.index(item)] = str(int(number_list[number_list.index(item)].lstrip("-"))) + except ValueError: + pass + return number_list + @staticmethod def group_digits(text: str, separator: str = ',', group_size: int = 3) -> str: """Enable digit grouping (i.e. adds comma separators between thousands digits). From 7e59efb8960b0209422359712eeb43f73c8af38d Mon Sep 17 00:00:00 2001 From: Avery Tummons Date: Thu, 15 Jun 2023 11:51:45 -0500 Subject: [PATCH 2/2] Number Grouping Test Cases --- mpfmc/tests/test_Text.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/mpfmc/tests/test_Text.py b/mpfmc/tests/test_Text.py index 5ad8c807..1487b0cc 100644 --- a/mpfmc/tests/test_Text.py +++ b/mpfmc/tests/test_Text.py @@ -349,6 +349,42 @@ def test_number_grouping(self): self.assertEqual(self.get_widget().text, '2,000,000') self.advance_time() + def test_number_grouping_with_text(self): + self.mc.events.post('number_grouping') + self.advance_time() + + # should be 00 even though text is 0 + self.assertEqual(self.get_widget().text, '00') + self.advance_time() + + self.get_widget().update_text('2000000 abc') + self.assertEqual(self.get_widget().text, '2,000,000 abc') + self.advance_time() + + def test_negative_number_grouping(self): + self.mc.events.post('number_grouping') + self.advance_time() + + # should be 00 even though text is 0 + self.assertEqual(self.get_widget().text, '00') + self.advance_time() + + self.get_widget().update_text('-123456') + self.assertEqual(self.get_widget().text, '-123,456') + self.advance_time() + + def test_multiple_number_grouping(self): + self.mc.events.post('number_grouping') + self.advance_time() + + # should be 00 even though text is 0 + self.assertEqual(self.get_widget().text, '00') + self.advance_time() + + self.get_widget().update_text('2000000 -9 -1234') + self.assertEqual(self.get_widget().text, '2,000,000 -9 -1,234') + self.advance_time() + def test_text_casing(self): self.mc.events.post('text_nocase') self.advance_time()