Beispiel #1
0
    def _check_player_ids_need_update(self) -> bool:
        """
        Helper function to check if player ids exist.
        If it does, player ids need to be updated only if ``year`` does
        not match THIS year.
        """
        if self.player_ids_json_path.exists():
            player_ids_loaded = utils.load_from_json(self.player_ids_json_path)

            if player_ids_loaded.get("year") == self.year:
                return False
        return True
Beispiel #2
0
def test_write_to_and_load_from(tmp_path):
    # Setup
    tmp_file_path = tmp_path.joinpath("test.json")
    json_data = {"test": "test", "test2": "test2"}

    # Exercise
    assert tmp_file_path.exists() is False  # non-existent prior to write
    utils.write_to_json(json_data, tmp_file_path)
    result = utils.load_from_json(tmp_file_path)

    # Verify
    assert tmp_file_path.exists() is True
    assert result == json_data
Beispiel #3
0
def test_load_from_json(tmp_path):
    # Setup
    tmp_file_path = tmp_path.joinpath("test.json")
    json_data = {"test": "test", "test2": "test2"}

    with open(tmp_file_path, "w") as written_file:
        json.dump(json_data, written_file)

    # Exercise
    assert tmp_file_path.exists() is True  # existent prior to load
    result = utils.load_from_json(tmp_file_path)

    # Verify
    assert result == json_data
Beispiel #4
0
def test_load_stat_ids():
    # Setup
    file_loc = Path(__file__)
    stat_ids_json_path = file_loc.parent.parent.joinpath(
        "assets/stat_ids.json")

    # Exercise
    result = utils.load_from_json(stat_ids_json_path)

    # Verify
    assert len(result) == 91

    for k, v in result.items():
        assert int(k) == v["id"]
        assert list(v.keys()) == ["id", "abbr", "name", "shortName"]
Beispiel #5
0
def test_load_player_ids():
    # Setup
    file_loc = Path(__file__)
    stat_ids_json_path = file_loc.parent.parent.joinpath(
        "assets/player_ids.json")

    # Exercise
    result = utils.load_from_json(stat_ids_json_path)

    # Verify
    assert "year" in result
    for k, v in result.items():
        if k == "year":
            assert isinstance(v, int)
        else:
            assert list(v.keys()) == ["name", "position", "team", "injury"]
Beispiel #6
0
def create_player_pts_df(year: int,
                         week: int,
                         player_pts: Dict[str, Any],
                         savepath: Optional[Path] = None) -> pd.DataFrame:
    """
    Create a DataFrame to house all player projected and actual points.
    The ``player_pts`` argument can be ``projected_player_pts`` or
    ``actual_player_pts``.

    Actual points will need to be pulled multiple times throughout as
    more games are played/completed. Projected points should be pulled
    once and only once.
    """
    # Determine projected ('projectedStats') or actual points ('stats').
    stats_type = _get_player_pts_stat_type(player_pts)

    if stats_type == "projectedStats":
        prefix = "PROJ_"

        # Projected points should always be saved (pulled only once)
        if savepath is None:
            raise ValueError(
                "When creating a projected player points dataframe, " +
                "``savepath`` must be specified.")

    # _get_player_pts_stat_type handles check and raises error if not
    # projectedStats or stats
    else:
        prefix = "ACTUAL_"

    index = []
    points = []

    for pid, player_pts_dict in player_pts.items():
        points_dict = _unpack_player_pts(year, week, player_pts_dict)
        index.append(pid)
        points.append(points_dict)

    player_pts_df = pd.DataFrame(points, index=index)

    # Get definition of each point attribute
    stat_ids_json_path = Path("assets/stat_ids.json")
    stat_ids_dict = utils.load_from_json(stat_ids_json_path)
    stat_defns = {
        k: v["name"].replace(" ", "_")
        for k, v in stat_ids_dict.items()
    }
    player_pts_df = player_pts_df.rename(columns=stat_defns)

    player_pts_df = player_pts_df.add_prefix(prefix)
    player_pts_df = player_pts_df.reset_index().rename(
        columns={"index": "Player"})

    # Get definition of each player team and name based on player id
    player_ids_json_path = Path("assets/player_ids.json")
    player_ids = utils.load_from_json(player_ids_json_path)
    team = player_pts_df["Player"].apply(lambda x: player_ids[x]["team"])
    player_pts_df.insert(1, "Team", team)

    player_defns = {k: v["name"] for k, v in player_ids.items() if k != "year"}
    name = player_pts_df["Player"].apply(lambda x: player_defns[x])
    player_pts_df["Player"] = name

    # Make pts col the third column for easy access
    pts_col = player_pts_df.filter(regex=f"{prefix}pts")
    pts_col_name = f"{prefix}pts"
    player_pts_df = player_pts_df.drop(pts_col_name, axis=1)
    player_pts_df.insert(2, pts_col_name, pts_col)

    # Convert all col types to non-string as strings come from scrape
    col_types = {}
    for c in player_pts_df.columns:
        if c in ("Player", "Team"):
            col_types[c] = "object"
        else:
            col_types[c] = "float64"

    player_pts_df = player_pts_df.astype(col_types)
    player_pts_df = player_pts_df.fillna(0.0)

    # Write projected players to csv so only done once
    if stats_type == "projectedStats":

        print(f"\tWriting projected player stats to {savepath}...")
        player_pts_df.to_csv(savepath)

    return player_pts_df