Skip to content

Commit

Permalink
Change stats to return object rather than dictionary
Browse files Browse the repository at this point in the history
  • Loading branch information
panzarino committed Aug 21, 2017
1 parent 92a0176 commit f51be65
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
16 changes: 2 additions & 14 deletions mlbgame/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,9 @@ def combine_games(games):

def player_stats(game_id):
"""Return dictionary of player stats for game matching the game id."""
# get information for that day
obj = None
# get information for that game
data = mlbgame.stats.player_stats(game_id)
output = {'home_pitching': [], 'away_pitching': [], 'home_batting': [],
'away_batting': []}
for y in data:
for x in data[y]:
# create objects for all data
if y == 'home_pitching' or y == 'away_pitching':
obj = mlbgame.stats.PitcherStats(x)
elif y == 'home_batting' or y == 'away_batting':
obj = mlbgame.stats.BatterStats(x)
# place into correct place in return dictionary
output[y].append(obj)
return output
return mlbgame.stats.Stats(data, game_id)


def team_stats(game_id):
Expand Down
2 changes: 1 addition & 1 deletion mlbgame/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ class Players(object):
"""

def __init__(self, data):
"""Creates an overview object that matches the corresponding info in `data`.
"""Creates a players object that matches the corresponding info in `data`.
`data` should be an dictionary of values.
"""
self.game_id = data['game_id']
Expand Down
36 changes: 34 additions & 2 deletions mlbgame/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,40 @@ def team_stats(game_id):
return output


class Stats(object):
"""Hold stats information for a game.
Properties:
away_batting
away_pitching
game_id
home_batting
home_pitching
"""

def __init__(self, data, game_id):
"""Creates a players object that matches the corresponding info in `data`.
`data` should be an dictionary of values.
'game_id' should be the id for the game.
"""
self.game_id = game_id
output = {'home_pitching': [], 'away_pitching': [], 'home_batting': [],
'away_batting': []}
for y in data:
for x in data[y]:
# create objects for all data
if y == 'home_pitching' or y == 'away_pitching':
obj = mlbgame.stats.PitcherStats(x)
elif y == 'home_batting' or y == 'away_batting':
obj = mlbgame.stats.BatterStats(x)
# place into correct place in return dictionary
output[y].append(obj)
self.home_pitching = output['home_pitching']
self.away_pitching = output['away_pitching']
self.home_batting = output['home_batting']
self.away_batting = output['away_batting']


class PitcherStats(mlbgame.object.Object):
"""Holds stats information for a pitcher.
Expand Down Expand Up @@ -158,6 +192,4 @@ def __str__(self):

class TeamStats(mlbgame.object.Object):
"""Holds total pitching or batting stats for a team"""
# basically a copy of the object
# class with a different name for clarification
pass

0 comments on commit f51be65

Please sign in to comment.