From b8e368b94e313ff364a73cfb97ba86091cb25f35 Mon Sep 17 00:00:00 2001 From: int3l Date: Sun, 24 Nov 2019 01:17:47 +0200 Subject: [PATCH 1/4] Nicer pep number matching and extraction Covers the following cases: !pep 1234 test !pep 1234test !pep1234 test !pep1245test !pep test !pep --- bot.py | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/bot.py b/bot.py index 2527d8f..6e77729 100644 --- a/bot.py +++ b/bot.py @@ -295,18 +295,12 @@ def cmd_uptime(match: Match[str]) -> Response: return UptimeResponse() -@handle_message('!pep') +@handle_message('!pep[ ]?(?P\d{1,4})') def cmd_pep(match: Match[str]) -> Response: - *_, msg = match.groups() - *_, rest = msg.partition(' ') - try: - pep = str(int(rest)).zfill(4) - except ValueError: - return MessageResponse(match, 'Please make sure you gave me a number!') - else: - return MessageResponse( - match, f'https://www.python.org/dev/peps/pep-{pep}/', - ) + *_, number = match.groups() + return MessageResponse( + match, f'https://www.python.org/dev/peps/pep-{number.zfill(4)}/', + ) COMMAND_RE = re.compile(r'!\w+') From 60c0fb436679906135a244e17e8d54cd8c632515 Mon Sep 17 00:00:00 2001 From: int3l Date: Sun, 24 Nov 2019 01:23:29 +0200 Subject: [PATCH 2/4] Satisfy flake8 --- bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 6e77729..226c213 100644 --- a/bot.py +++ b/bot.py @@ -295,7 +295,7 @@ def cmd_uptime(match: Match[str]) -> Response: return UptimeResponse() -@handle_message('!pep[ ]?(?P\d{1,4})') +@handle_message(r'!pep[ ]?(?P\d{1,4})') def cmd_pep(match: Match[str]) -> Response: *_, number = match.groups() return MessageResponse( From d4c565fea9f59a3435e2c2c9e2052fd2da34b9b7 Mon Sep 17 00:00:00 2001 From: int3l Date: Sun, 24 Nov 2019 01:33:22 +0200 Subject: [PATCH 3/4] Convert weird Unicode integers to ASCII format --- bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 226c213..58cb723 100644 --- a/bot.py +++ b/bot.py @@ -299,7 +299,7 @@ def cmd_uptime(match: Match[str]) -> Response: def cmd_pep(match: Match[str]) -> Response: *_, number = match.groups() return MessageResponse( - match, f'https://www.python.org/dev/peps/pep-{number.zfill(4)}/', + match, f'https://www.python.org/dev/peps/pep-{int(number).zfill(4)}/', ) From aa9c6983bc2c83edc04cc86bb04aa82655470a8e Mon Sep 17 00:00:00 2001 From: int3l Date: Sun, 24 Nov 2019 01:43:08 +0200 Subject: [PATCH 4/4] Avoid conversion, match only ASCII numbers --- bot.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bot.py b/bot.py index 58cb723..2a3da9c 100644 --- a/bot.py +++ b/bot.py @@ -295,11 +295,11 @@ def cmd_uptime(match: Match[str]) -> Response: return UptimeResponse() -@handle_message(r'!pep[ ]?(?P\d{1,4})') +@handle_message(r'!pep[ ]?(?P\d{1,4})', flags=re.ASCII) def cmd_pep(match: Match[str]) -> Response: *_, number = match.groups() return MessageResponse( - match, f'https://www.python.org/dev/peps/pep-{int(number).zfill(4)}/', + match, f'https://www.python.org/dev/peps/pep-{number.zfill(4)}/', )