import time import json class Person: __ACCOUNTS_FILE = "accounts.json" __DOMAIN = "godnessli.ru" def __init__(self, account: str, admin = False): if self.__check_person(account): self.__read_person(account) else: self.__create_person(account, admin) def get_account(self) -> str: return self.__account def get_unknown_answers(self) -> int: return self.__unknown_count def clear_answers(self): self.__unknown_count = 0 def incr_account(self): self.__unknown_count += 1 def __check_person(self, account: str) -> bool: accounts = None try: accounts = self.__get_accounts() except FileNotFoundError: return False if accounts != None: for acc in accounts: if acc == account: return True return False def __read_person(self, account: str): accounts = self.__get_accounts() acc = accounts[account] self.__nick_name = acc["nick"] self.__admin = acc["admin"] self.__unknown_counter = acc["u_count"] self.__whether_counter = acc["w_count"] self.__ideas = acc["ideas"] def __create_person(self, account: str, admin: bool): accounts = None try: accounts = self.__get_accounts() except FileNotFoundError: pass with open(self.__ACCOUNTS_FILE, 'w', encoding = 'utf-8') as p_file: begin = account.find("@") end = account.find(self.__DOMAIN) if accounts != None: accs = dict(accounts) else: self.__create_file(account, admin) new_acc = { account: { "nick" : account[begin:end], "admin" : admin, "u_count" : 0, "w_count" : 0, "ideas" : [] } } if accounts != None: accs.update(new_acc) p_file.write(accs) self.__nick = new_acc["nick"] self.__admin = new_acc["admin"] self.__unknown_counter = new_acc["u_count"] self.__whether_counter = new_acc["w_count"] self.__ideas = new_acc["ideas"] def __get_accounts(self) -> dict: with open(self.__ACCOUNTS_FILE, 'r', encoding = 'utf-8') as p_file: accounts = None try: accounts = json.load(p_file) except json.decoder.JSONDecodeError as e: print(e) p_file.close() return accounts def __create_file(self, name: str, admin: bool): with open(self.__ACCOUNTS_FILE, 'w', encoding = 'utf-8') as p_file: account = { name : { "nick" : self.__parse_name(name), "admin" : False, "u_count" : 0, "w_count" : 0, "ideas" : [] } } p_file.write(account) def __parse_name(self, name: str) -> str: begin = name.find("@") end = name.find(self.__DOMAIN) return name[begin:end] tema = Person("@tema:godnessli")