diff --git a/problem.py b/problem.py index 9108837..cbb5a25 100644 --- a/problem.py +++ b/problem.py @@ -51,7 +51,7 @@ class Construction: @classmethod def from_txt(cls, data: str) -> Construction: - data = data.split(' ') + data = data.strip().split() return Construction(data[0], data[1:]) def __init__(self, name: str, args: list[str]): @@ -71,12 +71,13 @@ class Clause: @classmethod def from_txt(cls, data: str) -> Clause: - if data == ' =': + data = data.strip() + if data == '=': return Clause([], []) - points, constructions = data.split(' = ') + points, constructions = data.split('=', maxsplit=1) return Clause( - points.split(' '), - [Construction.from_txt(c) for c in constructions.split(', ')], + points.strip().split(), + [Construction.from_txt(c) for c in constructions.split(',')], ) def __init__(self, points: list[str], constructions: list[Construction]): @@ -162,15 +163,15 @@ def from_txt(cls, data: str, translate: bool = True) -> Problem: """Load a problem from a str object.""" url = '' if '\n' in data: - url, data = data.split('\n') + url, data = data.split('\n', maxsplit=1) - if ' ? ' in data: - clauses, goal = data.split(' ? ') + if '?' in data: + clauses, goal = data.split('?', maxsplit=1) goal = Construction.from_txt(goal) else: clauses, goal = data, None - clauses = clauses.split('; ') + clauses = [c.strip() for c in clauses.split(';') if c.strip()] problem = Problem( url=url, clauses=[Clause.from_txt(c) for c in clauses], goal=goal ) diff --git a/problem_test.py b/problem_test.py index b8fe3ba..357627d 100644 --- a/problem_test.py +++ b/problem_test.py @@ -41,6 +41,21 @@ def test_orthocenter_no_translate(self): '{S} a : ; b : ; c : ; h : T a b c h 00 T a c b h 01 ? T a h b c', ) + def test_problem_parser_allows_whitespace_around_delimiters(self): + canonical = ( + 'a b c = triangle a b c; h = on_tline h b a c, ' + 'on_tline h c a b ? perp a h b c' + ) + irregular = ( + ' a b c=triangle a b c ;h = on_tline h b a c, ' + 'on_tline h c a b; ?perp a h b c ' + ) + + expected = pr.Problem.from_txt(canonical, translate=False) + actual = pr.Problem.from_txt(irregular, translate=False) + + self.assertEqual(actual.txt(), expected.txt()) + def test_orthocenter_translate(self): txt = 'a b c = triangle a b c; h = on_tline h b a c, on_tline h c a b ? perp a h b c' # pylint: disable=line-too-long