Skip to content

Commit

Permalink
Finished v2021.0 and added feature roadmap.
Browse files Browse the repository at this point in the history
  • Loading branch information
covrebo committed Feb 27, 2021
1 parent 2bdbdbd commit ffc7127
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 43 deletions.
73 changes: 55 additions & 18 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,5 @@
import services

# √ choose the players check to be sure the data directory exists
# √ print the header in the console
# √ prompt the user for the race number
# √ prompt the user for the results URL from ESPN
# √ scrape ESPN results table and write results to CSV
# √ import the picks for the week
# √ import the weekly picks and convert to named tuple
# TODO: import the standings and convert to named tuples
# TODO: calculate the weekly points and new standings
# TODO: write the new standing to csv

def main():
# check if the data directory exists, and create it if it doesn't
services.create_data_directory()
Expand All @@ -25,8 +14,14 @@ def main():
picks = services.import_picks_from_csv(race)

# print the picks for the week
print()
print("######################")
print("### WEEKLY PICKS ###")
print("######################")
print()
for p in picks:
print(f"{p.picker} has the {p.picks[0]}, {p.picks[1]}, and {p.picks[2]}")
print()

# retrieve the race results
raw_site = services.pull_site(url)
Expand All @@ -41,23 +36,65 @@ def main():
# calculate weekly points
weekly_points = services.weekly_points(race, picks, results)

print("### RESULTS ###")
print()
print("########################")
print("### WEEKLY RESULTS ###")
print("########################")
print()
for rank, picker in enumerate(weekly_points):
print(f"#{rank + 1}: {picker.picker} with {picker.total_points}")
print()

# write the results to a csv file
services.write_results_to_csv(results, race)

# import previous standings
prev_standings = services.import_previous_standings(race)
print(prev_standings)
# DEBUG print(prev_standings)

# update standings
standings = services.calculate_standings(race, prev_standings, weekly_points)
print(standings)
standings = services.calculate_standings(prev_standings, weekly_points)
# DEBUG print(standings)

# write the standings to a csv
new_standings = services.write_standings_to_csv(race, prev_standings, standings)

# TODO: write the standings to a csv
services.write_standings_to_csv(race, prev_standings, standings)
# print the standings to the console
services.display_standings(new_standings)

# TODO: Track weekly pool winners
# TODO: Convert to db
# TODO: Migrate console prints to services
# TODO: Write tests

if __name__ == '__main__':
main()
main()

#################
### ROADMAP ###
#################
#
# 2021.0
# Initial release
# √ choose the players check to be sure the data directory exists
# √ print the header in the console
# √ prompt the user for the race number
# √ prompt the user for the results URL from ESPN
# √ scrape ESPN results table and write results to CSV
# √ import the picks for the week
# √ import the weekly picks and convert to named tuple
# √ import the standings and convert to named tuples
# √ calculate the weekly points and new standings
# √ write the new standings to csv
#
# 2021.1
# Weekly win tracking
# Split picks selections from results
# Refactor for better data structures
# Email support for weekly email
#
# 2021.2
# Refactor from csv to database supper
#
# 2021.3
# Web app implementation
64 changes: 39 additions & 25 deletions services.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,42 +45,35 @@ def weekly_points(race: int, picks: List[Picks], results: List) -> List[
int(results_dict[f'{pick.picks[0]}']['PTS']) + int(
results_dict[f'{pick.picks[1]}']['PTS']) + int(
results_dict[f'{pick.picks[2]}']['PTS']) + int(bonus))))
# 0, (int(results_dict[f'{pick.picks[0]}']['PTS']) + int(results_dict[f'{pick.picks[1]}']['PTS']) + int(results_dict[f'{pick.picks[2]}']['PTS']), 0)
print()

wkly_points = sorted(wkly_points, key=lambda x: x.total_points,
reverse=True)

# print(race)
# print(picks)
# print(results)
# print(results_dict)
# print(wkly_points)
# for pick in wkly_points:
# print(f"{pick.picker} finished with {pick.total_points} points.")
return wkly_points


