def __init__(self, conn: sqlite3.Connection, logs: logging, schema_file: TextIO): self.conn = conn self.logs = logs self.schema = schema_file.read() try: conn.executescript(self.schema) logs.info("Database initialized from schema.sql") except sqlite3.Error: logs.error("Failed creating database from schema.sql")
def verify(self, logger: logging): sum_collected = float(0) sum_net = float(0) for player in self.players: sum_net += float(player.net) sum_collected += float(player.collected_amount) if float(sum_collected) - (float(self.pot) - float(self.rake)) > 0.001: logger.error('collected != pot+rake for hand: ' + self.hand_id) return False if float(sum_net) + float(self.rake) > 0.001: logger.error('net != rake for hand: ' + self.hand_id) return False return True
def download_file(file_url: str, file_name: str, log: logging) -> str: """ Download file_url and move it to file_name, do nothing if file_name already exists. :param log: log to be use. :param file_url: file url to be download :param file_name: file name where the data will be downloaded :return: name of the file if the file can be download. """ if os.path.isfile(file_name): return file_name if log is not None: log = logging remaining_download_tries = REMAINING_DOWNLOAD_TRIES downloaded_file = None while remaining_download_tries > 0: try: downloaded_file, error_code = request.urlretrieve( file_url, file_name) log.debug("File downloaded -- " + downloaded_file) if downloaded_file.endswith('.gz'): extracted_file = downloaded_file.replace('.gz', '') with open(extracted_file, 'w') as outfile: outfile.write( gzip.decompress(open(downloaded_file, 'rb').read()).decode('utf-8')) os.remove(downloaded_file) downloaded_file = extracted_file log.debug("File extracted-- " + downloaded_file) break except ( HTTPError, URLError, ContentTooShortError, ) as error: logging.error( "Error downloading -- Incorrect URL or file not found: " + file_url + " on trial no: " + str(REMAINING_DOWNLOAD_TRIES - remaining_download_tries)) log.error("Error code: " + str(error)) remaining_download_tries = remaining_download_tries - 1 downloaded_file = None continue except Exception as error: remaining_download_tries = remaining_download_tries - 1 log.error("Error code: " + str(error)) downloaded_file = None return downloaded_file
def download_file(file_url: str, file_name: str, log: logging) -> str: """ Download file_url and move it to file_name, do nothing if file_name already exists. :param log: log to be use. :param file_url: file url to be download :param file_name: file name where the data will be downloaded :return: name of the file if the file can be download. """ if os.path.isfile(file_name): return file_name if log is not None: log = logging remaining_download_tries = REMAINING_DOWNLOAD_TRIES downloaded_file = None while remaining_download_tries > 0: try: downloaded_file, error_code = request.urlretrieve( file_url, file_name) log.debug("File downloaded -- " + downloaded_file) break except (HTTPError, URLError, ContentTooShortError) as error: logging.error( "Error downloading -- Incorrect URL or file not found: " + file_url + " on trial no: " + str(REMAINING_DOWNLOAD_TRIES - remaining_download_tries)) log.error("Error code: " + str(error)) remaining_download_tries = remaining_download_tries - 1 downloaded_file = None continue except Exception as error: remaining_download_tries = remaining_download_tries - 1 log.error("Error code: " + str(error)) downloaded_file = None return downloaded_file