Exemple #1
0
def manual_game_analysis(league_data, predictions):
    """
    Takes the league_data dictionary and current predictions.
    Asks the user to select the home and away teams from the available
    teams.
    Provides a comparison.
    Returns the prediction as a list: [homeTeam, predictedHomeScore, awayTeam, predictedAwayScore]
    """
    today = datetime.today()
    team_list = []
    selection1 = ""
    selection2 = ""
    team_list_display = []
    if league_data == {}:
        print(
            "You can't run manual game analysis until you have selected the appropriate league(s)."
        )
        print("Please select a league or import a JSON file first.")
        input("\nPress enter to continue")
        error = "No leagues loaded"  # Response advising why the function failed.
        return error

    # Build the basic team list (this must be kept for later use)
    for team in list_teams(league_data):
        team_list.append(team)

    # Build a list of teams with their option numbers ready for display
    for team in team_list:
        team_list_display.append(str(team_list.index(team) + 1) + " " + team)

    # Display the newly generated list
    cf.custom_pretty_print(team_list_display, 3)

    # while homeTeam not in teamList or awayTeam not in teamList:

    while not cf.valid_input(selection1, range(1, len(team_list) + 1)):
        print("\nSelect home team from the above list:", end=" ")
        selection1 = input()
    while not cf.valid_input(selection2, range(1, len(team_list) + 1)):
        print("\nSelect away team from the above list:", end=" ")
        selection2 = input()

    home_team = team_list[int(selection1) - 1]
    away_team = team_list[int(selection2) - 1]
    home_team_league, away_team_league = get_league(home_team, away_team,
                                                    league_data)

    if home_team_league == away_team_league:
        league = home_team_league
    else:
        league = "(mixed leagues)"

    print(home_team + " vs " + away_team)

    comparison = compare(home_team, away_team, league_data)
    print("\n\nComparison")
    print("==========")
    print("\nPositive numbers indicate Home team statistic is higher."
          " \nNegative numbers indicate Away team statistic is higher.\n")
    comparison_index_count = 0

    print("Home / Away Game Statistical Differences")
    print("========================================")

    for stat in ["Played", "Won", "Drew", "Lost", "For", "Against", "Points"]:
        print(stat, comparison[0][comparison_index_count], end=" ")
        comparison_index_count += 1
    print()
    for stat in ["Won per Game", "Drew per Game", "Lost per Game"]:
        print(stat, comparison[0][comparison_index_count], end=" ")
        comparison_index_count += 1
    print()
    for stat in ["For per Game", "Against per Game", "Points per Game"]:
        print(stat, comparison[0][comparison_index_count], end=" ")
        comparison_index_count += 1

    print("\n\nTotal Game Statistical Differences")
    print("==================================")
    comparison_index_count = 0

    for stat in ["Played", "Won", "Drew", "Lost", "For", "Against", "Points"]:
        print(stat, comparison[1][comparison_index_count], end=" ")
        comparison_index_count += 1
    print()
    for stat in ["Won per Game", "Drew per Game", "Lost per Game"]:
        print(stat, comparison[1][comparison_index_count], end=" ")
        comparison_index_count += 1
    print()
    for stat in ["For per Game", "Against per Game", "Points per Game"]:
        print(stat, comparison[1][comparison_index_count], end=" ")
        comparison_index_count += 1

    home_team_max_goals = league_data[home_team_league][home_team]["Home"][
        "For per Game"] * 2.5
    away_team_max_goals = league_data[away_team_league][away_team]["Away"][
        "For per Game"] * 2.5

    home_team_goals = int(
        (league_data[home_team_league][home_team]["Home"]["For per Game"] *
         1.25) *
        league_data[away_team_league][away_team]["Away"]["Against per Game"])
    away_team_goals = int(
        (league_data[away_team_league][away_team]["Away"]["For per Game"] *
         1.25) *
        league_data[home_team_league][home_team]["Home"]["Against per Game"])

    if home_team_goals > home_team_max_goals:
        home_team_goals = int(home_team_max_goals)

    if away_team_goals > away_team_max_goals:
        away_team_goals = int(away_team_max_goals)

    prediction_goal_separation = abs(home_team_goals - away_team_goals)
    total_goals = home_team_goals + away_team_goals

    if home_team_goals > 0 and away_team_goals > 0:
        both_to_score = "Yes"
    else:
        both_to_score = "No"

    prediction_name = "Home_home_F_A_vs_Away_away_F_A"
    prediction_description = "home_team_goals = int((home_team_avg_gpg_f * 1.25) * (away_team_avg_gpg_a) : away_team_goals = int((away_team_avg_gpg_f * 1.25) * (home_team_avg_gpg_a))"

    # Save current prediction as a list item
    prediction = {
        "League": league,
        "Date and time": "Manual entry: ",
        "Prediction type": prediction_name,
        "Home team": home_team,
        "Home team prediction": home_team_goals,
        "Away team": away_team,
        "Away team prediction": away_team_goals,
        "Total goalsexpected": total_goals,
        "Predicted separation": prediction_goal_separation,
        "Both to score": both_to_score,
        "date_as_dtobject": today
    }

    # Flatten league stats for prediction storage and exporting
    index = 0
    for team in [home_team, away_team]:  # Do for each team
        if team == home_team:  # Used for the prediction keys
            h_a_stat_key = "Home Team"
        elif team == away_team:
            h_a_stat_key = "Away Team"
        league = [home_team_league, away_team_league][index]
        index += 1
        for section in ["Home", "Away",
                        "Total"]:  # Go through each set of stats
            for stat in league_data[league][team][
                    section]:  # Add each stat and a descriptive key to the prediction dictionary
                prediction[h_a_stat_key + " " + section + " " +
                           stat] = league_data[league][team][section][stat]

    prediction["Description"] = prediction_description

    print(
        "\n\nPredicted outcome: " + home_team + " " + str(home_team_goals) +
        " " + away_team + " " + str(away_team_goals) +
        "\n\nFull analysis details can be viewed be exporting the predictions to a file via the 'Reports' menu."
    )

    # If the prediction is not already in the predictions list, add it.
    if prediction not in predictions:
        predictions.append(prediction)

    return predictions
Exemple #2
0
 def test_valid_input2(self):
     self.assertIs(valid_input('eggs', ['spam', 'snakes', 'r/programming']),
                   False)
Exemple #3
0
 def test_valid_input3(self):
     self.assertIs(valid_input(int('2'), range(1, 10 + 1)), True)
Exemple #4
0
 def test_valid_input(self):
     self.assertIs(valid_input('eggs', ['eggs', 'spam', 'snakes']), True)