NBAPlayers Documentation


In [1]:
import datetime
import logging
import pprint

logger = logging.getLogger()
logger.setLevel(logging.DEBUG)

In [17]:
import players
reload(players)
from players import NBAPlayers
from seasons import NBASeasons

Initialization


In [3]:
nbap = NBAPlayers()
nbas = NBASeasons()

In [29]:
for k,v in vars(nbap).iteritems():
    print 'NBAPlayers attribute: {0}'.format(k)


NBAPlayers attribute: nbadb
NBAPlayers attribute: parser
NBAPlayers attribute: scraper
NBAPlayers attribute: polite

In [32]:
for k,v in vars(nbap.scraper).iteritems():
    print k,v


headers {'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2227.0 Safari/537.36'}
is_polite True

In [33]:
print nbap.nbadb


None

In [34]:
print nbap.polite


False

In [41]:
nbap = NBAPlayers(db=True, polite=True)
print nbap.nbadb
print nbap.polite


<nba.db.pgsql.NBAPostgres object at 0x7f137a13ded0>
True

In [42]:
for k,v in vars(nbap.nbadb).iteritems():
    print k, v


user postgres
conn <connection object at 0x7f13780b6c58; dsn: 'dbname=nba user=postgres', closed: 0>
database nba

In [22]:
print nbap.nbadb


<nba.db.pgsql.NBAPostgres object at 0x7f137a12fad0>

missing_players()


In [8]:
nbap = NBAPlayers()

In [9]:
season = nbas.in_what_season(datetime.datetime.today())
nbap.missing_players(season)


---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-9-650e86bc6ab5> in <module>()
      1 season = nbas.in_what_season(datetime.datetime.today())
----> 2 nbap.missing_players(season)

/home/sansbacon/workspace/nba/players.py in missing_players(self, season)
     42 
     43         if not self.nbadb:
---> 44             raise ValueError('missing_players requires a database connection')
     45 
     46         missing = []

ValueError: missing_players requires a database connection

In [18]:
nbap = NBAPlayers(db=True)

In [19]:
season = nbas.in_what_season(datetime.datetime.today())
missing = nbap.missing_players(season)

In [21]:
if missing:
    print missing[0]
    
else:
    print 'players table up-to-date'


players table up-to-date

season()


In [23]:
nbas.season('2015-16')


Out[23]:
{'end': datetime.datetime(2016, 4, 13, 0, 0),
 'start': datetime.datetime(2015, 10, 27, 0, 0)}

season_dates()


In [26]:
nbas.season_dates('2015-16')[0:5]


Out[26]:
[datetime.datetime(2016, 4, 13, 0, 0),
 datetime.datetime(2016, 4, 12, 0, 0),
 datetime.datetime(2016, 4, 11, 0, 0),
 datetime.datetime(2016, 4, 10, 0, 0),
 datetime.datetime(2016, 4, 9, 0, 0)]

In [34]:
# dates sort properly

sorted(nbas.season_dates('2015-16'))[0:5]


Out[34]:
[datetime.datetime(2015, 10, 27, 0, 0),
 datetime.datetime(2015, 10, 28, 0, 0),
 datetime.datetime(2015, 10, 29, 0, 0),
 datetime.datetime(2015, 10, 30, 0, 0),
 datetime.datetime(2015, 10, 31, 0, 0)]

In [29]:
nbas.season_dates(season='2015-16', date_format='%Y-%m-%d')[0:5]


Out[29]:
['2016-04-13', '2016-04-12', '2016-04-11', '2016-04-10', '2016-04-09']

In [37]:
# date strings do not always sort properly

print sorted(nbas.season_dates(season='2015-16', date_format='%Y-%m-%d'))[0:5]
print sorted(nbas.season_dates(season='2015-16', date_format='%m_%d_%Y'))[0:5]


['2015-10-27', '2015-10-28', '2015-10-29', '2015-10-30', '2015-10-31']
['01_01_2016', '01_02_2016', '01_03_2016', '01_04_2016', '01_05_2016']

In [39]:
# can get subset of season's dates if needed

