Skip to content

Commit 89c1261

Browse files
committed
Use named groups for PGN annotation regexes
1 parent 8ebc881 commit 89c1261

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

chess/pgn.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -120,20 +120,20 @@
120120
SKIP_MOVETEXT_REGEX = re.compile(r""";|\{|\}""")
121121

122122

123-
CLOCK_REGEX = re.compile(r"""\[%clk\s(\d+):(\d+):(\d+(?:\.\d*)?)\]""")
124-
EMT_REGEX = re.compile(r"""\[%emt\s(\d+):(\d+):(\d+(?:\.\d*)?)\]""")
123+
CLOCK_REGEX = re.compile(r"""\[%clk\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\]""")
124+
EMT_REGEX = re.compile(r"""\[%emt\s(?P<hours>\d+):(?P<minutes>\d+):(?P<seconds>\d+(?:\.\d*)?)\]""")
125125

126126
EVAL_REGEX = re.compile(r"""
127127
\[%eval\s(?:
128-
\#([+-]?\d+)
129-
|([+-]?(?:\d{0,10}\.\d{1,2}|\d{1,10}\.?))
128+
\#(?P<mate>[+-]?\d+)
129+
|(?P<cp>[+-]?(?:\d{0,10}\.\d{1,2}|\d{1,10}\.?))
130130
)(?:
131-
,(\d+)
131+
,(?P<depth>\d+)
132132
)?\]
133133
""", re.VERBOSE)
134134

135135
ARROWS_REGEX = re.compile(r"""
136-
\[%(?:csl|cal)\s(
136+
\[%(?:csl|cal)\s(?P<arrows>
137137
[RGYB][a-h][1-8](?:[a-h][1-8])?
138138
(?:,[RGYB][a-h][1-8](?:[a-h][1-8])?)*
139139
)\]
@@ -399,16 +399,16 @@ def eval(self) -> Optional[chess.engine.PovScore]:
399399

400400
turn = self.turn()
401401

402-
if match.group(1):
403-
mate = int(match.group(1))
402+
if match.group("mate"):
403+
mate = int(match.group("mate"))
404404
score: chess.engine.Score = chess.engine.Mate(mate)
405405
if mate == 0:
406406
# Resolve this ambiguity in the specification in favor of
407407
# standard chess: The player to move after mate is the player
408408
# who has been mated.
409409
return chess.engine.PovScore(score, turn)
410410
else:
411-
score = chess.engine.Cp(int(float(match.group(2)) * 100))
411+
score = chess.engine.Cp(int(float(match.group("cp")) * 100))
412412

413413
return chess.engine.PovScore(score if turn else -score, turn)
414414

@@ -418,7 +418,7 @@ def eval_depth(self) -> Optional[int]:
418418
this node and returns the corresponding depth, if any.
419419
"""
420420
match = EVAL_REGEX.search(self.comment)
421-
return int(match.group(3)) if match and match.group(3) else None
421+
return int(match.group("depth")) if match and match.group("depth") else None
422422

423423
def set_eval(self, score: Optional[chess.engine.PovScore], depth: Optional[int] = None) -> None:
424424
"""
@@ -450,7 +450,7 @@ def arrows(self) -> List[chess.svg.Arrow]:
450450
"""
451451
arrows = []
452452
for match in ARROWS_REGEX.finditer(self.comment):
453-
for group in match.group(1).split(","):
453+
for group in match.group("arrows").split(","):
454454
arrows.append(chess.svg.Arrow.from_pgn(group))
455455

456456
return arrows
@@ -493,7 +493,7 @@ def clock(self) -> Optional[float]:
493493
match = CLOCK_REGEX.search(self.comment)
494494
if match is None:
495495
return None
496-
return int(match.group(1)) * 3600 + int(match.group(2)) * 60 + float(match.group(3))
496+
return int(match.group("hours")) * 3600 + int(match.group("minutes")) * 60 + float(match.group("seconds"))
497497

498498
def set_clock(self, seconds: Optional[float]) -> None:
499499
"""
@@ -527,7 +527,7 @@ def emt(self) -> Optional[float]:
527527
match = EMT_REGEX.search(self.comment)
528528
if match is None:
529529
return None
530-
return int(match.group(1)) * 3600 + int(match.group(2)) * 60 + float(match.group(3))
530+
return int(match.group("hours")) * 3600 + int(match.group("minutes")) * 60 + float(match.group("seconds"))
531531

532532
def set_emt(self, seconds: Optional[float]) -> None:
533533
"""

0 commit comments

Comments
 (0)