Conditions | 9 |
Total Lines | 54 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | import re |
||
98 | span.decompose() |
||
99 | for br in soup.find_all("br"): |
||
100 | soup.br.insert_before(" ") |
||
101 | br.decompose() |
||
102 | soup = soup.find_all("table", {"class": "tbl"})[1] |
||
103 | sch = [] |
||
104 | rows = soup.find_all("tr") |
||
105 | for row in rows: |
||
106 | cols = row.find_all("td") |
||
107 | cols = [el.text.strip() for el in cols] |
||
108 | sch.append([el for el in cols if el]) |
||
109 | sch = [el for el in sch if len(el) > 1] |
||
110 | return sch |
||
111 | |||
112 | def generate(self) -> str: |
||
113 | """Собирает расписание в читаемую строку |
||
114 | """ |
||
115 | sch = self.clean() |
||
116 | msg = "" |
||
117 | replacements = { |
||
118 | "Лекция": "(Л)", |
||
119 | "Лабораторная работа": "(Л/Р)", |
||
120 | "Зачет": "(З)", |
||
121 | "Диф. зачет": "(ДифЗ)", |
||
122 | "Диф.зачет": "(ДифЗ)", |
||
123 | "Экзамен": "(Э)", |
||
124 | "(английский язык)": "", |
||
125 | } |
||
126 | for i in range(len(sch)): |
||
127 | for j in range(len(sch[i])): |
||
128 | item = sch[i][j] |
||
129 | for k, v in replacements.items(): |
||
130 | item = re.sub(k, v, item) |
||
131 | if re.findall("Иностранный язык", item): |
||
132 | lesson = re.compile(r"(?<=\()\D+(?=\))").findall(item) |
||
133 | item = ( |
||
134 | f"Английский язык ({lesson[1]}) " |
||
135 | f"Кузнецова И.Н/Коротина М.А. 12/13а" |
||
136 | ) |
||
137 | sch[i][j + 1] = "" |
||
138 | msg += f"{item} " |
||
139 | msg += "\n" |
||
140 | date = datetime.strptime(self.date, "%Y-%m-%d").strftime("%d.%m.%Y") |
||
141 | msg = f"Расписание на {date}:\n{msg}" |
||
142 | return msg |
||
143 | |||
144 | def send(self): |
||
145 | """Отправляет расписание в активную беседу |
||
146 | и в ЛС подписчикам рассылки "Расписание" |
||
147 | """ |
||
148 | bot = Bot() |
||
149 | sch = self.generate() |
||
150 | if sch: |
||
151 | bot.send_mailing(slug="schedule", text=sch) |
||
152 | bot.send_message(msg=sch, pid=bot.cid) |
||
173 |