-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
65 lines (57 loc) · 2.69 KB
/
views.py
File metadata and controls
65 lines (57 loc) · 2.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
context = super().get_context_data(**kwargs)
# Fetch data for hitters and pitchers based on the query
query = Q(year=2025, projection_system='steamer') | Q(year=2026, projection_system='zips')
hitters = HittingStatistics.objects.filter(query)
pitchers = PitchingStatistics.objects.filter(query)
all_stats = list(chain(hitters, pitchers))
hitting_stats = ['fOPS', 'fAVG', 'fR', 'fRBI', 'fHR', 'fSB']
pitching_stats = ['fERA', 'fWHIP', 'fW', 'fSO', 'fSVH', 'fK_BB']
# players = {s.player for s in all_stats}
player_stats = defaultdict(lambda: defaultdict(list))
for stat in all_stats:
player_stats[stat.player][stat.year].append(stat)
player_id_map = []
fScores = defaultdict(dict)
for player, years in player_stats.items():
player_id_map.append({'name': player.name, 'fangraphs_id': player.fangraphs_id})
for year, stats in years.items():
hitting_total = 0
pitching_total = 0
for stat in stats:
if type(stat) == HittingStatistics:
hitting_total = self.calculate_fScores(stat, hitting_stats)
elif type(stat) == PitchingStatistics:
pitching_total = self.calculate_fScores(stat, pitching_stats)
fscore = hitting_total + pitching_total
fScores[player.fangraphs_id][year] = fscore
context['fScores'] = fScores
context['players'] = player_id_map
# # Generate hitting and pitching fScores
# hitting_fScores = self.calculate_fScores(hitters, [
# 'fOPS', 'fAVG', 'fR', 'fRBI', 'fHR', 'fSB'
# ])
# pitching_fScores = self.calculate_fScores(pitchers, [
# 'fERA', 'fWHIP', 'fW', 'fSO', 'fSVH', 'fK_BB'
# ])
# context['fScores'] = hitting_fScores.update(pitching_fScores)
# Teams for the template
context['teams'] = [
{'name': 'Team A', 'form_id': 'trade_form1'},
{'name': 'Team B', 'form_id': 'trade_form2'},
]
return context
def calculate_fScores(self, stat, attributes):
"""
Calculate fScores for players based on specified attributes.
"""
# fScores = {}
# player_id = stat.player.fangraphs_id
# if player_id not in fScores:
# fScores[player_id] = {}
# fScores[player_id][stat.year] = round(
# sum(getattr(stat, attr, 0) for attr in attributes) / len(attributes)
# )
# return fScores
return round(
sum(getattr(stat, attr, 0) for attr in attributes) / len(attributes)
)