import inspect
my_dog_age = 7
class SmallDog:
pass
class MiddleDog:
pass
class BigDog:
pass
class DogMeta(type):
def __new__(mcs, name, bases, namespace):
dog = SmallDog
if my_dog_age >= 10:
dog = BigDog
elif my_dog_age >= 5:
dog = MiddleDog
return super().__new__(mcs, name, (dog,), namespace)
class Dog(metaclass=DogMeta):
pass
class MyDog(Dog):
pass
print(inspect.getmro(MyDog))
from multiprocessing import Process
class CustomProcess(Process):
def __init__(self):
super().__init__()
def __enter__(self):
return self
def __exit__(self, exc_type, exc_val, exc_tb):
print(exc_type, exc_val, exc_tb)
print("Посмертное действие")
if __name__ == '__main__':
with CustomProcess() as process:
raise Exception("Some Value")
from threading import Thread
from collections import namedtuple
import time
import typing
import random
Message = namedtuple('Message', ['delete_time', 'message_id'])
messages_to_delete: typing.List[Message] = []
def delete_message(message: Message):
print(f'Сообщение {message.message_id=} удалено')
messages_to_delete.remove(message)
def messages_deleter():
while True:
for message in messages_to_delete:
if message.delete_time - time.time() <= 0:
delete_message(message)
time.sleep(1)
if __name__ == '__main__':
thread = Thread(target=messages_deleter)
thread.start()
while True:
delete_time = time.time() + random.randint(1, 5)
messages_to_delete.append(
Message(delete_time=delete_time, message_id=random.randint(1, 1000))
)
time.sleep(1)
for cmd in cmds_before:
await client.send_message(bot_name, cmd)
text = '.реши 1 + 1'
if text.startswith('.реши'):
print(text[5:])
import enum
class SenderType(enum.Enum):
FROM_USER = enum.auto()
FROM_CHAT = enum.auto()
FROM_GROUP = enum.auto()
def get_sender_type(peer_id: int) -> SenderType:
SHIFT_VALUE = 2_000_000_000
if peer_id > SHIFT_VALUE:
return SenderType.FROM_CHAT
elif 0 < peer_id < SHIFT_VALUE:
return SenderType.FROM_USER
else:
return SenderType.FROM_GROUP
if __name__ == '__main__':
test_id = 345343111
if get_sender_type(test_id) == SenderType.FROM_USER:
print('Работает)')