Как улучшить асинхронный постоянно работающий python код?
Всем здравствуйте, я пишу телеграм ботов на python и библиотеке aiogram, перехожу на PostgreSQL при помощи библиотеки asyncpg. Так же добавил в свой код классы, которыми никогда до этого не пользовался, подскажите пожалуйста что можно улучшить в моём коде?
import asyncpg
from config import *
from aiogram.dispatcher import Dispatcher
from aiogram.utils import executor
from aiogram import Bot, types
import config
bot = Bot(config.bot_token)
dp = Dispatcher(bot)
class DataBase:
"""Класс для управления базой данных"""
async def get_user_info(self, user_id):
connection = await asyncpg.connect(host=host, user=user, password=password, database=db_name, port=port)
result = await connection.fetch('''
select * from users where t_id = $1
''', user_id)
await connection.close()
return result[0]
async def add_user(self, user_id, user_name):
connection = await asyncpg.connect(host=host, user=user, password=password, database=db_name, port=port)
await connection.fetch('''
insert into users(t_id, username) values($1, $2)
''', user_id, user_name)
await connection.close()
@dp.message_handler(commands=['start'], state='*') # Хендлер
async def start_message(message: types.Message):
db = DataBase()
user_info = await db.get_user_info(user_id=message.from_user.id)
if len(user_info):
print(user_info)
text = f'''
{user_info['t_id']}
{user_info['username']}
'''
await bot.send_message(message.chat.id, text)
else:
await db.add_user(user_id=message.from_user.id, user_name=message.from_user.first_name)
await bot.send_message(message.chat.id, 'Регистрация успешна!')
if __name__ == '__main__':
executor.start_polling(dp, skip_updates=True)
Хочу побольше узнать как можно задействовать классы в работе с БД и про библиотеку asyncpg ( потому что до этого я использовал sqlite3, поэтому тут мне немного сложно разобраться )