Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reworked perform_text_replacement function #34

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions bot/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
from collections import namedtuple
from string import punctuation
from typing import Union

from slackclient import SlackClient
Expand Down Expand Up @@ -46,6 +47,8 @@
"username": get_user_name,
}

_PUNCTUATION_TO_SPACE = str.maketrans({p:" " for p in punctuation})

Message = namedtuple("Message", "user_id channel_id text")


Expand Down Expand Up @@ -203,20 +206,17 @@ def perform_bot_cmd(msg, private=True):

def perform_text_replacements(text: str) -> Union[str, None]:
"""Replace first matching word in text with a little easter egg"""
words = text.lower().split()
strip_chars = "?!"
matching_words = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was redundant, nice concise solution @JnyJny

Copy link
Collaborator

@pybites pybites Jan 31, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you remember what happened in Slack that triggered you to fix this? Would be nice to document it in a test. Thanks for improving this part of the code!

word.strip(strip_chars)
for word in words
if word.strip(strip_chars) in TEXT_FILTER_REPLIES
]

if not matching_words:
return None
words = text.translate(_PUNCTUATION_TO_SPACE).casefold().split()

for word in words:
try:
return f"To _{word}_ I say: {TEXT_FILTER_REPLIES[word]}"
except KeyError:
continue

return None

match_word = matching_words[0]
replace_word = TEXT_FILTER_REPLIES.get(match_word)
return f"To _{match_word}_ I say: {replace_word}"


def parse_next_msg():
Expand Down