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

Util string_to_list with braces #1361

Merged
merged 3 commits into from May 19, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 6 additions & 2 deletions mpf/core/utility_functions.py
Expand Up @@ -102,8 +102,12 @@ def string_to_list(string: Union[str, List[str], None]) -> List[str]:

"""
if isinstance(string, str):
# Convert commas to spaces, then split the string into a list
new_list = string.replace(',', ' ').split()
if "{" in string:
# Split the string on spaces/commas EXCEPT regions within braces
new_list = re.findall(r'([\w|-]+?\{.*?\}|[\w|-]+)', string)
else:
# Convert commas to spaces, then split the string into a list
new_list = string.replace(",", " ").split()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess whitespace and/or comma is also not such a good idea, right? We should convert this into something more explicit (not necessarily now)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like an "it was good enough at the time" decisions, and generally speaking it's sufficient. I wouldn't want to change anything that would break existing configs, although accepting both commas and spaces is more complex than doing strict space separation.

# Look for string values of "None" and convert them to Nonetypes.
for index, value in enumerate(new_list):
if isinstance(value, str) and len(value) == 4 and value.lower() == 'none':
Expand Down
7 changes: 7 additions & 0 deletions mpf/tests/test_Utility_Functions.py
Expand Up @@ -55,6 +55,13 @@ def test_string_to_list(self):
self.assertEqual(type(result), list)
self.assertFalse(result)

my_string = '0 1 2{not split here} 3 4 5'
result = Util.string_to_list(my_string)
self.assertEqual(type(result), list)
self.assertEqual(result[1], '1')
self.assertEqual(result[2], '2{not split here}')
self.assertEqual(result[3], '3')

def test_string_to_lowercase_list(self):
my_string = 'A B c d e'
result = Util.string_to_lowercase_list(my_string)
Expand Down