-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathmlb_module.py
More file actions
161 lines (125 loc) · 4.03 KB
/
mlb_module.py
File metadata and controls
161 lines (125 loc) · 4.03 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
from typing import Union, List, Dict
import importlib
import inspect
from mlbstatsapi.models.stats import Stat
def merge_keys(mlb_dict, mlb_keys: Union[List[Union[dict, str]], str]) -> dict:
"""
A recursive function that merges requested nested dicts inside mlb_dict into mlb_dict base.
Parameters
----------
mlb_dict : dict
mlb_dict is a dictionary that requires transformation
mlb_keys : str, list[str], list[dict[str, dict]]
key names you want to nest
Returns
-------
dict
returns a dict that has been transformed
"""
if isinstance(mlb_keys, List):
for key in mlb_keys:
if isinstance(key, Dict):
for nested_key in key:
mlb_merge_item = mlb_dict.pop(nested_key)
mlb_dict.update(merge_keys(mlb_merge_item, key[nested_key]))
else:
mlb_merge_item = mlb_dict.pop(key)
mlb_dict.update(**mlb_merge_item)
else:
mlb_merge_item = mlb_dict.pop(mlb_keys)
mlb_dict.update(**mlb_merge_item)
return mlb_dict
def return_splits(split_data: dict, stat_type: str, stat_group: str) -> List['Split']:
"""
The split objects are built using the group name and split data. The stat group name is used to source the correct
stat group classes.
stat group: hitting will load the classes in hitting.py and use the _type parameter to locate the correct class.
Parameters
----------
split_data : dict
split data
stat_type : str
type of stat
stat_group : str
group of stat
Returns
-------
list
returns a list of stat objects
"""
splits = []
stat_module = f"mlbstatsapi.models.stats.{stat_group}"
stat_module = importlib.import_module(stat_module)
for name, obj in inspect.getmembers(stat_module, predicate=inspect.isclass):
if hasattr(obj, '_stat') and stat_type in obj._stat:
for split in split_data:
if 'stat' in split and split['stat']:
splits.append(obj(**split))
return splits
def get_split_count(stat: dict) -> int:
"""
function that returns split count from stats
Parameters
----------
stat: dict
dict of stats
Returns
-------
int
returns number of splits
"""
def create_split_data(stat_data: dict) -> dict:
"""
function that loops through stat information, creates splits, and return stat dict
Parameters
----------
stat_data: dict
dict of params to pass
Returns
-------
dict
returns a dict of stats
"""
stats = {}
for stat in stat_data:
# get type and group of stat
stat_type, stat_group, total_splits = get_stat_attributes(stat)
if 'splits' in stat and stat['splits']:
split_data = return_splits(stat['splits'], stat_type, stat_group)
stat_object = Stat(group=stat_group, type=stat_type,
totalSplits=total_splits, splits=split_data)
else:
continue
if stat_group not in stats:
stats[stat_group] = {}
stats[stat_group][stat_type] = stat_object
return stats
def get_stat_attributes(stats) -> str:
"""
return stat type
Parameters
----------
stats : dict
stat data
Returns
-------
(stat_type, stat_group)
"""
if 'type' in stats and 'displayName' in stats['type']:
stat_type = stats['type']['displayName']
else:
stat_type = 'gameLog'
# default to stats if no group returned
if 'group' in stats and 'displayName' in stats['group']:
stat_group = stats['group']['displayName']
else:
# if stat_type is None return None
if stat_type:
stat_group = 'stats'
else:
stat_group = None
if 'totalSplits' in stats:
total_splits = stats['totalSplits']
else:
total_splits = len(stats['splits'])
return (stat_type, stat_group, total_splits)