Skip to content

Commit

Permalink
Merge pull request #197 from jwodder/master
Browse files Browse the repository at this point in the history
Python 3 and Pyflakes fixes
  • Loading branch information
eevee committed Nov 25, 2016
2 parents 8233588 + c3f566b commit 3a75992
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 46 deletions.
3 changes: 2 additions & 1 deletion pokedex/compatibility.py
Expand Up @@ -3,6 +3,7 @@
Currently these are functions missing from Python 2.5.
"""
from __future__ import print_function
import six

try:
from itertools import permutations
Expand Down Expand Up @@ -67,7 +68,7 @@ def namedtuple(typename, field_names, verbose=False, rename=False):

# Parse and validate the field names. Validation serves two purposes,
# generating informative error messages and preventing template injection attacks.
if isinstance(field_names, basestring):
if isinstance(field_names, six.string_types):
field_names = field_names.replace(',', ' ').split() # names separated by whitespace and/or commas
field_names = tuple(map(str, field_names))
if rename:
Expand Down
4 changes: 2 additions & 2 deletions pokedex/db/__init__.py
Expand Up @@ -4,7 +4,7 @@
from sqlalchemy import engine_from_config, orm

from ..defaults import get_default_db_uri
from .tables import Language, metadata
from .tables import metadata
from .multilang import MultilangSession, MultilangScopedSession

ENGLISH_ID = 9
Expand Down Expand Up @@ -46,7 +46,7 @@ def connect(uri=None, session_args={}, engine_args={}, engine_prefix=''):
### Connect
engine_args[engine_prefix + 'url'] = uri
engine = engine_from_config(engine_args, prefix=engine_prefix)
conn = engine.connect()
engine.connect()
metadata.bind = engine

all_session_args = dict(autoflush=True, autocommit=False, bind=engine)
Expand Down
5 changes: 2 additions & 3 deletions pokedex/db/load.py
Expand Up @@ -3,7 +3,6 @@

import csv
import fnmatch
import io
import os.path
import sys

Expand All @@ -12,7 +11,7 @@
import sqlalchemy.types

import pokedex
from pokedex.db import metadata, tables, translations
from pokedex.db import metadata, translations
from pokedex.defaults import get_default_csv_dir
from pokedex.db.dependencies import find_dependent_tables
from pokedex.db.oracle import rewrite_long_table_names
Expand Down Expand Up @@ -323,7 +322,7 @@ def insert_and_commit():
# Could happen if row A refers to B which refers to C.
# This is ridiculous and doesn't happen in my data so far
raise ValueError("Too many levels of self-reference! "
"Row was: " + str(row))
"Row was: " + str(row_data))

session.execute(
insert_stmt.values(**row_data)
Expand Down
2 changes: 1 addition & 1 deletion pokedex/db/markdown.py
Expand Up @@ -174,7 +174,7 @@ def __init__(self, factory, session, string_language=None, game_language=None):
self.game_language = game_language

def handleMatch(self, m):
from pokedex.db import tables, util
from pokedex.db import tables
start, label, category, target, end = m.groups()
try:
table = dict(
Expand Down
4 changes: 1 addition & 3 deletions pokedex/db/multilang.py
@@ -1,7 +1,5 @@
from functools import partial

from sqlalchemy.ext.associationproxy import association_proxy, AssociationProxy
from sqlalchemy.orm import Query, aliased, mapper, relationship, synonym
from sqlalchemy.orm import Query, mapper, relationship, synonym
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.orm.scoping import ScopedSession
from sqlalchemy.orm.session import Session, object_session
Expand Down
18 changes: 9 additions & 9 deletions pokedex/db/tables.py
Expand Up @@ -26,18 +26,15 @@
"""
# XXX: Check if "gametext" is set correctly everywhere

import collections
from functools import partial
import six

from sqlalchemy import Column, ForeignKey, MetaData, PrimaryKeyConstraint, Table, UniqueConstraint
from sqlalchemy import Column, ForeignKey, MetaData, PrimaryKeyConstraint, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base, DeclarativeMeta
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.orm import backref, relationship
from sqlalchemy.orm.session import Session
from sqlalchemy.orm.interfaces import AttributeExtension
from sqlalchemy.sql import and_, or_
from sqlalchemy.schema import ColumnDefault
from sqlalchemy.sql import and_
from sqlalchemy.types import Boolean, Enum, Integer, SmallInteger, Unicode, UnicodeText

