-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
62 lines (53 loc) · 2.13 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import re
from aiogram.utils.keyboard import InlineKeyboardBuilder
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
def back_button_markup(callback_data: str) -> InlineKeyboardMarkup:
buttons = InlineKeyboardBuilder()
buttons.button(
text="🔙 Назад",
callback_data=callback_data
)
buttons.adjust(1)
return buttons.as_markup()
def back_button(callback_data: str) -> InlineKeyboardButton:
return InlineKeyboardButton(
text="🔙 Назад",
callback_data=callback_data
)
translit_dict = {
'а': 'a', 'б': 'b', 'в': 'v', 'г': 'g', 'д': 'd', 'е': 'e', 'ё': 'jo',
'ж': 'zh', 'з': 'z', 'и': 'i', 'й': 'jj', 'к': 'k', 'л': 'l', 'м': 'm',
'н': 'n', 'о': 'o', 'п': 'p', 'р': 'r', 'с': 's', 'т': 't', 'у': 'u',
'ф': 'f', 'х': 'kh', 'ц': 'c', 'ч': 'ch', 'ш': 'sh', 'щ': 'shh', 'ъ': 'q',
'ы': 'y', 'ь': 'x', 'э': 'je', 'ю': 'yu', 'я': 'ya',
'А': 'A', 'Б': 'B', 'В': 'V', 'Г': 'G', 'Д': 'D', 'Е': 'E', 'Ё': 'Jo',
'Ж': 'Zh', 'З': 'Z', 'И': 'I', 'Й': 'Jj', 'К': 'K', 'Л': 'L', 'М': 'M',
'Н': 'N', 'О': 'O', 'П': 'P', 'Р': 'R', 'С': 'S', 'Т': 'T', 'У': 'U',
'Ф': 'F', 'Х': 'Kh', 'Ц': 'C', 'Ч': 'Ch', 'Ш': 'Sh', 'Щ': 'Shh',
'Ъ': 'Q', 'Ы': 'Y', 'Ь': 'X', 'Э': 'Je', 'Ю': 'Yu', 'Я': 'Ya'
}
reverse_translit_dict = {v: k for k, v in translit_dict.items()}
def encode_rus_to_eng(text):
result = []
for char in text:
result.append(translit_dict.get(char, char))
return ''.join(result)
def decode_eng_to_rus(text):
result = []
i = 0
while i < len(text):
for length in (3, 2, 1):
chunk = text[i:i + length]
if chunk in reverse_translit_dict:
result.append(reverse_translit_dict[chunk])
i += length
break
else:
result.append(text[i])
i += 1
return ''.join(result)
def sort_key(s):
match = re.match(r"([A-Za-z]+)(\d+)([A-Za-z]*)", s)
if match:
return match.group(1), int(match.group(2)), match.group(3)
return s