def import_config(args): """ Imports the config data of a csv file into the config table. :param args: A list with arguments from the argument parser :type args: namespace """ Config.query().truncate() print "Importing config from", args.file try: csvfile, reader, hasHeader = utils.get_csv(args.file) line = 0 for row in reader: if line != 0 or (line == 0 and not hasHeader): entry = Config(key=row[0], value=row[1]) entry.save(commit=False) line = line + 1 Config.commit() csvfile.close() print "Imported", line - 1, "config entries." except csv.Error: print "Error importing %s in line %d" % (args.file, line) except IOError: print "No such file: %s" % args.file
def import_results(args): """ Imports the match data of a csv file into the result table. :param args: A list with arguments from the argument parser :type args: namespace """ print "Importing results from", args.file try: csvfile, reader, hasHeader = utils.get_csv(args.file) line = 0 if hasHeader: print "\tFirst line of csv file is ignored. It seems to be a " \ "header row.\n" # nickname --> player players = {} for row in reader: if line != 0 or (line == 0 and not hasHeader): if row[1] is row[2]: continue player1 = players.get(row[1], None) player2 = players.get(row[2], None) if player1 is None: player1, created = utils.add_player(row[1], commit=False) players[player1.nickname.value] = player1 if player2 is None: player2, created = utils.add_player(row[2], commit=False) players[player2.nickname.value] = player2 dbRow = Match1on1(player1=player1.player_id.value, player2=player2.player_id.value, outcome=row[3], date=row[0]) dbRow.save(commit=False) if line % 100 == 0: sys.stdout.write("\r" + "Imported %d entries..." % line) sys.stdout.flush() line = line + 1 Rank_Elo.commit() Rank_Glicko.commit() Match1on1.commit() csvfile.close() print "\rImported %d entries." % (line - (1 if hasHeader else 0)) except csv.Error: print "Error importing %s in line %d" % (args.file, line) except IOError: print "No such file: %s" % args.file
def predict(args): """ Reads match constellations from an input file and writes the results to a specified output file. :param args: A list with arguments from the argument parser :type args: namespace """ if os.path.abspath(args.ifile) == os.path.abspath(args.ofile): print "You tried to overwrite your input with your output file." print "Aborted." return if args.algorithm == "glicko": print "Not implemented yet" return try: modes = {True: "incremental", False: "non-incremental"} print "Predicting the matches in %s mode" % modes[args.incremental] print "Open %s and write into %s..." % (args.ifile, args.ofile) csvfile, reader, hasHeader = utils.get_csv(args.ifile) ofile = open(args.ofile, 'w') line = 0 if hasHeader: print "\tFirst line of csv file is ignored. It seems to be a " \ "header row.\n" # constants conf = utils.get_config(args) k = conf["elo.chess.k"] func = conf["elo.chess.function"] # Query all ratings and store it in a dictionary. This is done for # faster access and on-the-fly calculation of new elo values ratings = Rank_Elo.query().all() rdict = {} for r in ratings: player = Player.query().get(player_id=r.player_id.value) rdict[player.nickname.value] = r for row in reader: if line != 0 or (line == 0 and not hasHeader): ratings = (rdict.get(row[1], None), rdict.get(row[2], None)) if ratings[0] is None or ratings[1] is None: continue value1 = ratings[0].value.value value2 = ratings[1].value.value if (value1 > value2): outcome = 1 elif (value1 < value2): outcome = 0 else: outcome = 0.5 if args.incremental: result = elo.elo1on1(value1, value2, outcome, k, func) ratings[0].value = result[0] ratings[1].value = result[1] ofile.write("%s,%s,%s,%s\n" % (row[0], row[1], row[2], str(outcome))) elif line == 0 and hasHeader: ofile.write('"%s","%s","%s","Statistically most possible ' \ 'outcome"\n' % (row[0], row[1], row[2])) if line % 13 == 0: sys.stdout.write("\rWrote %d entries to the out file" % line) sys.stdout.flush() line = line + 1 print "\rWrote %d entries to the out file" % (line - (1 if hasHeader else 0)) csvfile.close() ofile.close() except csv.Error: print "Error importing %s in line %d" % (args.ifile, line) except IOError: print "One file is missing. Either %s or %s" % (args.ifile, args.ofile)
def predict(args): """ Reads match constellations from an input file and writes the results to a specified output file. :param args: A list with arguments from the argument parser :type args: namespace """ if os.path.abspath(args.ifile) == os.path.abspath(args.ofile): print "You tried to overwrite your input with your output file." print "Aborted." return if args.algorithm == "glicko": print "Not implemented yet" return try: modes = {True: "incremental", False: "non-incremental"} print "Predicting the matches in %s mode" % modes[args.incremental] print "Open %s and write into %s..." % (args.ifile, args.ofile) csvfile, reader, hasHeader = utils.get_csv(args.ifile) ofile = open(args.ofile, 'w') line = 0 if hasHeader: print "\tFirst line of csv file is ignored. It seems to be a " \ "header row.\n" # constants conf = utils.get_config(args) k = conf["elo.chess.k"] func = conf["elo.chess.function"] # Query all ratings and store it in a dictionary. This is done for # faster access and on-the-fly calculation of new elo values ratings = Rank_Elo.query().all() rdict = {} for r in ratings: player = Player.query().get(player_id=r.player_id.value) rdict[player.nickname.value] = r for row in reader: if line != 0 or (line == 0 and not hasHeader): ratings = (rdict.get(row[1], None), rdict.get(row[2], None)) if ratings[0] is None or ratings[1] is None: continue value1 = ratings[0].value.value value2 = ratings[1].value.value if (value1 > value2): outcome = 1 elif (value1 < value2): outcome = 0 else: outcome = 0.5 if args.incremental: result = elo.elo1on1(value1, value2, outcome, k, func) ratings[0].value = result[0] ratings[1].value = result[1] ofile.write("%s,%s,%s,%s\n" % (row[0], row[1], row[2], str(outcome))) elif line == 0 and hasHeader: ofile.write('"%s","%s","%s","Statistically most possible ' \ 'outcome"\n' % (row[0], row[1], row[2])) if line % 13 == 0: sys.stdout.write("\rWrote %d entries to the out file" % line) sys.stdout.flush() line = line + 1 print "\rWrote %d entries to the out file" % ( line - (1 if hasHeader else 0)) csvfile.close() ofile.close() except csv.Error: print "Error importing %s in line %d" % (args.ifile, line) except IOError: print "One file is missing. Either %s or %s" % (args.ifile, args.ofile)