Minor tweaks to Python number parsing
authorJakob Cornell <jakob+gpg@jcornell.net>
Fri, 22 Sep 2023 03:34:18 +0000 (22:34 -0500)
committerJakob Cornell <jakob+gpg@jcornell.net>
Fri, 22 Sep 2023 03:35:21 +0000 (22:35 -0500)
strikebot/strikebot/src/strikebot/updates.py

index 01eb67b57d978be8456bbaf793b076839bd1ab53..15c84727f5d9b6779abb6c5dfec344528fba7a78 100644 (file)
@@ -125,15 +125,16 @@ def _parse_from_lines(lines: list[str], curr_count: Optional[int], bot_user: str
                                        typo = True
                                elif curr_count is not None and abs(curr_count) >= 100 and bool(match["neg"]) == (curr_count < 0):
                                        goal_parts = _separate(str(abs(curr_count)))
+                                       [*head, tail] = goal_parts
                                        partials = [
-                                               goal_parts[: -1] + [goal_parts[-1][: -1]],  # missing last digit
-                                               goal_parts[: -1] + [goal_parts[-1][: -2]],  # missing last two digits
-                                               goal_parts[: -1] + [goal_parts[-1][: -2] + goal_parts[-1][-1]],  # missing second-last digit
+                                               [*head, tail[:-1]],  # missing last digit
+                                               [*head, tail[:-2]],  # missing last two digits
+                                               [*head, tail[:-2] + tail[-1]],  # missing second-last digit
                                        ]
                                        if parts in partials:
                                                # missing any of last two digits
                                                typo = True
-                                       elif any(parts == p[: -1] + [p[-1] + goal_parts[0]] + goal_parts[1 :] for p in partials):
+                                       elif any(parts == [*p[:-1], p[-1] + goal_parts[0], *goal_parts[1:]] for p in partials):
                                                # double paste
                                                typo = True
 
@@ -144,8 +145,8 @@ def _parse_from_lines(lines: list[str], curr_count: Optional[int], bot_user: str
                        else:
                                groups_okay = True
                                if curr_count is not None and sep and sep.isspace():
-                                       # Presume that the intended count consists of as many valid digit groups as necessary to match the
-                                       # number of digits in the expected count, if possible.
+                                       # Presume that the intended count consists of as many valid digit groups as
+                                       # necessary to match the number of digits in the expected count, if possible.
                                        digit_count = len(str(abs(curr_count)))
                                        use_parts = []
                                        accum = 0
@@ -157,13 +158,15 @@ def _parse_from_lines(lines: list[str], curr_count: Optional[int], bot_user: str
                                                else:
                                                        break
 
-                                       # could still be a no-separator count with some extra digit groups on the same line
+                                       # could still be a no-separator count with some extra digit groups on the same
+                                       # line
                                        if not use_parts:
                                                use_parts = [parts[0]]
 
                                        lone = lone and len(use_parts) == len(parts)
                                else:
-                                       # current count is unknown or no separator was used
+                                       # current count is unknown, or any detected separator unambiguously delineates
+                                       # the number
                                        groups_okay = all_parts_valid
                                        use_parts = parts