Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3 support #126

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
*.py[co]
*.egg*
__pycache__/
env*/

.*.swp
doc
MANIFEST
Expand Down
9 changes: 3 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ games = nflgame.games(2013, week=1)
players = nflgame.combine_game_stats(games)
for p in players.rushing().sort('rushing_yds').limit(5):
msg = '%s %d carries for %d yards and %d TDs'
print msg % (p, p.rushing_att, p.rushing_yds, p.rushing_tds)
print(msg % (p, p.rushing_att, p.rushing_yds, p.rushing_tds))
```

And the output is:
Expand All @@ -47,7 +47,7 @@ import nflgame
games = nflgame.games(2013, week=1)
plays = nflgame.combine_plays(games)
for p in plays.sort('passing_yds').limit(5):
print p
print(p)
```

And the output is:
Expand Down Expand Up @@ -109,8 +109,6 @@ On all platforms, it is recommend to install it with `pip`:
pip install nflgame
```

(You may need to use `pip2` if Python 3 is the default on your system.)

If you can't get `pip` to work on Windows, then you can try downloading the
Windows installer on nflgame's PyPI page.

Expand All @@ -120,8 +118,7 @@ Python standard library, but `nflgame.live` depends on `pytz` and the
All three dependencies are installed automatically if you install nflgame from
PyPI with `pip`.

nflgame does not yet work on Python 3, but it should work with Python 2.6 and
2.7.
nflgame works on Python 2.6, 2.7, and Python 3.3+


### Updating the player database (e.g., rosters)
Expand Down
14 changes: 5 additions & 9 deletions nflgame/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
players = nflgame.combine_game_stats(games)
for p in players.rushing().sort('rushing_yds').limit(5):
msg = '%s %d carries for %d yards and %d TDs'
print msg % (p, p.rushing_att, p.rushing_yds, p.rushing_tds)
print(msg % (p, p.rushing_att, p.rushing_yds, p.rushing_tds))

And the output is:

Expand All @@ -45,7 +45,7 @@
games = nflgame.games(2013, week=1)
plays = nflgame.combine_plays(games)
for p in plays.sort('passing_yds').limit(5):
print p
print(p)

And the output is:

Expand Down Expand Up @@ -80,20 +80,16 @@
We tend to respond fairly quickly!
"""

try:
from collections import OrderedDict
except:
from ordereddict import OrderedDict # from PyPI
import itertools

import nflgame.game
import nflgame.live
import nflgame.player
import nflgame.sched
import nflgame.seq
from nflgame.compat import itervalues, reduce
from nflgame.version import __version__

assert OrderedDict # Asserting the import for static analysis.
VERSION = __version__ # Deprecated. Backwards compatibility.

NoPlayers = nflgame.seq.GenPlayerStats(None)
Expand Down Expand Up @@ -159,7 +155,7 @@ def find(name, team=None):
If team is not None, it is used as an additional search constraint.
"""
hits = []
for player in players.itervalues():
for player in itervalues(players):
if player.name.lower() == name.lower():
if team is None or team.lower() == player.team.lower():
hits.append(player)
Expand Down Expand Up @@ -425,7 +421,7 @@ def _search_schedule(year, week=None, home=None, away=None, kind='REG',
(as opposed to waiting for a 404 error from NFL.com).
"""
infos = []
for info in nflgame.sched.games.itervalues():
for info in itervalues(nflgame.sched.games):
y, t, w = info['year'], info['season_type'], info['week']
h, a = info['home'], info['away']
if year is not None:
Expand Down
7 changes: 5 additions & 2 deletions nflgame/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ def connect():
send an alert for a while, you'll probably be disconnected from the SMTP
server. In which case, the connection setup will be executed again.)
"""

from __future__ import print_function

import smtplib
import sys

Expand Down Expand Up @@ -115,8 +118,8 @@ def google_voice_login(email, passwd):
global _voice

if not _gv_available:
print >> sys.stderr, "The pygooglevoice Python package is required " \
"in order to use Google Voice."
print('The pygooglevoice Python package is required in order to use '
'Google Voice.', file=sys.stderr)
return

_voice = googlevoice.Voice()
Expand Down
111 changes: 111 additions & 0 deletions nflgame/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
"""Python 2 & 3 compatibility layer for nflgame."""

import sys

from collections import namedtuple


IS_PY3 = sys.version_info[0] == 3


# ifilter
try:
from itertools import ifilter
except ImportError:
ifilter = filter # Python 3


# MAXINT
try:
from sys import maxint as MAXINT
except ImportError:
from sys import maxsize as MAXINT # Python 3


# OrderedDict
try:
from ordereddict import OrderedDict # from PyPI
except ImportError:
from collections import OrderedDict # Python 2.7 + Python 3


# reduce
try:
reduce = reduce
except NameError:
from functools import reduce # Python 3


# Asserting the imports for static analysis.
assert ifilter
assert MAXINT
assert OrderedDict
assert reduce


# urllib
urllib_ns = namedtuple('urllib', ('HTTPError', 'URLError', 'urlopen'))

try:
import urllib2
urllib = urllib_ns(urllib2.HTTPError, urllib2.URLError, urllib2.urlopen)
del urllib2
except ImportError:
# Python 3
from urllib import error as urllib_error, request as urllib_request
urllib = urllib_ns(urllib_error.HTTPError,
urllib_error.URLError,
urllib_request.urlopen)
del urllib_error, urllib_request


# Python 3
if IS_PY3:
# Constants
binary_type = bytes
text_type = str
range = range
input = input

# Dict iter functions
def iteritems(obj, **kwargs):
return iter(obj.items(**kwargs))

def iterkeys(obj, **kwargs):
return iter(obj.keys(**kwargs))

def itervalues(obj, **kwargs):
return iter(obj.values(**kwargs))
# Python 2
else:
# Constants
binary_type = str
input = raw_input # noqa
range = xrange # noqa
text_type = unicode # noqa

# Dict iter functions
def iteritems(obj, **kwargs):
return obj.iteritems(**kwargs)

def iterkeys(obj, **kwargs):
return obj.iterkeys(**kwargs)

def itervalues(obj, **kwargs):
return obj.itervalues(**kwargs)


# String functions
def force_binary(value, encoding='utf-8', errors='strict'):
if isinstance(value, binary_type):
return value
return value.encode(encoding, errors)


def force_text(value, encoding='utf-8', errors='strict'):
if isinstance(value, text_type):
return value
return value.decode(encoding, errors)


del namedtuple, urllib_ns
Loading