renamed function 'i' and 'duplicatestest' for readability
This commit is contained in:
parent
3834385d2f
commit
70fb324923
@ -124,40 +124,43 @@ def filters(db):
|
||||
# Functions: Database Creation
|
||||
#------------------------------------------------+
|
||||
|
||||
def inserttodb(c, q, d):
|
||||
for x in d:
|
||||
# possible to do executemany
|
||||
# but want to be able to catch the problematic rows
|
||||
# as it is iterated through.
|
||||
# and proceed with adding OK rows.
|
||||
try:
|
||||
c.execute(q, x)
|
||||
except sql.ProgrammingError as e:
|
||||
print(e)
|
||||
print(x)
|
||||
|
||||
#------------------------------------------------+
|
||||
|
||||
# insert new data directly into new database file
|
||||
def i(d, s):
|
||||
def insert_to_database(d, s):
|
||||
def insert(c, q, d):
|
||||
for x in d:
|
||||
# possible to do executemany
|
||||
# but want to be able to catch the problematic rows
|
||||
# as it is iterated through.
|
||||
# and proceed with adding OK rows.
|
||||
try:
|
||||
c.execute(q, x)
|
||||
except sql.ProgrammingError as e:
|
||||
print(e)
|
||||
print(x)
|
||||
return
|
||||
|
||||
con = sql.connect(d)
|
||||
with con:
|
||||
csr = con.cursor()
|
||||
try:
|
||||
times, ranks, ids, speed, speed_ids = filters(get_list_from_server_txt(s))
|
||||
times, \
|
||||
ranks, \
|
||||
ids, \
|
||||
speed, \
|
||||
speed_ids = filters(get_list_from_server_txt(s))
|
||||
if times:
|
||||
inserttodb(csr, "INSERT OR REPLACE INTO Cts_times VALUES(?, ?, ?, ?)", times)
|
||||
insert(csr, "INSERT OR REPLACE INTO Cts_times VALUES(?, ?, ?, ?)", times)
|
||||
logging.info('\n'.join(y for y in [str(x) for x in times]))
|
||||
if ranks:
|
||||
inserttodb(csr, "INSERT OR REPLACE INTO Cts_ranks VALUES(?, ?, ?, ?)", ranks)
|
||||
insert(csr, "INSERT OR REPLACE INTO Cts_ranks VALUES(?, ?, ?, ?)", ranks)
|
||||
logging.info('\n'.join(y for y in [str(x) for x in ranks]))
|
||||
if ids:
|
||||
inserttodb(csr, "INSERT OR REPLACE INTO Id2alias VALUES(?, ?, ?)", ids)
|
||||
insert(csr, "INSERT OR REPLACE INTO Id2alias VALUES(?, ?, ?)", ids)
|
||||
logging.info('\n'.join(y for y in [str(x) for x in ids]))
|
||||
if speed:
|
||||
inserttodb(csr, "INSERT OR REPLACE INTO Speed VALUES(?, ?)", speed)
|
||||
insert(csr, "INSERT OR REPLACE INTO Speed VALUES(?, ?)", speed)
|
||||
if speed_ids:
|
||||
inserttodb(csr, "INSERT OR REPLACE INTO Fastest_players VALUES(?, ?)", speed_ids)
|
||||
insert(csr, "INSERT OR REPLACE INTO Fastest_players VALUES(?, ?)", speed_ids)
|
||||
except sql.Error:
|
||||
logging.exception("sql error encountered in function 'i'")
|
||||
if con:
|
||||
@ -165,29 +168,29 @@ def i(d, s):
|
||||
|
||||
# 'insert' new data into a file i.e sql query file
|
||||
def write_query(out_file, data):
|
||||
times, \
|
||||
ranks, \
|
||||
ids, \
|
||||
speed, \
|
||||
speed_ids = filters(get_list_from_server_txt(data))
|
||||
with open(out_file, 'w', encoding='utf-8') as file_handle:
|
||||
times, \
|
||||
ranks, \
|
||||
ids, \
|
||||
speed, \
|
||||
speed_ids = filters(get_list_from_server_txt(data))
|
||||
for t in times:
|
||||
file_handle.write("INSERT OR REPLACE INTO Cts_times VALUES(\'%s\', \'%s\', %s, %s);\n" % tuple(t))
|
||||
for r in ranks:
|
||||
file_handle.write("INSERT OR REPLACE INTO Cts_ranks VALUES(\'%s\', \'%s\', %s, \'%s\');\n" % tuple(r))
|
||||
for i in ids:
|
||||
file_handle.write("INSERT OR REPLACE INTO Id2alias VALUES(\'%s\', \'%s\', \'%s\');\n" % tuple(i))
|
||||
pass
|
||||
return
|
||||
|
||||
# Test whether repeat rows are added.
|
||||
def duplicatestest(d, s):
|
||||
c = sql.connect(d)
|
||||
def check_duplicates(database, data):
|
||||
c = sql.connect(database)
|
||||
p = True
|
||||
with c:
|
||||
cs = c.cursor()
|
||||
try:
|
||||
logging.info("Inserting into database (1/2)")
|
||||
i(d, s)
|
||||
insert_to_database(database, data)
|
||||
logging.info("Querying (1/2)")
|
||||
cs.execute("SELECT * FROM Cts_times")
|
||||
a = cs.fetchall()
|
||||
@ -196,7 +199,7 @@ def duplicatestest(d, s):
|
||||
cs.execute("SELECT * FROM Id2alias")
|
||||
c = cs.fetchall()
|
||||
logging.info("Inserting into database (2/2)")
|
||||
i(d, s)
|
||||
insert_to_database(database, data)
|
||||
logging.info("Querying (2/2)")
|
||||
cs.execute("SELECT * FROM Cts_times")
|
||||
x = cs.fetchall()
|
||||
@ -234,7 +237,7 @@ if __name__ == "__main__":
|
||||
log_file = init_logging()
|
||||
print("Writing log to ", log_file)
|
||||
if args.test:
|
||||
duplicatestest(args.db, args.src)
|
||||
check_duplicates(args.dest, args.src)
|
||||
if args.query:
|
||||
try:
|
||||
write_query(args.dest, args.src)
|
||||
@ -242,4 +245,4 @@ if __name__ == "__main__":
|
||||
traceback.print_exc()
|
||||
print("\n\t Exiting - no file to work with.", file=sys.stderr)
|
||||
else:
|
||||
i(args.dest, args.src)
|
||||
insert_to_database(args.dest, args.src)
|
||||
|
Loading…
Reference in New Issue
Block a user