Skip to content

Commit

Permalink
Util string_to_list with braces (#1361)
Browse files Browse the repository at this point in the history
* Allow braces to encapsulate whitespace in Util.string_to_list

* Tests for string_to_list with braces
  • Loading branch information
avanwinkle committed May 19, 2019
1 parent 1e0dd67 commit b625a93
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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()
# 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

0 comments on commit b625a93

Please sign in to comment.