Skip to content

Commit 774b2c4

Browse files
jdeverasdelquin
authored andcommitted
Fix flake8 errors (#24)
And simplify time expression code with a pluralizer.
1 parent 46912af commit 774b2c4

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

bot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import logging
2-
import datetime
32

43
import telegram
54
from telegram.ext import Updater, Filters, MessageHandler, CommandHandler
@@ -90,7 +89,8 @@ def main():
9089
dp.add_handler(CommandHandler('start', command_start))
9190
dp.add_handler(CommandHandler('help', command_help))
9291
dp.add_handler(CommandHandler('status', command_status))
93-
dp.add_handler(MessageHandler(Filters.status_update.new_chat_members, welcome))
92+
dp.add_handler(MessageHandler(Filters.status_update.new_chat_members,
93+
welcome))
9494
dp.add_handler(MessageHandler(Filters.group, reply))
9595

9696
logger.info('Bot is ready')

config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
_config_registry = []
77

8+
89
class _ConfigItem(NamedTuple):
910
name: str
1011
value: Any
@@ -47,7 +48,8 @@ def log(logger_method):
4748
POLL_INTERVAL = config('POLL_INTERVAL', int, default=3)
4849

4950
# Bot message for start command
50-
BOT_GREETING = config('BOT_GREETING', default="Hi! I'm a friendly, slightly psychopath robot")
51+
BOT_GREETING = config('BOT_GREETING',
52+
default="Hi! I'm a friendly, slightly psychopath robot")
5153

5254
# A username longer than this will be considered non-human
5355
# - Allowed values: An integer larger than 1

utils.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
import datetime
33
import random
44
import re
5-
import typing
6-
from typing import Tuple
5+
from typing import Tuple, Optional, NamedTuple
76

87
from telegram import User
98
import config
@@ -75,13 +74,13 @@ def bot_wants_to_reply() -> bool:
7574
return random.random() < config.VERBOSITY
7675

7776

78-
class BotReplySpec(typing.NamedTuple):
77+
class BotReplySpec(NamedTuple):
7978
message: str
8079
trigger: str
8180
reply: str
8281

8382

84-
def triggers_reply(message: str) -> typing.Optional[BotReplySpec]:
83+
def triggers_reply(message: str) -> Optional[BotReplySpec]:
8584
for trigger_words, bot_reply in config.REPLIES.items():
8685
regex = get_reply_regex(trigger_words)
8786
match = regex.search(message)
@@ -96,6 +95,12 @@ def triggers_reply(message: str) -> typing.Optional[BotReplySpec]:
9695
return None
9796

9897

98+
def pluralise(number: int, singular: str, plural: Optional[str] = None) -> str:
99+
if plural is None:
100+
plural = f"{singular}s"
101+
return singular if number == 1 else plural
102+
103+
99104
def since(dt=None, reference=datetime.datetime.now()) -> str:
100105
"""Returns a textual description of time passed.
101106
@@ -115,15 +120,15 @@ def since(dt=None, reference=datetime.datetime.now()) -> str:
115120
buff = []
116121
days = delta.days
117122
if days:
118-
buff.append(f"{days} day" if days == 1 else f"{days} days")
123+
buff.append(f"{days} {pluralise(days, 'day')}")
119124
seconds = delta.seconds
120125
if seconds > 3600:
121126
hours = seconds // 3600
122-
buff.append(f"{hours} hour" if hours == 1 else f"{hours} hours")
127+
buff.append(f"{hours} {pluralise(hours, 'hour')}")
123128
seconds = seconds % 3600
124129
minutes = seconds // 60
125130
if minutes > 0:
126-
buff.append(f"{minutes} minute" if minutes == 1 else f"{minutes} minutes")
131+
buff.append(f"{minutes} {pluralise(minutes, 'minute')}")
127132
seconds = seconds % 60
128-
buff.append(f"{seconds} second" if seconds == 1 else f"{seconds} seconds")
133+
buff.append(f"{seconds} {pluralise(seconds, 'second')}")
129134
return " ".join(buff)

0 commit comments

Comments
 (0)