from pokedex.db import markdown, multilang
Expand All @@ -56,18 +53,21 @@ def __unicode__(self):
if not pk_constraint:
return u"<%s object at %x>" % (typename, id(self))

pk = u', '.join(unicode(getattr(self, column.name))
pk = u', '.join(six.text_type(getattr(self, column.name))
for column in pk_constraint.columns)
try:
return u"<%s object (%s): %s>" % (typename, pk, self.identifier)
except AttributeError:
return u"<%s object (%s)>" % (typename, pk)

def __str__(self):
return unicode(self).encode('utf8')
if six.PY2:
return six.text_type(self).encode('utf8')
else:
return type(self).__unicode__(self)

def __repr__(self):
return unicode(self).encode('utf8')
return str(self)

mapped_classes = []
class TableMetaclass(DeclarativeMeta):
Expand Down
9 changes: 6 additions & 3 deletions pokedex/db/translations.py
Expand Up @@ -30,6 +30,7 @@
import re
from collections import defaultdict

import six
from six.moves import zip

from pokedex.db import tables
Expand Down Expand Up @@ -155,10 +156,13 @@ def __unicode__(self):
return template.format(self=self, string=string)

def __str__(self):
return unicode(self).encode('utf-8')
if six.PY2:
return six.text_type(self).encode('utf8')
else:
return type(self).__unicode__(self)

def __repr__(self):
return unicode(self).encode('utf-8')
return str(self)

class Translations(object):
"""Data and opertaions specific to a location on disk (and a source language)
Expand Down Expand Up @@ -648,7 +652,6 @@ def match_to_source(source, *translations):
if first or match:
best_string = current_string
best_crc = current_crc
best_message = translation
if match:
break
first = False
Expand Down
2 changes: 0 additions & 2 deletions pokedex/db/util.py
Expand Up @@ -9,8 +9,6 @@
from sqlalchemy.sql.functions import coalesce
from sqlalchemy.orm.exc import NoResultFound

from pokedex.db import tables

### Getter

def get(session, table, identifier=None, name=None, id=None, language=None):
Expand Down
17 changes: 7 additions & 10 deletions pokedex/doc/tabledoc.py
Expand Up @@ -9,19 +9,17 @@

import functools
import textwrap
import six

from docutils import nodes
from docutils.statemachine import ViewList
from sphinx.util.compat import Directive, make_admonition
from sphinx.locale import _
from sphinx.domains.python import PyClasslike
from sphinx.util.docfields import Field, GroupedField, TypedField
from sphinx.ext.autodoc import ClassLevelDocumenter
from sphinx.util.docfields import TypedField

from sqlalchemy import types
from sqlalchemy.orm.attributes import InstrumentedAttribute
from sqlalchemy.orm.properties import RelationshipProperty
from sqlalchemy.orm import Mapper, configure_mappers
from sqlalchemy.orm import configure_mappers
from sqlalchemy.ext.associationproxy import AssociationProxy
from pokedex.db.markdown import MoveEffectPropertyMap, MoveEffectProperty

Expand Down Expand Up @@ -128,7 +126,7 @@ def wrapped(cls, remaining_attrs):
### Section generation functions

def generate_table_header(cls, remaining_attrs):
first_line, sep, next_lines = unicode(cls.__doc__).partition(u'\n')
first_line, sep, next_lines = six.text_type(cls.__doc__).partition(u'\n')
yield first_line
for line in textwrap.dedent(next_lines).split('\n'):
yield line
Expand Down Expand Up @@ -184,7 +182,7 @@ def generate_columns(cls, remaining_attrs):
yield column_header(c, name) + ':'
yield u''
if c.doc:
yield u' ' + unicode(c.doc)
yield u' ' + six.text_type(c.doc)
yield u''

@with_header(u'Internationalized strings')
Expand All @@ -200,7 +198,7 @@ def generate_strings(cls, remaining_attrs):
translation_class.__table__.name)
yield u''
if c.doc:
yield u' ' + unicode(c.doc)
yield u' ' + six.text_type(c.doc)
yield u''

@with_header(u'Relationships')
Expand All @@ -220,7 +218,7 @@ def isrelationship(prop):
yield u'(→ %s)' % class_name
if rel.doc:
yield u''
yield u' ' + unicode(rel.doc)
yield u' ' + six.text_type(rel.doc)
if rel.secondary is not None:
yield u''
yield ' Association table: ``%s``' % rel.secondary
Expand Down Expand Up @@ -299,7 +297,6 @@ def before_content(self):
break
else:
raise ValueError('Table %s not found' % name)
table = cls.__table__

remaining_attrs = set(x for x in dir(cls) if not x.startswith('_'))
remaining_attrs.difference_update(['metadata', 'translation_classes',
Expand Down
4 changes: 2 additions & 2 deletions pokedex/formulae.py
Expand Up @@ -2,7 +2,7 @@
"""Faithful translations of calculations the games make."""
from __future__ import division

from itertools import izip
from six.moves import reduce, xrange, zip

def nCr(n, r):
"""n-choose-r.
Expand All @@ -13,7 +13,7 @@ def nCr(n, r):

return reduce(
lambda x, y: x * y[0] / y[1],
izip(xrange(n - r + 1, n + 1),
zip(xrange(n - r + 1, n + 1),
xrange(1, r + 1)),
1)

Expand Down
4 changes: 3 additions & 1 deletion pokedex/struct/_pokemon_struct.py
Expand Up @@ -6,7 +6,9 @@

import datetime

from construct import *
from construct import (Adapter, BitField, BitStruct, Buffered,
EmbeddedBitStruct, Enum, Flag, Padding, String, Struct,
ULInt16, ULInt32, ULInt8)

# TODO:
# - strings should be validated, going both in and out
Expand Down
2 changes: 1 addition & 1 deletion pokedex/tests/test_database_sanity.py
Expand Up @@ -5,7 +5,7 @@
from sqlalchemy.orm.exc import NoResultFound
from sqlalchemy.sql import func

from pokedex.db import connect, tables, util
from pokedex.db import tables, util

def test_encounter_slots(session):
"""Encounters have a version, which has a version group; encounters also
Expand Down
5 changes: 2 additions & 3 deletions pokedex/tests/test_schema.py
Expand Up @@ -3,11 +3,10 @@
import pytest

from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.orm import class_mapper, joinedload, sessionmaker
from sqlalchemy.orm.session import Session
from sqlalchemy.orm import joinedload, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

from pokedex.db import tables, markdown
from pokedex.db import tables
from pokedex.db.multilang import MultilangScopedSession, MultilangSession, \
create_translation_table

Expand Down
2 changes: 0 additions & 2 deletions pokedex/tests/test_strings.py
Expand Up @@ -3,8 +3,6 @@
import pytest
parametrize = pytest.mark.parametrize

from sqlalchemy.orm.exc import NoResultFound

from pokedex.db import tables, connect, util, markdown

@pytest.fixture(scope="module")
Expand Down
2 changes: 1 addition & 1 deletion pokedex/tests/test_util.py
Expand Up @@ -3,7 +3,7 @@
import pytest
parametrize = pytest.mark.parametrize

from pokedex.db import connect, tables, util
from pokedex.db import tables, util

def test_get_item_identifier(session):
item = util.get(session, tables.Item, identifier='master-ball')
Expand Down
5 changes: 3 additions & 2 deletions pokedex/util/media.py
Expand Up @@ -32,6 +32,7 @@

import os
from functools import partial
import six

class MediaFile(object):
"""Represents a file: picture, sound, etc.
Expand Down Expand Up @@ -83,7 +84,7 @@ def __str__(self):

class BaseMedia(object):
def __init__(self, root):
if isinstance(root, basestring):
if isinstance(root, six.string_types):
self.file_class = partial(MediaFile, root)
else:
self.file_class = root
Expand Down Expand Up @@ -179,7 +180,7 @@ def sprite(self,
If the sprite is not found, raise a ValueError.
"""
if isinstance(version, basestring):
if isinstance(version, six.string_types):
version_dir = version
try:
generation, info = self._pokemon_sprite_info[version_dir]
Expand Down

0 comments on commit 3a75992

Please sign in to comment.