from aiogram import types
from aiogram.dispatcher.filters import Filter, Command
class CommandNotInListFilter(Filter):
def __init__(self, commands: Command):
self.commands = commands
def check(self, message: types.Message) -> bool:
if not message.text.startswith('/'):
return False
return message.text[1:] in self.commands.commands
@dp.message_handler(
CommandNotInListFilter(Command(['start', 'help']))
)
async def handle_unknown_commands(message: types.Message):
await message.reply("Данной команды не существует!")
from itertools import chain
from more_itertools import grouper
data = [[1, 2, 3], [4, 5, 6], [6, 7, 8], [1, 2]]
res = [[el for el in group if el is not None]
for group in grouper(chain.from_iterable(data), 4)]
# [[1, 2, 3, 4], [5, 6, 6, 7], [8, 1, 2]]
def hex_digits_range(from_: int, to: int, zfill_value: int = 20):
for i in range(from_, to + 1):
yield format(int(hex(i), base=16), 'x').zfill(zfill_value)
gen = hex_digits_range(500, 515)
for el in gen:
print(el)
from itertools import groupby
def join_segments(string: str, group_len: int, sep: str = ' '):
groups = groupby(
enumerate(reversed(string)),
lambda el: el[0] // group_len
)
string = sep.join(
''.join(char for i, char in group)
for _, group in groups
)
return string[::-1]
if __name__ == '__main__':
assert join_segments('1605', 4) == '1605'
assert join_segments('9550', 4) == '9550'
assert join_segments('12556430', 4) == '1255 6430'
assert join_segments('1', 4) == '1'
assert join_segments('-1234', 4) == '- 1234'
assert join_segments('1234', 1) == '1 2 3 4'
assert join_segments('1234', 10) == '1234'
assert join_segments('', 10) == ''