Как мне сделать это по DRY
import telebot
import time
bot = telebot.TeleBot('код')
## Предметы
his = 'История'
ru = 'Русский язык'
ph = 'Физика'
geog = 'География'
al = 'Алгебра'
de = 'Немецкий язык'
geom = 'Геометрия'
che = 'Химия'
bio = 'Биология'
eng = 'Английский язык'
sp = 'Физ-ра'
lit = 'Лит-ра'
p_math = 'Практикум по математике'
inf = 'Информатика'
soc = 'Обществознание'
p_de = 'Практикум по немецкому'
##Скрипт с временем
chat_id_class9b = 'айди'
time_now = time.localtime()
print(time_now.tm_hour, ':', time_now.tm_min)
#Понедельник
if time_now.tm_wday == 0:
if (time_now.tm_hour == 8) & (time_now.tm_min == 40):
send0 = his
bot.send_message(chat_id_class9b, send0)
if (time_now.tm_hour == 9) & (time_now.tm_min == 40):
send0 = ru
bot.send_message(chat_id_class9b, send0)
if (time_now.tm_hour == 10) & (time_now.tm_min == 40):
send0 = ph
bot.send_message(chat_id_class9b, send0)
if (time_now.tm_hour == 11) & (time_now.tm_min == 40):
send0 = geog
bot.send_message(chat_id_class9b, send0)
if (time_now.tm_hour == 12) & (time_now.tm_min == 40):
send0 = de
bot.send_message(chat_id_class9b, send0)
if (time_now.tm_hour == 13) & (time_now.tm_min == 40):
send0 = al
bot.send_message(chat_id_class9b, send0)
if (time_now.tm_hour == 14) & (time_now.tm_min == 40):
send0 = geom
bot.send_message(chat_id_class9b, send0)
# Вторник
if time_now.tm_wday == 1:
if (time_now.tm_hour == 8) & (time_now.tm_min == 40):
send1 = de
bot.send_message(chat_id_class9b, send1)
if (time_now.tm_hour == 9) & (time_now.tm_min == 40):
send1 = al
bot.send_message(chat_id_class9b, send1)
if (time_now.tm_hour == 10) & (time_now.tm_min == 40):
send1 = eng
bot.send_message(chat_id_class9b, send1)
if (time_now.tm_hour == 11) & (time_now.tm_min == 40):
send1 = che
bot.send_message(chat_id_class9b, send1)
if (time_now.tm_hour == 12) & (time_now.tm_min == 40):
send1 = bio
bot.send_message(chat_id_class9b, send1)
if (time_now.tm_hour == 13) & (time_now.tm_min == 40):
send1 = soc
bot.send_message(chat_id_class9b, send1)
if (time_now.tm_hour == 14) & (time_now.tm_min == 40):
send1 = his
bot.send_message(chat_id_class9b, send1)
# Среда
if time_now.tm_wday == 2:
if (time_now.tm_hour == 8) & (time_now.tm_min == 40):
send2 = al
bot.send_message(chat_id_class9b, send2)
if (time_now.tm_hour == 9) & (time_now.tm_min == 40):
send2 = de
bot.send_message(chat_id_class9b, send2)
if (time_now.tm_hour == 10) & (time_now.tm_min == 40):
send2 = sp
bot.send_message(chat_id_class9b, send2)
if (time_now.tm_hour == 11) & (time_now.tm_min == 40):
send2 = ru
bot.send_message(chat_id_class9b, send2)
if (time_now.tm_hour == 12) & (time_now.tm_min == 40):
send2 = lit
bot.send_message(chat_id_class9b, send2)
if (time_now.tm_hour == 13) & (time_now.tm_min == 40):
send2 = che
bot.send_message(chat_id_class9b, send2)
# Четверг
if time_now.tm_wday == 3:
if (time_now.tm_hour == 8) & (time_now.tm_min == 40):
send3 = geom
bot.send_message(chat_id_class9b, send3)
if (time_now.tm_hour == 9) & (time_now.tm_min == 40):
send3 = de
bot.send_message(chat_id_class9b, send3)
if (time_now.tm_hour == 10) & (time_now.tm_min == 40):
send3 = lit
bot.send_message(chat_id_class9b, send3)
if (time_now.tm_hour == 11) & (time_now.tm_min == 40):
send3 = bio
bot.send_message(chat_id_class9b, send3)
if (time_now.tm_hour == 12) & (time_now.tm_min == 40):
send3 = inf
bot.send_message(chat_id_class9b, send3)
if (time_now.tm_hour == 13) & (time_now.tm_min == 40):
send3 = al
bot.send_message(chat_id_class9b, send3)
# Пятница
if time_now.tm_wday == 4:
if (time_now.tm_hour == 8) & (time_now.tm_min == 40):
send4 = lit
bot.send_message(chat_id_class9b, send4)
if (time_now.tm_hour == 9) & (time_now.tm_min == 40):
send4 = ph
bot.send_message(chat_id_class9b, send4)
if (time_now.tm_hour == 10) & (time_now.tm_min == 40):
send4 = inf
bot.send_message(chat_id_class9b, send4)
if (time_now.tm_hour == 11) & (time_now.tm_min == 40):
send4 = eng
bot.send_message(chat_id_class9b, send4)
if (time_now.tm_hour == 12) & (time_now.tm_min == 40):
send4 = geog
bot.send_message(chat_id_class9b, send4)
if (time_now.tm_hour == 13) & (time_now.tm_min == 40):
send4 = geom
bot.send_message(chat_id_class9b, send4)
if (time_now.tm_hour == 14) & (time_now.tm_min == 40):
send4 = sp
bot.send_message(chat_id_class9b, send4)
bot.polling()
Ответы (1 шт):
Автор решения: CrazyElf
→ Ссылка
Например, так:
his = 'История'
...
schedule = {
(0, 8): his,
...
}
if time_now.tm_min == 40:
send0 = schedule.get((time_now.tm_wday, time_now.tm_hour))
if send0:
bot.send_message(chat_id_class9b, send0)
Заполняете только словарь schedule нужными значениями и всё, if-ы только эти два, которые у меня в коде, остальные все можно выкинуть. Обратите внимание на двойные скобки у schedule.get, это важно! Ключ словаря - кортеж из дня недели и часа, поэтому так.
Структуру словаря можно и другую сделать, например, вложенную - на верхнем уровне день недели, внутри ещё словарь по часам. но это уже детали, принцип работы будет примерно тот же. DRY вполне соблюдён.