From d12d7c7ea4f0104990f8cd4d7e8d78d23cf09369 Mon Sep 17 00:00:00 2001 From: lucasace Date: Sun, 18 Oct 2020 10:35:31 +0530 Subject: [PATCH] # This is a combination of 4 commits. # This is the 1st commit message: [CODE] Cleaned pylint warnings for check # The commit message #2 will be skipped: # [FILES] Deleted all matches # The commit message #3 will be skipped: # [FILES] Deleted countries.py # The commit message #4 will be skipped: # [CODE] Cleaned code for pylint warnings --- __pycache__/team.cpython-36.pyc | Bin 7273 -> 0 bytes allmatcheslist.py | 38 ------ check.py | 211 +++++++++++++------------------- data/zip/countries.py | 9 -- main.py | 7 +- test/__init__.py | 7 ++ test/test_check.py | 20 +++ test/test_main.py | 25 ++++ 8 files changed, 143 insertions(+), 174 deletions(-) delete mode 100644 __pycache__/team.cpython-36.pyc delete mode 100644 allmatcheslist.py delete mode 100644 data/zip/countries.py create mode 100644 test/__init__.py create mode 100644 test/test_check.py create mode 100644 test/test_main.py diff --git a/__pycache__/team.cpython-36.pyc b/__pycache__/team.cpython-36.pyc deleted file mode 100644 index 7513a202f145cf7dcb3880e573a12e98fd9d5ec7..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 7273 zcmcIpU2Gi3k)EEvot@pKR{RqwImud)W9`}EpD4u+7{#_^!4hoRF>T8?mmD$L9+9&@ zv&){LNG@jM!%?;s13D1_xXo^5_RY9PS|xK^}s8p4S5c(8yCBg#fv)dS-tl zGO>k=m*}3J>guZMs_N>h;rUX@zVqVF`ahjhl>b#mej3IvpoMRu;|ga@#bJ1=O|_*u zDobISqsuwNG4RxyW-I69OO=qjsKmgvzI zZ{eA{+};i%PhePn)ek&ZyyHWApZu)I6-0!`IWh~3RJf=2tk?j=0;C973bE=r zUgBf?*gdsw;aR@NfQLU`npI-8q0TDxeBIuM5@wZNKDO!wemu^2@B`Hf)~fPh*EedzUuyqdU#4efA4jyZVr|t0UIN%on$I@MEc);Ta&0r!zqQ`S3rP!c)nX(Q)|bKj35)qw)GgY)6wZLegD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kMj=tu#d|_hPot8z{#fq${ys&ci zsvjV!?@qoPY&6|~S8j)u7q^k2x=r8hzMesEhXdGC*~%B>Q&t8wSMc@MuUwmR!_Idk z_;QB1b6>h9WMf+XAH5WTRR5&h=5#YG}2mT6-rj^5b3 zxQN*U+Iv+OPv9X=p-c3eC_x3~HI6RYO>a0Qf#PMFF<0Ga(+!;bVD|^~!ESJg_EQp4 za!3Ws2BjqB9*gMMAsyLgKavSamMh`%Z?@eC=?N8M^14S#jgljbwChZULEqw z&7*@z!fM@#mr&3)U5@&e0?ajhlW`NqyX6s@8PMyF4^GUgKhk%uALPZ&OFdsj8(JG&w*zFRNDRxnUl_kPd*titi~_(MCT-$~fFCeDVk0 z;OLXr&~YNV;j8E#{({3bWQsE!wFuzIOT`&($}#}w<9 zN=JAsnoEP5==_4D`Hw{N^`TPxZuvM=7l-%T!t;_Um37_k9_6QxL-)e@p$1aA(v?}F zUb~Xkf`gkPu_&}s+4&u~q`ZU_W~n+N1I3@wCcX#Z5Co}+t`_kX38%*m(_4UC*)i~~ zs= '2019-01-01'] - -match = {} - -for i in tqdm(os.listdir('cricket/data/zip')): - if i != 'players.csv': - x = pd.read_csv('cricket/data/zip/' + i) - for j in allm['Match-ID']: - if j not in match: - match[j] = [] - x1 = x[x['matchid'] == j] - if not x1.empty: - match[j].append(i) - -deletion = [] -for i in match: - if len(match[i]) < 22: - deletion.append(i) - -for i in deletion: - del match[i] - -for i in match: - deletion.append( - [i, match[i][0], match[i][1], match[i][2], match[i][3], match[i][4], match[i][5], match[i][6], match[i][7], - match[i][8], match[i][9], match[i][10], match[i][10], match[i][11], match[i][12], match[i][13], match[i][14], - match[i][15], match[i][16], match[i][17], match[i][18], match[i][19], match[i][20], match[i][21]]) - x = pd.DataFrame(deletion, - columns=['match', 'player0', 'player1', 'player2', 'player3', 'player4', 'player5', 'player6', - 'player7', 'player8', 'player9', 'player10', 'player10', 'player11', 'player12', - 'player13', 'player14', 'player15', 'player16', 'player17', 'player18', 'player19', - 'player20', 'player21']) - x.to_csv("cricket/data/list") diff --git a/check.py b/check.py index 33fb3899..05d3c416 100644 --- a/check.py +++ b/check.py @@ -6,22 +6,14 @@ import os import warnings import pandas as pd -from tqdm import tqdm -from team import Teams -from team import Predict +from team import Teams, Predict -# Remove all the warnings that show on console -warnings.filterwarnings("ignore") # csv file containing the winning points for all 6 matches winning_points = pd.read_csv("data/Winning points.csv") -values = {} - -# Each ID maps to a specific match later used to calculate corresponding points -ID = ["a", "b", "c", "d", "e", "f"] - +scores = {} # define our clear function def clear(): @@ -48,21 +40,18 @@ def get_my11score_wk(playername, match_id): Returns: wkscore (int): My11 WK score of the player of the given match """ - wk = pd.DataFrame() + wicket_keeper = pd.DataFrame() try: - wk = pd.read_csv("data/wk/" + playername) - except: + wicket_keeper = pd.read_csv("data/wk/" + playername) + except FileNotFoundError: wkscore = 0 - if not wk.empty: - match_id = match_id.replace("@", "/") - match_id = match_id.replace("p_", "p?") - match_id = match_id.strip() - - wk = wk[wk["MATCH_ID"] == match_id] + if not wicket_keeper.empty: + match_id = match_id.replace("@", "/").replace("p_", "p?").strip() + wicket_keeper = wicket_keeper[wicket_keeper["MATCH_ID"] == match_id] - if not wk.empty: - wkscore = float(wk.SCORE.values[0]) / 2 + if not wicket_keeper.empty: + wkscore = float(wicket_keeper.SCORE.values[0]) / 2 else: wkscore = 0 @@ -70,55 +59,55 @@ def get_my11score_wk(playername, match_id): def get_my11score_ball(playername, match_id): - """ - Returns My11 Bowling Score + """Returns My11 Bowling Score - Parameters: - playername (str): Player whose bowling score will be calculated - match_id (str): Match where the players score will be calculated + Parameters: + playername (str): Player whose bowling score will be calculated + match_id (str): Match where the players score will be calculated - Returns: - ballscore (int): My11 bowling score of the player of the given match + Returns: + ballscore (int): My11 bowling score of the player of the given match """ + # Remove all the warnings that show on console + warnings.filterwarnings("ignore") + ball = pd.DataFrame() try: ball = pd.read_csv("data/bowl/" + playername) - except: + except FileNotFoundError: ballscore = 0 if not ball.empty: - match_id = match_id.replace("@", "/") - match_id = match_id.replace("p_", "p?") - match_id = match_id.strip() + match_id = match_id.replace("@", "/").replace("p_", "p?").strip() ball = ball[ball["Match_id"] == match_id] if not ball.empty: - w = 12 * float(ball.Wicket.values[0]) + wicket = 12 * float(ball.Wicket.values[0]) if float(ball.Wicket.values[0]) >= 7: - w += 9 + wicket += 9 elif float(ball.Wicket.values[0]) >= 5: - w += 6 + wicket += 6 elif float(ball.Wicket.values[0]) >= 3: - w += 3 - o = float(ball.Overs.values[0]) - m = 4 * float(ball.Maidens.values[0]) - e = 0 - if o >= 2: + wicket += 3 + over = float(ball.Overs.values[0]) + maiden = 4 * float(ball.Maidens.values[0]) + economy = 0 + if over >= 2: if float(ball.Economy.values[0]) < 3: - e += 3 + economy += 3 elif float(ball.Economy.values[0]) <= 4.49: - e += 2 + economy += 2 elif float(ball.Economy.values[0]) <= 5.99: - e += 1 + economy += 1 elif float(ball.Economy.values[0]) <= 7.49: - e += 0 + economy += 0 elif float(ball.Economy.values[0]) <= 8.99: - e -= 1 + economy -= 1 elif float(ball.Economy.values[0]) > 9: - e -= 2 - ballscore = w + m + e + economy -= 2 + ballscore = wicket + maiden + economy else: ballscore = 0 @@ -158,7 +147,7 @@ def get_my11score_bat(playername, match_id): bat["runs"] = runs if score != "-": - batscore = ((float(runs) * 0.5) + (float(bat["4s"]) * 0.5) + float(bat["6s"])) + batscore = (float(runs) * 0.5) + (float(bat["4s"]) * 0.5) + float(bat["6s"]) elif score == "-": batscore = 0 @@ -172,109 +161,82 @@ def get_my11score_bat(playername, match_id): return batscore -def main(): - for ids in ID: +fielding_points = { + "a": 18, + "b": 30, + "c": 31, + "d": 20, + "e": 11, + "f": 38, +} - # Create an instance of the teams class to use its member functions - t = Teams(ids) - - file_name = t.match - """ - Returns the file name corresponding to the ID 'we' - Format of file: {match}%{matchid}%{date}.csv - """ - - match, match_ID, date = ( - file_name.split("%")[0], - file_name.split("%")[1], - file_name.split("%")[2][:-4], - ) - # Mapping from ID to match name - matchname = t.get_match +def main(): + """Runs the check algorithm""" + for ids in Teams.get_match: - # Assigning the ID to the match name in the dictionary - # matchname[we] = match + # Create an instance of the teams class to use its member functions + team_class = Teams(ids) # Dictionary which maps matches to the score obtained using the recommended team - values[match] = {} + scores[team_class.match.split("%")[0]] = {} # Returns the players recommended by the regression model - captain, vc, wkteam, allteam, ballteam, batteam = t.team() - players = t.player + captain, vcaptain = team_class.team() - values[match][Predict.value] = None + scores[team_class.match.split("%")[0]][Predict.value] = None # Total score obtained from the given team teamtotal = 0 - for player in players: - - # batscore, ballscore, my11score = 0, 0, 0 + for player in team_class.player: # Name of the player - player_name = player[0:player.find("(")] - - player_name = player_name.strip() - for i in tqdm(os.listdir("data/zip/")): - if player_name in i: + for i in os.listdir("data/zip/"): + if player[0 : player.find("(")].strip() in i: player_name = i break - # Returns my11 batting score using my11 scoring scheme - batscore = get_my11score_bat(player_name, match_ID) - - # Returns my11 bowling score using my11 scoring scheme - ballscore = get_my11score_ball(player_name, match_ID) - - # Returns my11 WK score using my11 scoring scheme - wkscore = get_my11score_wk(player_name, match_ID) - # Calculate total score obtained - total = batscore + ballscore + wkscore + total = ( + get_my11score_bat(player_name, team_class.match.split("%")[1]) + + get_my11score_ball(player_name, team_class.match.split("%")[1]) + + get_my11score_wk(player_name, team_class.match.split("%")[1]) + ) if player == captain: - total = total * 2 - elif player == vc: + total *= 2 + elif player == vcaptain: total *= 1.5 - else: - total = total teamtotal += total - if ids == "a": - values[match][Predict.value] = teamtotal + 18 - elif ids == "b": - values[match][Predict.value] = teamtotal + 31 - elif ids == "c": - values[match][Predict.value] = teamtotal + 30 - elif ids == "d": - values[match][Predict.value] = teamtotal + 20 - elif ids == "e": - values[match][Predict.value] = teamtotal + 11 - elif ids == "f": - values[match][Predict.value] = teamtotal + 38 + scores[team_class.match.split("%")[0]][Predict.value] = ( + fielding_points[ids] + teamtotal + ) loss = {} win = {} - for i in range(len(winning_points)): - #print(matchname) - print(i) - print(winning_points.iloc[i,-1]) - match_id = matchname[winning_points.iloc[i, -1]].split("%")[0].strip() - print(match_id) - difference = winning_points.iloc[i, 1] - values[match_id][Predict.value] - win[match_id] = winning_points.iloc[i, 1] - loss[match_id] = difference + for i in range(len(winning_points)): + win[ + team_class.get_match[winning_points.iloc[i, -1]].split("%")[0].strip() + ] = winning_points.iloc[i, 1] + loss[team_class.get_match[winning_points.iloc[i, -1]].split("%")[0].strip()] = ( + winning_points.iloc[i, 1] + - scores[ + team_class.get_match[winning_points.iloc[i, -1]].split("%")[0].strip() + ][Predict.value] + ) - clear() - x = [] - for i in values: - x.append([i, values[i][5], loss[i], win[i], str((100 * (loss[i] / win[i]))) + "%"]) + result = [] + for i in scores: + result.append( + [i, scores[i][5], loss[i], win[i], str((100 * (loss[i] / win[i]))) + "%"] + ) - x = pd.DataFrame( - x, + result = pd.DataFrame( + result, columns=[ "Game", "Real points Score", @@ -284,6 +246,9 @@ def main(): ], ) - print(x.head(6)) + return result -main() +if __name__=="__main__": + clear() + checks = main() + print(checks.head(6)) diff --git a/data/zip/countries.py b/data/zip/countries.py deleted file mode 100644 index 38fa2731..00000000 --- a/data/zip/countries.py +++ /dev/null @@ -1,9 +0,0 @@ -import pandas as pd -from tqdm import tqdm as tqdm -import os -import numpy as np -country =[] -for i in tqdm(os.listdir('.')): - if '(' in i: - country.append(i.split('(')[1]) -print(np.unique(np.array(country))) diff --git a/main.py b/main.py index 041c81fb..347f4f52 100644 --- a/main.py +++ b/main.py @@ -9,12 +9,11 @@ from fastapi.staticfiles import StaticFiles from team import Teams -#pylint: disable=missing-function-docstring -#pylint: disable=global-variable-undefined +# pylint: disable=missing-function-docstring +# pylint: disable=global-variable-undefined app = FastAPI() - templates = Jinja2Templates(directory="templates") app.mount("/static", StaticFiles(directory="static"), name="static") @@ -60,7 +59,7 @@ def result(request: Request): players.append(i[: i.find("\xa0")] + tag_c) captain_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] vcaptain_list = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] - for i,_ in enumerate(players): + for i, _ in enumerate(players): if "(C)" in players[i]: captain_list[i] = "(C)" vcaptain_list[i] = "" diff --git a/test/__init__.py b/test/__init__.py new file mode 100644 index 00000000..18fb9452 --- /dev/null +++ b/test/__init__.py @@ -0,0 +1,7 @@ +""" +Setting paths for testing +""" + +import sys + +sys.path.insert(1, "../") diff --git a/test/test_check.py b/test/test_check.py new file mode 100644 index 00000000..f1b9223e --- /dev/null +++ b/test/test_check.py @@ -0,0 +1,20 @@ +""" +Tests for check.py +""" +import numpy as np +from check import main + + +def test_main(): + """ + Testing main function of check.py + """ + result = main() + assert len(result) == 6 + assert np.array_equal( + result.iloc[:, 1].values, + np.array([245.00, 355.25, 461.50, 416.75, 334.75, 338.75]), + ) + assert np.array_equal( + result.iloc[:, 2].values, np.array([237.25, 185.25, 81, 97.25, 65.75, 172]) + ) diff --git a/test/test_main.py b/test/test_main.py new file mode 100644 index 00000000..54e03822 --- /dev/null +++ b/test/test_main.py @@ -0,0 +1,25 @@ +""" +Testing for main.py +""" +from fastapi.testclient import TestClient +from main import app + +client = TestClient(app) + +# pylint: disable=missing-function-docstring + + +def test_home(): + response = client.get("/") + assert response.status_code == 200 + + +def test_home_post(): + for i in ["a", "b", "c", "d", "e", "f"]: + response = client.post("/", data={"match": i}) + assert response.status_code == 307 + + +def test_result(): + response = client.post("/results") + assert response.status_code == 200