Skip to content

Commit

Permalink
Drop newer features like match case and str.removeprefix
Browse files Browse the repository at this point in the history
  • Loading branch information
IsaacG authored and BethanyG committed Dec 18, 2022
1 parent 33afa50 commit 2fba891
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions exercises/practice/sgf-parsing/.meta/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,42 +64,41 @@ def parse_node(sgf: str) -> SgfTree:
children = []

while idx < len(sgf):
match sgf[idx]:
case "[":
# Parse property values.
if idx == prop_key_start:
raise ValueError("propery key is empty")
prop_key = sgf[prop_key_start:idx]
if not prop_key.isupper():
raise ValueError('property must be in uppercase')

idx, prop_vals = parse_property_vals(sgf, idx)
properties[prop_key].extend(prop_vals)

# New property.
prop_key_start = idx
case ";":
# Single child.
child = parse_node(sgf[idx:])
children.append(child)
break
case "(":
# Multiple children.
children = []
while idx < len(sgf):
if sgf[idx] != "(":
break
# Child start.
idx += 1
child_start = idx
while sgf[idx] != ")":
idx += 1
# Child end.
child = parse_node(sgf[child_start:idx])
children.append(child)
if sgf[idx] == "[":
# Parse property values.
if idx == prop_key_start:
raise ValueError("propery key is empty")
prop_key = sgf[prop_key_start:idx]
if not prop_key.isupper():
raise ValueError('property must be in uppercase')

idx, prop_vals = parse_property_vals(sgf, idx)
properties[prop_key].extend(prop_vals)

# New property.
prop_key_start = idx
elif sgf[idx] == ";":
# Single child.
child = parse_node(sgf[idx:])
children.append(child)
break
elif sgf[idx] == "(":
# Multiple children.
children = []
while idx < len(sgf):
if sgf[idx] != "(":
break
# Child start.
idx += 1
child_start = idx
while sgf[idx] != ")":
idx += 1
case _:
# Child end.
child = parse_node(sgf[child_start:idx])
children.append(child)
idx += 1
else:
idx += 1

if idx > prop_key_start and not properties:
raise ValueError('properties without delimiter')
Expand All @@ -112,4 +111,5 @@ def parse(sgf: str) -> SgfTree:
raise ValueError('tree missing')
if not sgf.startswith("(;"):
raise ValueError('tree with no nodes')
return parse_node(sgf.removeprefix("(").removesuffix(")"))
inside = sgf[1:-1]
return parse_node(inside)

0 comments on commit 2fba891

Please sign in to comment.