From cfa79cbbaf42a8f74a2cd4bca4d1d495b4d597f1 Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Tue, 7 May 2024 16:50:38 +0200 Subject: Python code style --- persistence.py | 68 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'persistence.py') diff --git a/persistence.py b/persistence.py index f8d7097..512836e 100644 --- a/persistence.py +++ b/persistence.py @@ -1,5 +1,5 @@ import sqlite3, typing -from Types import * +from app_types import * class UserRepository: @@ -7,29 +7,29 @@ class UserRepository: self.__initialize() def __initialize(self): - cur = self.__getConnection().cursor() + cur = self.__get_connection().cursor() cur.execute("CREATE TABLE IF NOT EXISTS user(chat_id INT, alias TEXT)") cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS chat_id_idx ON user(chat_id)") - def __getConnection(self): + def __get_connection(self): return sqlite3.connect('db.sqlite') - def getUser(self, chat_id): - connection = self.__getConnection() + def get_user(self, chat_id): + connection = self.__get_connection() cur = connection.cursor() query = "select * from user where chat_id = {chat_id}".format(chat_id = chat_id) cur.execute(query) return cur.fetchone() - def getAllUsers(self): - connection = self.__getConnection() + def get_all_users(self): + connection = self.__get_connection() cur = connection.cursor() query = "select * from user" cur.execute(query) return cur.fetchall() - def removeChat(self, chat_id): - connection = self.__getConnection() + def remove_chat(self, chat_id): + connection = self.__get_connection() cur = connection.cursor() query = "DELETE FROM user where chat_id = {chat_id}".format( chat_id = chat_id) @@ -37,9 +37,9 @@ class UserRepository: connection.commit() - def putUser(self, chat_id, alias): - if not self.getUser(chat_id): - connection = self.__getConnection() + def put_user(self, chat_id, alias): + if not self.get_user(chat_id): + connection = self.__get_connection() cur = connection.cursor() query = "INSERT INTO user(chat_id, alias) VALUES({chat_id}, '{text}')".format( chat_id = chat_id, @@ -52,18 +52,18 @@ class StatsRepository: def __init__(self): self.__initialize() - def __getConnection(self): + def __get_connection(self): return sqlite3.connect('db.sqlite') def __initialize(self): - cur = self.__getConnection().cursor() + cur = self.__get_connection().cursor() cur.execute("CREATE TABLE IF NOT EXISTS stats(user_id TEXT, display_name TEXT, level INT, matches_played INT, kills INT, wins INT)") cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS user_id_idx ON stats(user_id)") - async def putStats(self, user: User): + async def put_stats(self, user: User): stats: UserStats = await user.fetch_stats() - connection = self.__getConnection() + connection = self.__get_connection() cur = connection.cursor() query = "INSERT OR REPLACE INTO stats(user_id, display_name, level, matches_played, kills, wins) VALUES('{user_id}', '{display_name}', {level}, {matches_played}, {kills}, {wins})".format( user_id = user.id, @@ -75,39 +75,39 @@ class StatsRepository: cur.execute(query) connection.commit() - def getStats(self) -> typing.List[UserStats]: - connection = self.__getConnection() + def get_stats(self) -> typing.List[UserStats]: + connection = self.__get_connection() cur = connection.cursor() query = "SELECT * FROM stats" cur.execute(query) result = cur.fetchall() - return [self.__mapFromDb(stats) for stats in result] + return [self.__map_from_db(stats) for stats in result] - def __mapFromDb(self, record): - userStats = UserStats() - userStats.user_id = str(record[0]) - userStats.user_display_name = str(record[1]) - userStats.level = int(record[2]) - userStats.matches_played = int(record[3]) - userStats.kills = int(record[4]) - userStats.wins = int(record[5]) - return userStats + def __map_from_db(self, record): + user_stats = UserStats() + user_stats.user_id = str(record[0]) + user_stats.user_display_name = str(record[1]) + user_stats.level = int(record[2]) + user_stats.matches_played = int(record[3]) + user_stats.kills = int(record[4]) + user_stats.wins = int(record[5]) + return user_stats class PresenceRepository: def __init__(self): self.__initialize() - def __getConnection(self): + def __get_connection(self): return sqlite3.connect('db.sqlite') def __initialize(self): - cur = self.__getConnection().cursor() + cur = self.__get_connection().cursor() cur.execute("CREATE TABLE IF NOT EXISTS user_presence(display_name TEXT, last_online INT)") cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS display_name_idx ON user_presence(display_name)") - def getLastUserPresence(self, display_name): - connection = self.__getConnection() + def get_last_user_presence(self, display_name): + connection = self.__get_connection() cur = connection.cursor() query = "select last_online from user_presence where display_name = '{display_name}'".format(display_name = display_name) cur.execute(query) @@ -118,8 +118,8 @@ class PresenceRepository: else: return 0 - def setLastUserPresence(self, display_name: str, last_online: int): - connection = self.__getConnection() + def set_last_user_presence(self, display_name: str, last_online: int): + connection = self.__get_connection() cur = connection.cursor() query = "INSERT OR REPLACE INTO user_presence(display_name, last_online) VALUES('{display_name}', {last_online})".format( display_name = display_name, -- cgit v1.2.3