From a74f7cd8c11f22c7afa0b7e703ec4d5d8a61c6b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= <47358913+isidentical@users.noreply.github.com> Date: Sun, 20 Oct 2019 00:48:21 +0300 Subject: [PATCH 1/2] get rid of itertools, reply with the matched case --- bot.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/bot.py b/bot.py index f08fdfa..40ecc28 100644 --- a/bot.py +++ b/bot.py @@ -1,7 +1,6 @@ import argparse import asyncio import datetime -import itertools import json import random import re @@ -321,12 +320,9 @@ def msg_ping(match: Match[str]) -> Response: @handle_message(r'.*\b(nano|linux|windows|emacs)\b', flags=re.IGNORECASE) def msg_gnu_please(match: Match[str]) -> Response: msg, word = match[3], match[4] - cases = { - f'{gnu}{sep}{word}' - for gnu, sep in itertools.product(('gnu', 'GNU'), '/+') - } - if msg in cases: - return MessageResponse(match, f'YES! GNU/{esc(word)}') + query = re.match(fr'(?i)gnu(\/|\+){word}', msg) + if query: + return MessageResponse(match, f'YES! {query.match(0)}') else: return MessageResponse(match, f"Um please, it's GNU/{esc(word)}!") From f81261991d575faefef1bfa030d250a5b49a1969 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Ta=C5=9Fkaya?= <47358913+isidentical@users.noreply.github.com> Date: Sun, 20 Oct 2019 00:57:56 +0300 Subject: [PATCH 2/2] use proper regex --- bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bot.py b/bot.py index 40ecc28..55fef1f 100644 --- a/bot.py +++ b/bot.py @@ -320,7 +320,7 @@ def msg_ping(match: Match[str]) -> Response: @handle_message(r'.*\b(nano|linux|windows|emacs)\b', flags=re.IGNORECASE) def msg_gnu_please(match: Match[str]) -> Response: msg, word = match[3], match[4] - query = re.match(fr'(?i)gnu(\/|\+){word}', msg) + query = re.match(fr'gnu[/+]{word}', msg, flags=re.IGNORECASE) if query: return MessageResponse(match, f'YES! {query.match(0)}') else: