diff --git a/.github/workflows/pytest.yml b/.github/workflows/pytest.yml new file mode 100644 index 0000000..6a4c42d --- /dev/null +++ b/.github/workflows/pytest.yml @@ -0,0 +1,24 @@ +name: Run Pytest + +on: + pull_request: + branches: [ main ] + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: '3.12' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Run pytest + run: pytest tests/ diff --git a/.gitignore b/.gitignore index 6764e14..f63e7fd 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ *.csv .idea/ +__pycache__/ diff --git a/draftkings.csv b/draftkings.csv new file mode 100644 index 0000000..45b6dde --- /dev/null +++ b/draftkings.csv @@ -0,0 +1,75 @@ +Player,DK Position,DK Salary,DK Projection +"Alvin and the Chipmunks","RB","7800","22.9" +"Matt Busche","RB","7800","22.9" +"Chase from Paw Patrol","RB","5900","17.9" +"Austin Powers","RB","5300","16.2" +"Kyren Williams","RB","7000","21.1" +"CeeDee Lamb","WR","8800","22.6" +"Chris Olave","WR","6100","17" +"Drake London","WR","6700","20" +"James Conner","RB","7600","17.5" +"Mike Gesicki","TE","3100","10.6" +"Ja'Marr Chase","WR","8600","21.3" +"Titans ","DST","3200","8.7" +"De'Von Achane","RB","6400","17.5" +"Bijan Robinson","RB","7400","18.8" +"Tyreek Hill","WR","7300","18.6" +"Geno Smith","QB","4800","13.9" +"Cardinals ","DST","2600","7.1" +"Justin Herbert","QB","5300","17.2" +"Malik Nabers","WR","7500","18.6" +"Kenneth Walker III","RB","7300","18.1" +"Jameis Winston","QB","5400","17.4" +"Gardner Minshew II","QB","4500","14.7" +"Saquon Barkley","RB","8200","19.9" +"David Njoku","TE","5500","14.4" +"Jakobi Meyers","WR","5300","14" +"Giants ","DST","2300","6.1" +"D'Andre Swift","RB","6400","16.1" +"Daniel Jones","QB","5200","16.6" +"LAC ","DST","3300","7.8" +"Trey McBride","TE","5800","14.7" +"Mason Rudolph","QB","4600","14.5" +"Eagles ","DST","3100","7.4" +"Ravens ","DST","3500","8" +"Dak Prescott","QB","6300","19.5" +"Saints ","DST","3800","8.5" +"Commanders ","DST","3400","7.8" +"Bryce Young","QB","4400","13.6" +"Cedric Tillman","WR","4300","11.5" +"James Cook","RB","7200","17" +"Jaxon Smith-Njigba","WR","6200","15" +"LAR ","DST","2800","6.3" +"Chuba Hubbard","RB","6500","15.5" +"Tyler Lockett","WR","5600","13.7" +"Lamar Jackson","QB","8000","24.3" +"Dalton Kincaid","TE","5000","12.5" +"Bo Nix","QB","5900","17.9" +"Alexander Mattison","RB","5700","13.9" +"Tony Pollard","RB","6800","16.1" +"BUF ","DST","3000","6.5" +"CIN ","DST","3700","7.9" +"Ladd McConkey","WR","5600","13.6" +"Tyrone Tracy Jr.","RB","5500","13.4" +"Darnell Mooney","WR","6000","14.3" +"Josh Allen","QB","7700","23.1" +"Matthew Stafford","QB","5700","17" +"Taysom Hill","TE","3800","9.9" +"CHI ","DST","2800","6" +"CLE ","DST","2900","6.1" +"LV ","DST","2400","5.2" +"Kyler Murray","QB","6600","19.7" +"Derrick Henry","RB","8300","18.7" +"Evan Engram","TE","5300","12.8" +"Brock Bowers","TE","6000","14.2" +"Calvin Ridley","WR","5700","13.6" +"Jayden Daniels","QB","7500","22.4" +"Cooper Kupp","WR","7700","17.4" +"Jalen Hurts","QB","7800","23.3" +"Joe Burrow","QB","6900","20.4" +"Jahmyr Gibbs","RB","7600","17.2" +"NE ","DST","3000","6.1" +"SEA ","DST","2600","5.3" +"Kirk Cousins","QB","6400","18.8" +"J.K. Dobbins","RB","7000","15.9" +"AJ Barner","TE","2600","7.2" diff --git a/main.py b/main.py index d72e0d5..b15b46e 100644 --- a/main.py +++ b/main.py @@ -16,25 +16,24 @@ "two_te": {"QB": 1, "RB": 2, "WR": 3, "TE": 2, "DST": 1} } -players = pd.read_csv("draftkings.csv", usecols=[PLAYER, POSITION, PROJECTION, SALARY]) -# trim whitespace from columns -players = players.apply(lambda x: x.str.strip() if x.dtype == "object" else x) +def calculate_lineups(lineup_type, output_file, csv_file): + players = pd.read_csv(csv_file, usecols=[PLAYER, POSITION, PROJECTION, SALARY]) + # trim whitespace from columns + players = players.apply(lambda x: x.str.strip() if x.dtype == "object" else x) + + # Group players by position + available_players = players.groupby([POSITION, PLAYER, PROJECTION, SALARY]).size().reset_index() + + # Create salary and points dicts by position + salaries = { + pos: available_players[available_players[POSITION] == pos].set_index(PLAYER)[SALARY].to_dict() + for pos in available_players[POSITION].unique() + } + points = { + pos: available_players[available_players[POSITION] == pos].set_index(PLAYER)[PROJECTION].to_dict() + for pos in available_players[POSITION].unique() + } - -# Group players by position -available_players = players.groupby([POSITION, PLAYER, PROJECTION, SALARY]).size().reset_index() - -# Create salary and points dicts by position -salaries = { - pos: available_players[available_players[POSITION] == pos].set_index(PLAYER)[SALARY].to_dict() - for pos in available_players[POSITION].unique() -} -points = { - pos: available_players[available_players[POSITION] == pos].set_index(PLAYER)[PROJECTION].to_dict() - for pos in available_players[POSITION].unique() -} - -def calculate_lineups(lineup_type, output_file): lineup_results = [] previous_lineups = [] @@ -86,7 +85,14 @@ def calculate_lineups(lineup_type, output_file): pd.DataFrame(lineup_results).to_csv(output_file + ".csv", index=False, header=False) -for name, config in lineup_configs.items(): - calculate_lineups(config, name) -print("Lineup files created") +def generate_lineup_files(csv_file): + for name, config in lineup_configs.items(): + calculate_lineups(config, name, csv_file) + + print("Lineup files created") + + +if __name__ == "__main__": + csv_file = "draftkings.csv" # You can change this to any CSV file name + generate_lineup_files(csv_file) diff --git a/requirements.txt b/requirements.txt index 8845169..456354e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,2 +1,3 @@ pandas==2.2.3 pulp==2.9.0 +pytest==8.3.3 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_things.py b/tests/test_things.py new file mode 100644 index 0000000..5069f9e --- /dev/null +++ b/tests/test_things.py @@ -0,0 +1,100 @@ +import os + +import pytest + +from main import generate_lineup_files + + +@pytest.fixture(autouse=True) +def generate_files(): + generate_lineup_files('./draftkings.csv') + + +def test_generate_lineup_files(): + assert os.path.exists('four_wr.csv') + assert os.path.exists('three_rb.csv') + assert os.path.exists('two_te.csv') + + +def test_four_wr(): + with open('four_wr.csv', 'r') as f: + four_wr = [f.readline().strip() for _ in range(10)] + + # because of the duplicate scores, the results are not always returned in the same order + conditions = [ + len(four_wr) == 10, + four_wr[0][-5:] == "145.5", + four_wr[1][-5:] == "145.3", + four_wr[2][-5:] == "145.3", + four_wr[3][-5:] == "145.3", + four_wr[4][-5:] == "145.3", + four_wr[5][-5:] == "145.3", + four_wr[6][-5:] == "145.2", + four_wr[7][-5:] == "145.2", + four_wr[8][-5:] == "145.2", + four_wr[9][-5:] == "145.2", + ] + + assert all(conditions) + + +def test_three_rb(): + with open('three_rb.csv', 'r') as f: + three_rb = [f.readline().strip() for _ in range(10)] + + conditions = [ + len(three_rb) == 10, + three_rb[ + 0] == "1,DST,Cardinals,2600,7.1,QB,Lamar Jackson,8000,24.3,RB,Austin Powers,5300,16.2,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,148.2", + three_rb[ + 1] == "2,DST,Cardinals,2600,7.1,QB,Jameis Winston,5400,17.4,RB,Alvin and the Chipmunks,7800,22.9,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,148.0", + three_rb[ + 2] == "3,DST,Cardinals,2600,7.1,QB,Jameis Winston,5400,17.4,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,148.0", + three_rb[ + 3] == "4,DST,Titans,3200,8.7,QB,Jameis Winston,5400,17.4,RB,Alvin and the Chipmunks,7800,22.9,RB,Austin Powers,5300,16.2,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,147.9", + three_rb[ + 4] == "5,DST,Titans,3200,8.7,QB,Jameis Winston,5400,17.4,RB,Austin Powers,5300,16.2,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49900,147.9", + three_rb[ + 5] == "6,DST,Cardinals,2600,7.1,QB,Justin Herbert,5300,17.2,RB,Alvin and the Chipmunks,7800,22.9,RB,Austin Powers,5300,16.2,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,147.9", + three_rb[ + 6] == "7,DST,Cardinals,2600,7.1,QB,Gardner Minshew II,4500,14.7,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,49900,147.8", + three_rb[ + 7] == "8,DST,Cardinals,2600,7.1,QB,Justin Herbert,5300,17.2,RB,Alvin and the Chipmunks,7800,22.9,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,147.8", + three_rb[ + 8] == "9,DST,Cardinals,2600,7.1,QB,Justin Herbert,5300,17.2,RB,Chase from Paw Patrol,5900,17.9,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,147.8", + three_rb[ + 9] == "10,DST,Cardinals,2600,7.1,QB,Dak Prescott,6300,19.5,RB,Alvin and the Chipmunks,7800,22.9,RB,Austin Powers,5300,16.2,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,147.7", + ] + + assert all(conditions) + + +def test_two_te(): + with open('two_te.csv', 'r') as f: + two_te = [f.readline().strip() for _ in range(10)] + + conditions = [ + len(two_te) == 10, + two_te[ + 0] == "1,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,Mike Gesicki,3100,10.6,TE,Taysom Hill,3800,9.9,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,146.0", + two_te[ + 1] == "2,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,Mike Gesicki,3100,10.6,TE,Taysom Hill,3800,9.9,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,146.0", + two_te[ + 2] == "3,DST,Cardinals,2600,7.1,QB,Lamar Jackson,8000,24.3,RB,Alvin and the Chipmunks,7800,22.9,RB,Matt Busche,7800,22.9,TE,AJ Barner,2600,7.2,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,146.0", + two_te[ + 3] == "4,DST,Titans,3200,8.7,QB,Justin Herbert,5300,17.2,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,145.9", + two_te[ + 4] == "5,DST,Titans,3200,8.7,QB,Justin Herbert,5300,17.2,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,50000,145.9", + two_te[ + 5] == "6,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,AJ Barner,2600,7.2,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,145.8", + two_te[ + 6] == "7,DST,Titans,3200,8.7,QB,Lamar Jackson,8000,24.3,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,AJ Barner,2600,7.2,TE,Mike Gesicki,3100,10.6,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,WR,Jakobi Meyers,5300,14.0,49800,145.8", + two_te[ + 7] == "8,DST,Titans,3200,8.7,QB,Dak Prescott,6300,19.5,RB,Alvin and the Chipmunks,7800,22.9,RB,Kyren Williams,7000,21.1,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,145.7", + two_te[ + 8] == "9,DST,Titans,3200,8.7,QB,Dak Prescott,6300,19.5,RB,Kyren Williams,7000,21.1,RB,Matt Busche,7800,22.9,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,145.7", + two_te[ + 9] == "10,DST,Cardinals,2600,7.1,QB,Lamar Jackson,8000,24.3,RB,Chase from Paw Patrol,5900,17.9,RB,Matt Busche,7800,22.9,TE,David Njoku,5500,14.4,TE,Mike Gesicki,3100,10.6,WR,Cedric Tillman,4300,11.5,WR,Chris Olave,6100,17.0,WR,Drake London,6700,20.0,50000,145.7", + ] + + assert all(conditions)