Return new list of changed strings

This commit is contained in:
Patrick Elmer 2023-02-06 15:59:09 +09:00
parent 427073e52a
commit 5956894770
2 changed files with 15 additions and 5 deletions

View File

@ -2,7 +2,10 @@ import re
def apply(changes, strings, categories={}, ignore_errors=True, apply=True, zero_characters=['']): def apply(changes, strings, categories={}, ignore_errors=True, apply=True, zero_characters=['']):
"""Apply a sound change to a given string""" """
Applies a sound change or a list of sound changes to a string or
a list of given strings.
"""
if not apply: if not apply:
return strings return strings
@ -13,13 +16,15 @@ def apply(changes, strings, categories={}, ignore_errors=True, apply=True, zero_
return_str = isinstance(strings, str) return_str = isinstance(strings, str)
if isinstance(strings, str): if isinstance(strings, str):
strings = [strings] strings = [strings]
else:
strings = strings.copy()
for change in changes: for change in changes:
v = validate_change(change, ignore_errors=ignore_errors)
if v == False:
continue
change = convert_change_to_regex(change, categories=categories, zero_characters=zero_characters)
if validate_change(change, ignore_errors=ignore_errors) == False:
continue
change = convert_change_to_regex(change, categories=categories, zero_characters=zero_characters)
original, change_to, before, after = split_change(change) original, change_to, before, after = split_change(change)
pattern = f"({before})({original})({after})" pattern = f"({before})({original})({after})"

View File

@ -92,3 +92,8 @@ def test_multiple_changes_and_strings():
def test_applying_invalid_change(): def test_applying_invalid_change():
assert apply('', 'pana') == 'pana' assert apply('', 'pana') == 'pana'
assert apply(['p>f', '>', 'f>h'], 'pana') == 'hana' assert apply(['p>f', '>', 'f>h'], 'pana') == 'hana'
def test_original_list_remains_unchanged():
strings = ['abc', 'bcd', 'cde']
new_strings = apply('c>f', strings)
assert strings != new_strings