nbas.season_dates(season='2015-16', start_date=nbas.season_start('2015-16'), end_date=datetime.datetime.today())[0:5]


Out[39]:
[datetime.datetime(2016, 2, 6, 19, 10, 58, 314684),
 datetime.datetime(2016, 2, 5, 19, 10, 58, 314684),
 datetime.datetime(2016, 2, 4, 19, 10, 58, 314684),
 datetime.datetime(2016, 2, 3, 19, 10, 58, 314684),
 datetime.datetime(2016, 2, 2, 19, 10, 58, 314684)]

season_start()


In [41]:
for season in ['2015-16', '2014-15', '2013-14']:
    print nbas.season_start(season)


2015-10-27 00:00:00
2014-10-28 00:00:00
2013-10-29 00:00:00

season_end()


In [42]:
for season in ['2015-16', '2014-15', '2013-14']:
    print nbas.season_end(season)


2016-04-13 00:00:00
2015-04-15 00:00:00
2014-04-16 00:00:00

seasons()


In [44]:
for k,v in nbas.seasons().iteritems():
    print k, v


2015-16 {'start': datetime.datetime(2015, 10, 27, 0, 0), 'end': datetime.datetime(2016, 4, 13, 0, 0)}
2014-15 {'start': datetime.datetime(2014, 10, 28, 0, 0), 'end': datetime.datetime(2015, 4, 15, 0, 0)}
2013-14 {'start': datetime.datetime(2013, 10, 29, 0, 0), 'end': datetime.datetime(2014, 4, 16, 0, 0)}
2012-13 {'start': datetime.datetime(2012, 10, 30, 0, 0), 'end': datetime.datetime(2013, 4, 17, 0, 0)}
2011-12 {'start': datetime.datetime(2011, 11, 25, 0, 0), 'end': datetime.datetime(2012, 4, 26, 0, 0)}
2010-11 {'start': datetime.datetime(2010, 10, 26, 0, 0), 'end': datetime.datetime(2011, 4, 13, 0, 0)}
2009-10 {'start': datetime.datetime(2009, 10, 27, 0, 0), 'end': datetime.datetime(2010, 4, 14, 0, 0)}
2008-09 {'start': datetime.datetime(2008, 10, 28, 0, 0), 'end': datetime.datetime(2009, 4, 15, 0, 0)}
2007-08 {'start': datetime.datetime(2007, 10, 30, 0, 0), 'end': datetime.datetime(2008, 4, 16, 0, 0)}
2006-07 {'start': datetime.datetime(2006, 10, 31, 0, 0), 'end': datetime.datetime(2007, 4, 18, 0, 0)}
2005-06 {'start': datetime.datetime(2005, 11, 1, 0, 0), 'end': datetime.datetime(2006, 4, 19, 0, 0)}
2004-05 {'start': datetime.datetime(2004, 11, 2, 0, 0), 'end': datetime.datetime(2005, 4, 20, 0, 0)}
2003-04 {'start': datetime.datetime(2003, 10, 28, 0, 0), 'end': datetime.datetime(2004, 4, 14, 0, 0)}
2002-03 {'start': datetime.datetime(2002, 10, 29, 0, 0), 'end': datetime.datetime(2003, 4, 16, 0, 0)}
2001-02 {'start': datetime.datetime(2001, 10, 30, 0, 0), 'end': datetime.datetime(2002, 4, 17, 0, 0)}
2000-01 {'start': datetime.datetime(2000, 10, 31, 0, 0), 'end': datetime.datetime(2001, 4, 18, 0, 0)}
1999-00 {'start': datetime.datetime(1999, 11, 2, 0, 0), 'end': datetime.datetime(2000, 4, 19, 0, 0)}
1998-99 {'start': datetime.datetime(1999, 2, 5, 0, 0), 'end': datetime.datetime(1999, 5, 5, 0, 0)}
1997-98 {'start': datetime.datetime(1997, 10, 31, 0, 0), 'end': datetime.datetime(1998, 4, 19, 0, 0)}
1996-97 {'start': datetime.datetime(1996, 11, 1, 0, 0), 'end': datetime.datetime(1997, 4, 20, 0, 0)}

In [ ]: