From cb7044a4c14a70354740d976a34c7c696266fa6e Mon Sep 17 00:00:00 2001 From: Jakob Cornell Date: Thu, 21 Sep 2023 22:34:18 -0500 Subject: [PATCH] Minor tweaks to Python number parsing --- strikebot/strikebot/src/strikebot/updates.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/strikebot/strikebot/src/strikebot/updates.py b/strikebot/strikebot/src/strikebot/updates.py index 01eb67b..15c8472 100644 --- a/strikebot/strikebot/src/strikebot/updates.py +++ b/strikebot/strikebot/src/strikebot/updates.py @@ -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 -- 2.30.2