def calculate_standings(race: str, prev_standings: List[List],
weekly_points: List[WeeklyPoints]) -> Dict:
def calculate_standings(prev_standings: List[List],
wkly_points: List[WeeklyPoints]) -> Dict:
prev_standings_dict = {}
wkly_standings = {}
if prev_standings:
for row in prev_standings[1:]:
prev_standings_dict[f"{row[0]}"]: row[-1]
for row in prev_standings[1:]:
tp = [pick.total_points for pick in weekly_points if
pick.picker == row[0]]
wp = [pick.total_points for pick in weekly_points if
pick.picker == row[0]]
wkly_standings[f"{row[0]}"]= [
tp = [pick.total_points for pick in wkly_points if
pick.picker == row[0]]
wp = [pick.total_points for pick in wkly_points if
pick.picker == row[0]]
wkly_standings[f"{row[0]}"] = [
('picker', f"{row[0]}"),
('weekly_points', int(wp[0])),
('total_points', int(row[-1]) + int(tp[0]))
]
return wkly_standings
else:
for pick in weekly_points:
wkly_standings[pick.picker]= [
for pick in wkly_points:
wkly_standings[pick.picker] = [
('picker', pick.picker),
('weekly_points', pick.total_points),
('total_points', pick.total_points)
Expand Down Expand Up @@ -205,22 +198,26 @@ def import_previous_standings(race: str) -> List[List]:
return None


def write_standings_to_csv(race: str, prev_standings: List[List], standings: Dict):
def write_standings_to_csv(race: str, prev_standings: List[List],
standings: Dict) -> List:
new_standings = []
if prev_standings:
headers = prev_standings[0]
print(f"headers before: {prev_standings[0]}")
# DEBUG print(f"headers before: {prev_standings[0]}")
headers.append(f'R{race} Points')
headers.append(f'R{race} Total Points')
print(f"headers after: {headers}")
new_standings.append(headers)
# DEBUG print(f"headers after: {headers}")
try:
with open(f"data/{race}_standings.csv", "w") as f:
writer = csv.writer(f)
writer.writerow(headers)
for row in prev_standings[1:]:
print(f"row before: {row}")
# DEBUG print(f"row before: {row}")
row.append(standings[f"{row[0]}"][1][1])
row.append(standings[f"{row[0]}"][2][1])
print(f"row after: {row}")
new_standings.append(row)
# DEBUG print(f"row after: {row}")
writer.writerow(row)
except:
print(f"There was an error trying to save the weekly standings.")
Expand All @@ -229,16 +226,18 @@ def write_standings_to_csv(race: str, prev_standings: List[List], standings: Dic
with open(f"data/{race}_standings.csv", "w") as f:
writer = csv.writer(f)
headers = ["picker", "R1 Points", "R1 Total Points"]
new_standings.append(headers)
writer.writerow(headers)
for key, value in standings.items():
print(f"row before: {key}, {value}")
# DEBUG print(f"row before: {key}, {value}")
row = [f"{key}", f"{value[1][1]}", f"{value[2][1]}"]
print(f"{row}")
new_standings.append(row)
# DEBUG print(f"{row}")
writer.writerow(row)
except:
print(f"There was an error trying to save the weekly standings.")

return None
return new_standings


###################
Expand Down Expand Up @@ -278,3 +277,18 @@ def get_user_input():
url = input("What is the results url from ESPN: ")

return race, url


def display_standings(new_standings: List):
# remove the row with the headers
new_standings.pop(0)
# sort the standings
sorted_standings = sorted(new_standings, key=lambda x: x[-1], reverse=True)
print()
print("###################")
print("### STANDINGS ###")
print("###################")
print()
for rank, list in enumerate(sorted_standings):
print(f"#{rank + 1}: {list[0]} with {list[-1]} points.")
return None

0 comments on commit ffc7127

Please sign in to comment.