Skip to content

Commit

Permalink
Merge 6dd5db2 into 91db872
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Aug 23, 2018
2 parents 91db872 + 6dd5db2 commit 0da4325
Show file tree
Hide file tree
Showing 32 changed files with 1,529 additions and 1,120 deletions.
11 changes: 0 additions & 11 deletions .coveragerc

This file was deleted.

3 changes: 3 additions & 0 deletions .editorconfig
Expand Up @@ -5,3 +5,6 @@ trim_trailing_whitespace = true
charset = utf-8
indent_style = space
indent_size = 4

[*.{yml,yaml}]
indent_size = 2
8 changes: 0 additions & 8 deletions .flake8

This file was deleted.

28 changes: 28 additions & 0 deletions .pre-commit-config.yaml
@@ -0,0 +1,28 @@
repos:
- repo: local
hooks:
- id: isort
name: Enforce import order
language: system
entry: isort
args: [--apply, --recursive]
types: [python]
language_version: python
- repo: https://github.com/ambv/black
rev: stable
hooks:
- id: black
language_version: python
- repo: local
hooks:
- id: pylint
name: pylint check
language: system
entry: pylint
args: [--rcfile=pylintrc]
types: [python]
- id: flake8
name: flake8 check
language: system
entry: flake8
types: [python]
23 changes: 15 additions & 8 deletions .travis.yml
@@ -1,22 +1,29 @@
language: python
cache: pip
python:
- 3.6
- 3.5
- 3.4

matrix:
include:
- python: 3.6
env: SKIP_BLACK=false
- python: 3.5
env: SKIP_BLACK=true
- python: 3.4
env: SKIP_BLACK=true

before_install:
- pip install -U pip
- pip install -U -r test_requirements.txt

install: python setup.py develop

before_script:
- pip list

script:
- pip list
- pytest --verbose --cov=mtg_ssm
- pylint mtg_ssm
- pylint --disable=similarities,redefined-outer-name tests
- pytest
- '[ "$SKIP_BLACK" == "true" ] || black --check .'
- isort --check --diff --recursive
- flake8
- pylint mtg_ssm tests

after_script: coveralls
20 changes: 12 additions & 8 deletions mtg_ssm/mtg/card_db.py
Expand Up @@ -57,7 +57,7 @@ def load_mtg_json(self, mtg_json_data, include_online_only=False):
self.code_to_card_set[card_set.code] = card_set
self.setname_to_card_set[card_set.name] = card_set

for card_data in set_data['cards']:
for card_data in set_data["cards"]:
card = models.Card(self, card_data)
self.name_to_card[card.name] = card

Expand All @@ -66,12 +66,10 @@ def load_mtg_json(self, mtg_json_data, include_online_only=False):

def rebuild_indexes(self):
"""Rebuild the printing indexes."""
self.cards = sorted(
self.name_to_card.values(),
key=lambda card: card.name)
self.cards = sorted(self.name_to_card.values(), key=lambda card: card.name)
self.card_sets = sorted(
self.code_to_card_set.values(),
key=lambda cset: cset.release_date)
self.code_to_card_set.values(), key=lambda cset: cset.release_date
)

self.set_code_to_printings = collections.defaultdict(list)
self.card_name_to_printings = collections.defaultdict(list)
Expand All @@ -83,7 +81,12 @@ def rebuild_indexes(self):
# snnm == (set, name, number, multiverseid)
snnm_index_keys = {
# pylint: disable=line-too-long
(printing.set_code, printing.card_name, printing.set_number, printing.multiverseid),
(
printing.set_code,
printing.card_name,
printing.set_number,
printing.multiverseid,
),
(printing.set_code, printing.card_name, None, printing.multiverseid),
(printing.set_code, printing.card_name, printing.set_number, None),
(printing.set_code, printing.card_name, None, None),
Expand All @@ -101,4 +104,5 @@ def rebuild_indexes(self):
self.set_code_to_printing_to_row = {}
for set_code, printings in self.set_code_to_printings.items():
self.set_code_to_printing_to_row[set_code] = {
printing: i for i, printing in enumerate(printings)}
printing: i for i, printing in enumerate(printings)
}
65 changes: 37 additions & 28 deletions mtg_ssm/mtg/counts.py
Expand Up @@ -25,10 +25,7 @@
import enum
from typing import Mapping

