Skip to content

Commit

Permalink
Clean up event and object creation
Browse files Browse the repository at this point in the history
  • Loading branch information
panzarino committed Aug 24, 2017
1 parent d91a9ab commit bb39c6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 25 deletions.
12 changes: 1 addition & 11 deletions mlbgame/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,7 @@ def __init__(self, data):
self.pitches.append(Pitch(y))
continue
# set information as correct data type
try:
setattr(self, x, int(data[x]))
except ValueError:
try:
setattr(self, x, float(data[x]))
except ValueError:
# string if not number
try:
setattr(self, x, str(data[x]))
except UnicodeEncodeError:
setattr(self, x, data[x])
mlbgame.object.setobjattr(self, x, data[x])

def nice_output(self):
"""Prints basic at bat info in a nice way."""
Expand Down
32 changes: 18 additions & 14 deletions mlbgame/object.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,29 @@
"""Module that is used for holding basic objects"""


def setobjattr(obj, key, value):
"""Sets an object attribute with the correct data type."""
try:
setattr(obj, key, int(value))
except ValueError:
try:
setattr(obj, key, float(value))
except ValueError:
# string if not number
try:
setattr(obj, key, str(value))
except UnicodeEncodeError:
setattr(obj, key, value)


class Object(object):
"""Basic class"""

def __init__(self, data):
"""Creates an object that matches the corresponding stats in `data`.
"""Creates an object that matches the corresponding values in `data`.
`data` should be an dictionary of values.
`data` should be a dictionary of values.
"""
# loop through data
for x in data:
# set information as correct data type
try:
setattr(self, x, int(data[x]))
except ValueError:
try:
setattr(self, x, float(data[x]))
except ValueError:
# string if not number
try:
setattr(self, x, str(data[x]))
except UnicodeEncodeError:
setattr(self, x, data[x])
setobjattr(self, x, data[x])

0 comments on commit bb39c6b

Please sign in to comment.