Esempio n. 1
0
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
Esempio n. 2
0
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