Search code examples
pythonpython-3.xkeyerror

Dealing with one KeyError best practice


I have shortend the script and the start date of pendulum to reproduce this error more quickly. When the start date was 2016, 4, 2 I got through around 47,661 returned results before this KeyError occurred. To my knowledge I could use a try: except to pass up the KeyError. However I would not know any other KeyErrors that may come up after that point and that seems like I am just (for lack of a better word) "swallowing" my errors. What is best practice for this 1 in 50,000 (so far) occurrance.

import csv
import requests
import datetime
from pprint import pprint
import pendulum

gamepks = set()

start = pendulum.datetime(2016, 5, 18)
end = pendulum.datetime(2016, 10, 2)
period = pendulum.period(start, end)

for dt in period.range('days'):
        day = dt.format('DD')
        month = dt.format('MM')
        year = dt.format('YYYY')
        the_date = str(month) + "/" + str(day) + "/" + str(year) 

        try:
            req = requests.get('http://gd.mlb.com/components/game/mlb/year_' + str(year) + '/month_' + str(month) + '/day_' + str(day) + '/miniscoreboard.json') # 
            get_gameIds = req.json()['data']['games']['game']

            for gameId in get_gameIds:
                gamepk = gameId['game_pk']
                gamepks.add(gamepk)            
        except(KeyError,TypeError):
            pass

for new_pk in sorted(gamepks):

    req = requests.get('https://statsapi.mlb.com/api/v1.1/game/' + str(new_pk) + '/feed/live?language=en') # ' + str(gamepk) + ' 530302
    at_bat_log = req.json()['liveData']['plays']['allPlays']

    # Get Game date

    game_data = req.json()['gameData']
    gamedate = game_data['datetime']['originalDate']

    # GET PARK & TEAMS

    teams = game_data['teams']
    home_team = teams['home']
    park = home_team['abbreviation']
    away = teams['away']['abbreviation']
    home = home_team['abbreviation']
    batter_team = (away,home)
    pitcher_team = (home,away)

    for keys in at_bat_log:
        result = keys['result']
        res_type = result['type']
        res_event = result['event']
        des = result['description']
        rbi = result['rbi']
        about = keys['about']
        topbot = about['halfInning']

        if topbot == "bottom":
            topbot = "B"
        if topbot == "top":
            topbot = "T"

# Traceback (most recent call last):
#  File "C:/Python36/Projects/Shoretend_for_testing_CODE.py", line 65, in <module>
#    inn = about['inning']
#  KeyError: 'inning'

        inn = about['inning']
        inning = str(topbot) + str(inn)

        row = [new_pk, inning]
        print(row)

Solution

  • Your best bet is to use .get method on dictionaries. so, instead of doing keys['about'], use keys.get("about", "DEFAULT_VALUE").
    so, if key exists, you will get the value, else you will get default value which is the second argument in the get method