NAME_SUBSTITUTIONS = [
('Ae', 'Æ'),
('Jo', 'Jö'),
]
NAME_SUBSTITUTIONS = [("Ae", "Æ"), ("Jo", "Jö")]
NAME_SUBSTITUTIONS += [(right, left) for (left, right) in NAME_SUBSTITUTIONS]


Expand All @@ -42,8 +39,9 @@ class UnknownPrintingError(Error):

class CountTypes(enum.Enum):
"""Enum for possible card printing types (normal, foil)."""
copies = 'copies'
foils = 'foils'

copies = "copies"
foils = "foils"


def new_print_counts() -> Mapping[str, Mapping[CountTypes, int]]:
Expand All @@ -53,21 +51,25 @@ def new_print_counts() -> Mapping[str, Mapping[CountTypes, int]]:

def find_printing(cdb, set_code, name, set_number, multiverseid, strict=True):
"""Attempt to find a CardPrinting from given parameters."""
print('Searching => Set: %s; Name: %s; Number: %s, Multiverse ID: %s' % (
set_code, name, set_number, multiverseid))
name = name or ''
print(
"Searching => Set: %s; Name: %s; Number: %s, Multiverse ID: %s"
% (set_code, name, set_number, multiverseid)
)
name = name or ""
names = [name]
for sub_from, sub_to in NAME_SUBSTITUTIONS:
if sub_from in name:
names.append(name.replace(sub_from, sub_to))
snnm_keys = []
for name_var in names:
snnm_keys.extend([
(set_code, name_var, set_number, multiverseid),
(set_code, name_var, None, multiverseid),
(set_code, name_var, set_number, None),
(set_code, name_var, None, None),
])
snnm_keys.extend(
[
(set_code, name_var, set_number, multiverseid),
(set_code, name_var, None, multiverseid),
(set_code, name_var, set_number, None),
(set_code, name_var, None, None),
]
)
printing = None
for snnm_key in snnm_keys:
found_printings = cdb.set_name_num_mv_to_printings.get(snnm_key, [])
Expand All @@ -79,15 +81,21 @@ def find_printing(cdb, set_code, name, set_number, multiverseid, strict=True):
printing = found_printings[0]
break
if printing is not None:
print('Found => Set: %s; Name: %s; Number: %s; Multiverse ID: %s' % (
printing.set_code, printing.card_name, printing.set_number,
printing.multiverseid))
print(
"Found => Set: %s; Name: %s; Number: %s; Multiverse ID: %s"
% (
printing.set_code,
printing.card_name,
printing.set_number,
printing.multiverseid,
)
)
return printing


def coerce_card_row(card_count):
"""Given a card_row dict, coerce types to match desired input."""
int_keys = ['multiverseid'] + [ct.name for ct in CountTypes]
int_keys = ["multiverseid"] + [ct.name for ct in CountTypes]
for key in int_keys:
try:
card_count[key] = int(card_count[key])
Expand All @@ -103,21 +111,22 @@ def aggregate_print_counts(cdb, card_rows, strict):
if not any(card_row.get(ct.name) for ct in CountTypes):
continue
card_row = coerce_card_row(card_row)
printing_id = card_row.get('id')
printing_id = card_row.get("id")
printing = cdb.id_to_printing.get(printing_id)
if printing is None:
print('printing not found by id, searching')
print("printing not found by id, searching")
printing = find_printing(
cdb=cdb,
set_code=card_row.get('set'),
name=card_row.get('name'),
set_number=card_row.get('number'),
multiverseid=card_row.get('multiverseid'),
strict=strict)
set_code=card_row.get("set"),
name=card_row.get("name"),
set_number=card_row.get("number"),
multiverseid=card_row.get("multiverseid"),
strict=strict,
)
if printing is None:
raise UnknownPrintingError(
'Could not match id to known printing from counts: %r' %
card_row)
"Could not match id to known printing from counts: %r" % card_row
)
for count_type in CountTypes:
ct_name = count_type.name
count = card_row.get(ct_name)
Expand Down
66 changes: 38 additions & 28 deletions mtg_ssm/mtg/models.py
Expand Up @@ -4,19 +4,20 @@
import string
import weakref

