From 0e83f867b629f5098459bf6e97891cb9dad37ee7 Mon Sep 17 00:00:00 2001 From: popeg Date: Thu, 18 Jan 2018 23:54:15 +0100 Subject: [PATCH 1/2] Peacekeeping PEP-8 forces. Simplify code redundancy. --- nba_py/__init__.py | 19 ++++++------------- nba_py/constants.py | 14 +++++++++----- tests/test_nba_py.py | 1 + tests/test_nba_py_league.py | 22 ++++++++++------------ tests/test_nba_py_team.py | 1 + 5 files changed, 27 insertions(+), 30 deletions(-) diff --git a/nba_py/__init__.py b/nba_py/__init__.py index d15a964..f5892c4 100644 --- a/nba_py/__init__.py +++ b/nba_py/__init__.py @@ -46,20 +46,14 @@ def _api_scrape(json_inp, ndx): else: A dictionary of both headers and values from the page """ - try: - headers = json_inp['resultSets'][ndx]['headers'] - values = json_inp['resultSets'][ndx]['rowSet'] + headers = json_inp.get('resultSets', 'resultSet')[ndx]['headers'] + values = json_inp.get('resultSets', 'resultSet')[ndx]['rowSet'] except KeyError: - # This is so ugly but this is what you get when your data comes out - # in not a standard format - try: - headers = json_inp['resultSet'][ndx]['headers'] - values = json_inp['resultSet'][ndx]['rowSet'] - except KeyError: - # Added for results that only include one set (ex. LeagueLeaders) - headers = json_inp['resultSet']['headers'] - values = json_inp['resultSet']['rowSet'] + # Added for results that only include one set (ex. LeagueLeaders) + headers = json_inp['resultSet']['headers'] + values = json_inp['resultSet']['rowSet'] + if HAS_PANDAS: return DataFrame(values, columns=headers) else: @@ -85,7 +79,6 @@ def _get_json(endpoint, params, referer='scores'): h['referer'] = 'http://stats.nba.com/{ref}/'.format(ref=referer) _get = get(BASE_URL.format(endpoint=endpoint), params=params, headers=h) - # print _get.url _get.raise_for_status() return _get.json() diff --git a/nba_py/constants.py b/nba_py/constants.py index 7e2c317..30e7366 100644 --- a/nba_py/constants.py +++ b/nba_py/constants.py @@ -1,10 +1,14 @@ from datetime import datetime -_curr_year = datetime.now().year -if datetime.now().month > 6: - CURRENT_SEASON = str(_curr_year) + "-" + str(_curr_year + 1)[2:] -else: - CURRENT_SEASON = str(_curr_year - 1) + "-" + str(_curr_year)[2:] + +def set_current_season(): + curr_year = datetime.now().year + current_season = '{}-{}'.format(curr_year - 1, str(curr_year)[2:]) + if datetime.now().month > 6: + current_season = '{}-{}'.format(curr_year, str(curr_year+1)[2:]) + return current_season + +CURRENT_SEASON = set_current_season() TEAMS = { 'ATL': { diff --git a/tests/test_nba_py.py b/tests/test_nba_py.py index 044bb6c..e8de25e 100644 --- a/tests/test_nba_py.py +++ b/tests/test_nba_py.py @@ -1,4 +1,5 @@ import nba_py + def test(): assert nba_py.Scoreboard(month=2, day=21, year=2015) diff --git a/tests/test_nba_py_league.py b/tests/test_nba_py_league.py index ba0639c..8b9bee8 100644 --- a/tests/test_nba_py_league.py +++ b/tests/test_nba_py_league.py @@ -5,23 +5,21 @@ except ImportError: pass -class TestPlayerSpeedDistanceTracking: +class TestPlayerSpeedDistanceTracking: def test_overall(self): speed = league.PlayerSpeedDistanceTracking(date_from='03/05/2016', date_to='03/05/2016', season="2015-16") assert speed overall = speed.overall() assert overall - iter = filter(lambda d: d['PLAYER_NAME'] == 'Derrick Rose', overall) - stats = next(iter) + iterator = filter(lambda d: d['PLAYER_NAME'] == 'Derrick Rose', overall) + stats = next(iterator) assert stats - assert stats['GP'] == 1 - assert stats['MIN'] == 29.25 - assert stats['DIST_MILES'] == 2.24 - assert stats['DIST_FEET'] == 11827.0 - assert stats['DIST_MILES_OFF'] == 1.29 - assert stats['DIST_MILES_DEF'] == 0.95 - assert stats['AVG_SPEED'] == 4.52 - assert stats['AVG_SPEED_OFF'] == 4.94 - assert stats['AVG_SPEED_DEF'] == 4.42 + assertions = { + stats['GP']: 1, stats['MIN']: 29.25, stats['DIST_MILES']: 2.24, + stats['DIST_FEET']: 11827.0, stats['DIST_MILES_OFF']: 1.29, stats['DIST_MILES_DEF']: 0.95, + stats['AVG_SPEED']: 4.52, stats['AVG_SPEED_OFF']: 4.94, stats['AVG_SPEED_DEF']: 4.42 + } + for assertion in assertions: + assert assertion == assertions[assertion] \ No newline at end of file diff --git a/tests/test_nba_py_team.py b/tests/test_nba_py_team.py index 1762f26..51203fb 100644 --- a/tests/test_nba_py_team.py +++ b/tests/test_nba_py_team.py @@ -2,6 +2,7 @@ from nba_py.player import get_player from nba_py.constants import TEAMS + def test(): team_id = TEAMS['ATL']['id'] player_id = get_player('Lebron', 'James') From 0ce2155b0ded7cc8a555a4d26d8025a3cefec11e Mon Sep 17 00:00:00 2001 From: popeg Date: Sun, 21 Jan 2018 13:49:11 +0100 Subject: [PATCH 2/2] Add extra enter for Travis pleasure. --- nba_py/constants.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nba_py/constants.py b/nba_py/constants.py index 30e7366..8219404 100644 --- a/nba_py/constants.py +++ b/nba_py/constants.py @@ -8,6 +8,7 @@ def set_current_season(): current_season = '{}-{}'.format(curr_year, str(curr_year+1)[2:]) return current_season + CURRENT_SEASON = set_current_season() TEAMS = {