Python Forum
Code problem - probably easy fix?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Code problem - probably easy fix?
#6
I don't really understand your code all that well, but does is this close to correct?
from datetime import datetime
from dataclasses import dataclass
import pandas as pd


@dataclass(order=True)
class Match:
    """Better than dictionary or lists for organizing data."""
    date: datetime
    player1: str
    player2: str
    score: float


@dataclass(order=True)
class Player:
    name: str
    rating: float = None


def load_matches(filename: str) -> tuple[list[Match], dict[str, Player]]:
    """Return list of match results and player ratings extracted from file."""
    # Collect match information in a list.
    df = pd.read_csv(filename)[["match_date", "player1", "player2", "actual_score"]]
    df["match_date"] = pd.to_datetime(df["match_date"])   # Convert timestamp to datetime
    df.sort_values(by=["match_date"])
    matches = [Match(*row) for index, row in df.iterrows()]

    # Extract initial player ratings from file.  Use first bp_score to
    # compute initial rating.
    df = pd.read_csv(filename)[["pb_date", "pb_player", "pb_score"]]
    df.dropna(subset=["pb_player"], inplace=True)   # Drop matches without bp_player
    df["pb_date"] = pd.to_datetime(df["pb_date"])   # Convert timestamp to datetime
    df.sort_values(by=["pb_date"])
    players = {}
    for index, (date, name, score) in df.iterrows():
        if score >= 1700000:
            rating = 10
        elif score >= 100000:
            rating = 5
        else:
            rating = 1
        if name not in players:
            players[name] = Player(name, rating)

    # Add other players from match data.
    for match in matches:
        if match.player1 not in players:
            players[match.player1] = Player(match.player1, 0)
        if match.player2 not in players:
            players[match.player2] = Player(match.player2, 0)

    return players, matches


def update_rating(match: Match, players: dict[str, Player]) -> None:
    if match.score < 0.5:
        players[match.player1].rating -= 1
        players[match.player2].rating += 1
    elif match.score > 0.5:
        players[match.player1].rating += 1
        players[match.player2].rating -= 1

# This does the first script. Somewhat.
players, matches = load_matches("matches_full.csv")

# This does the second script.  Well, it starts with the initial rating and
# adjusts based on match results.
for match in matches:
    update_rating(match, players)

for player in sorted(players.values(), key=lambda x: x.rating, reverse=True):
    print(player)
I left out the Glicko stuff. I don't understand it. I would make an error if I tried to implement it, and the important thing to demonstrate is how to get the initial rating from your first script and use it in your second script.
Reply


Messages In This Thread
Code problem - probably easy fix? - by colin_dent - Jun-29-2023, 04:19 AM
RE: Code problem - probably easy fix? - by Larz60+ - Jun-29-2023, 02:53 PM
RE: Code problem - probably easy fix? - by deanhystad - Jun-30-2023, 01:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  easy name problem Steinsack 1 1,766 Jun-16-2021, 02:03 PM
Last Post: snippsat
  Problem with very easy code. janekk9002 1 1,835 Dec-10-2020, 12:57 PM
Last Post: buran
  What was my mistake in this Python code (easy)? voltman 4 3,499 Nov-19-2019, 09:58 PM
Last Post: snippsat
  How to start with this easy problem? Fran 8 4,234 Sep-11-2018, 09:04 AM
Last Post: Fran
  Making a Easy Password/code system nmsturcke 4 3,890 Jul-09-2018, 02:50 AM
Last Post: ichabod801
  probably a easy problem for you krheigh 4 4,703 May-12-2017, 06:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020