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

Group Negative Digits #445

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions mpfmc/tests/test_Text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
19 changes: 18 additions & 1 deletion mpfmc/widgets/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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).
Expand Down