VARIANT_CHARS = (string.ascii_letters + '★')
STRICT_BASICS = {'Plains', 'Island', 'Swamp', 'Mountain', 'Forest'}
VARIANT_CHARS = string.ascii_letters + "★"
STRICT_BASICS = {"Plains", "Island", "Swamp", "Mountain", "Forest"}


class Card:
"""Model for storing card information."""
__slots__ = ('cdb', 'name', 'layout', 'names')

__slots__ = ("cdb", "name", "layout", "names")

def __init__(self, card_db, card_data):
self.cdb = weakref.proxy(card_db)
self.name = card_data['name']
self.layout = card_data['layout']
self.names = card_data.get('names', [self.name])
self.name = card_data["name"]
self.layout = card_data["layout"]
self.names = card_data.get("names", [self.name])

@property
def strict_basic(self) -> bool:
Expand All @@ -29,27 +30,36 @@ def printings(self):
return self.cdb.card_name_to_printings[self.name]

def __str__(self) -> str:
return 'Card: {card.name}'.format(card=self)
return "Card: {card.name}".format(card=self)

def __repr__(self) -> str:
return '<Card: {card.name}>'.format(card=self)
return "<Card: {card.name}>".format(card=self)


class CardPrinting:
"""Model for storing information about card printings."""

__slots__ = ('cdb', 'id_', 'card_name', 'set_code', 'set_number',
'set_integer', 'set_variant', 'multiverseid', 'artist',
'counts')
__slots__ = (
"cdb",
"id_",
"card_name",
"set_code",
"set_number",
"set_integer",
"set_variant",
"multiverseid",
"artist",
"counts",
)

def __init__(self, card_db, set_code, card_data):
self.cdb = weakref.proxy(card_db)
self.id_ = card_data['id']
self.card_name = card_data['name']
self.id_ = card_data["id"]
self.card_name = card_data["name"]
self.set_code = set_code
self.set_number = card_data.get('number')
self.multiverseid = card_data.get('multiverseid')
self.artist = card_data['artist']
self.set_number = card_data.get("number")
self.multiverseid = card_data.get("multiverseid")
self.artist = card_data["artist"]

if self.set_number is None:
self.set_integer = None # type: int
Expand All @@ -69,10 +79,10 @@ def set(self):
return self.cdb.code_to_card_set[self.set_code]

def __str__(self):
return 'CardPrinting: {print.id_}'.format(print=self)
return "CardPrinting: {print.id_}".format(print=self)

def __repr__(self):
return '<CardPrinting: {print.id_}>'.format(print=self)
return "<CardPrinting: {print.id_}>".format(print=self)

def __hash__(self):
return hash(self.id_)
Expand All @@ -84,18 +94,18 @@ def __eq__(self, other):
class CardSet:
"""Model for storing card set information."""

__slots__ = ('cdb', 'code', 'name', 'block', 'release_date',
'type_', 'online_only')
__slots__ = ("cdb", "code", "name", "block", "release_date", "type_", "online_only")

def __init__(self, card_db, set_data):
self.cdb = weakref.proxy(card_db)
self.code = set_data['code']
self.name = set_data['name']
self.block = set_data.get('block')
self.code = set_data["code"]
self.name = set_data["name"]
self.block = set_data.get("block")
self.release_date = dt.datetime.strptime(
set_data['releaseDate'], '%Y-%m-%d').date()
self.type_ = set_data['type']
self.online_only = set_data.get('onlineOnly', False)
set_data["releaseDate"], "%Y-%m-%d"
).date()
self.type_ = set_data["type"]
self.online_only = set_data.get("onlineOnly", False)

@property
def printings(self):
Expand All @@ -112,7 +122,7 @@ def printing_index(self, printing):
return self.cdb.set_code_to_printing_to_row[self.code][printing]

def __str__(self):
return 'CardSet: {cset.name}'.format(cset=self)
return "CardSet: {cset.name}".format(cset=self)

def __repr__(self):
return '<CardSet: {cset.code}>'.format(cset=self)
return "<CardSet: {cset.code}>".format(cset=self)

0 comments on commit 0da4325

Please sign in to comment.