Python
- 5 ответов
- 0 вопросов
3
Вклад в тег
from threading import Lock
class ResourceContainer:
_instance = None
_lock = Lock()
def __new__(cls):
with cls._lock:
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
self._db_connection = None
self._message_broker_connection = None
self._config = {}
def get_db_connection(self):
return self._db_connection
def set_db_connection(self, connection):
self._db_connection = connection
def get_message_broker_connection(self):
return self._message_broker_connection
def set_message_broker_connection(self, connection):
self._message_broker_connection = connection
def get_config(self):
return self._config
def set_config(self, config):
self._config = config
def foo():
container = ResourceContainer()
db_conn = container.get_db_connection()
# use db_conn
def bar():
container = ResourceContainer()
config = container.get_config()
# use config
def longest_substring(string, substring_len):
for i in range(len(a) - substring_len + 1):
substring = list(a[i:i + substring_len])
if sorted(substring) == sorted(set(substring)):
return substring
return longest_substring(string, substring_len - 1)
@dp.message_handler(content_types=ContentType.PHOTO)
async def process_photo(message: types.Message):
# Получаем список фотографий в сообщении
photos = message.photo
# Перебираем фотографии и обрабатываем их
for photo in photos:
# Скачиваем фотографию
await photo.download()
# Обрабатываем фотографию (например, сохраняем ее в базу данных)
process_photo(photo.file)