Skip to content

Commit

Permalink
Merge 10746f7 into 1e12cc9
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Oct 25, 2023
2 parents 1e12cc9 + 10746f7 commit dc595b0
Show file tree
Hide file tree
Showing 28 changed files with 224 additions and 331 deletions.
18 changes: 8 additions & 10 deletions .pre-commit-config.yaml
Expand Up @@ -8,21 +8,19 @@ repos:
language: system
require_serial: true
types: [python]
- id: isort
name: Check import order
language: system
entry: isort
types: [python]
- id: pylint
name: Static analysis from pylint
language: system
entry: pylint
types: [python]
- id: mypy
name: Type checking from mypy
language: system
entry: mypy
types: [python]
- id: ruff
name: ruff
description: "Run ruff on files"
entry: ruff check --force-exclude
language: system
types_or: [python, pyi]
args: [--fix, --exit-non-zero-on-fix]
require_serial: true
- id: doc8-python
name: Documentation checking from doc8 (Python)
language: system
Expand Down
1 change: 1 addition & 0 deletions TODO.md
Expand Up @@ -8,3 +8,4 @@
- Update card_count for sets when processing bundle filters
- Can we read images from icon_svg_uri in sets, rasterize, and
insert the set icons in the xlsx "All Sets" page?
- Can we switch to hatchling as our build system?
5 changes: 3 additions & 2 deletions mtg_ssm/containers/bundles.py
Expand Up @@ -20,7 +20,7 @@ class ScryfallDataSet(NamedTuple):
migrations: List[ScryMigration]


def filter_cards_and_sets(
def filter_cards_and_sets( # noqa: C901
scryfall_data: ScryfallDataSet,
*,
exclude_set_types: Optional[Set[ScrySetType]] = None,
Expand Down Expand Up @@ -49,7 +49,8 @@ def filter_cards_and_sets(

accepted_cards = []
nonempty_setcodes = set()
for card in scryfall_data.cards:
for card_loop in scryfall_data.cards:
card = card_loop # capture loop variable
while card.set in remapped_setcodes:
collector_number = card.collector_number
if collector_number.isdigit():
Expand Down
6 changes: 4 additions & 2 deletions mtg_ssm/containers/collection.py
Expand Up @@ -2,6 +2,8 @@

from dataclasses import dataclass

from typing_extensions import Self

from mtg_ssm.containers import counts
from mtg_ssm.containers.counts import ScryfallCardCount
from mtg_ssm.containers.indexes import Oracle
Expand All @@ -22,7 +24,7 @@ def __add__(self, other: "MagicCollection") -> "MagicCollection":
counts=counts.merge_card_counts(self.counts, other.counts),
)

def __iadd__(self, other: "MagicCollection") -> "MagicCollection":
def __iadd__(self, other: "MagicCollection") -> Self:
if not isinstance(other, MagicCollection):
return NotImplemented
self.counts = counts.merge_card_counts(self.counts, other.counts)
Expand All @@ -36,7 +38,7 @@ def __sub__(self, other: "MagicCollection") -> "MagicCollection":
counts=counts.diff_card_counts(self.counts, other.counts),
)

def __isub__(self, other: "MagicCollection") -> "MagicCollection":
def __isub__(self, other: "MagicCollection") -> Self:
if not isinstance(other, MagicCollection):
return NotImplemented
self.counts = counts.diff_card_counts(self.counts, other.counts)
Expand Down
12 changes: 5 additions & 7 deletions mtg_ssm/containers/counts.py
Expand Up @@ -33,7 +33,8 @@ def aggregate_card_counts(
) -> ScryfallCardCount:
"""Extract card counts from card rows."""
card_counts: ScryfallCardCount = {}
for card_row in card_rows:
for card_row_loop in card_rows:
card_row = card_row_loop # capture loop variable
if "scryfall_id" not in card_row:
card_row = legacy.coerce_row(card_row, oracle)
if not card_row:
Expand All @@ -50,9 +51,8 @@ def aggregate_card_counts(
while scryfall_id in oracle.index.migrate_old_id_to_new_id:
scryfall_id = oracle.index.migrate_old_id_to_new_id[scryfall_id]
if scryfall_id not in oracle.index.id_to_card:
raise CardNotFoundError(
f"Found counts for card={scryfall_id} not found scryfall data"
)
msg = f"Found counts for card={scryfall_id} not found scryfall data"
raise CardNotFoundError(msg)
card_counts[scryfall_id] = counts
return card_counts

Expand All @@ -66,9 +66,7 @@ def merge_card_counts(*card_counts_args: ScryfallCardCount) -> ScryfallCardCount
return dict(merged_counts)


def diff_card_counts(
left: ScryfallCardCount, right: ScryfallCardCount
) -> ScryfallCardCount:
def diff_card_counts(left: ScryfallCardCount, right: ScryfallCardCount) -> ScryfallCardCount:
"""Subtract right print counts from left print counts."""
diffed_counts: ScryfallCardCount = collections.defaultdict(dict)
for card_id in left.keys() | right.keys():
Expand Down
4 changes: 1 addition & 3 deletions mtg_ssm/containers/indexes.py
Expand Up @@ -73,9 +73,7 @@ def load_data(self, scrydata: ScryfallDataSet) -> None:

self.snnma_to_id = collections.defaultdict(set)

name_to_unsorted_cards: Dict[str, List[ScryCard]] = collections.defaultdict(
list
)
name_to_unsorted_cards: Dict[str, List[ScryCard]] = collections.defaultdict(list)
setcode_to_unsorted_cards: Dict[str, List[ScryCard]] = {}

for set_ in scrydata.sets:
Expand Down
22 changes: 11 additions & 11 deletions mtg_ssm/containers/legacy.py
Expand Up @@ -102,20 +102,18 @@ def extract_counts(card_row: Dict[str, Any]) -> Dict[str, int]:
def find_scryfall_id(card_row: Dict[str, str], oracle: Oracle) -> UUID:
"""Heuristically determine the scryfall id for a given input row."""
set_code = card_row.get("set", "")
set_codes = [set_code, set_code.lower()] + OTHER_SET_CODE_TO_SET_CODE.get(
set_code, []
)
set_codes = [
set_code,
set_code.lower(),
*OTHER_SET_CODE_TO_SET_CODE.get(set_code, []),
]
name = card_row.get("name", "")
collector_number = card_row.get("number") or None
mvid = int(card_row.get("multiverseid") or -1)
artist = card_row.get("artist") or None
artist = PSUDONYM_TO_ARTIST.get(artist, artist)
print(
f"Searching => Set: {set_code}; Name: {name}; Number: {collector_number}; MVID: {mvid}"
)
snnma_keys: List[
Tuple[Optional[str], str, Optional[str], Optional[int], Optional[str]]
] = []
print(f"Searching => Set: {set_code}; Name: {name}; Number: {collector_number}; MVID: {mvid}")
snnma_keys: List[Tuple[Optional[str], str, Optional[str], Optional[int], Optional[str]]] = []
for set_ in set_codes:
snnma_keys += [
(set_, name, collector_number, None, None),
Expand All @@ -141,8 +139,10 @@ def find_scryfall_id(card_row: Dict[str, str], oracle: Oracle) -> UUID:
)
return scryfall_id
if seen:
raise MultipleMatchError(f"Could not find scryfall card for row: {card_row}")
raise NoMatchError(f"Could not find scryfall card for row: {card_row}")
msg = f"Could not find scryfall card for row: {card_row}"
raise MultipleMatchError(msg)
msg = f"Could not find scryfall card for row: {card_row}"
raise NoMatchError(msg)


def coerce_row(card_row: Dict[str, Any], oracle: Oracle) -> Dict[str, Any]:
Expand Down
17 changes: 5 additions & 12 deletions mtg_ssm/scryfall/fetcher.py
Expand Up @@ -28,7 +28,7 @@
is_binary=True,
)
SESSION = CachedSession(
os.path.join(CACHE_DIR, "requests_cache.sqlite"),
os.path.join(CACHE_DIR, "requests_cache.sqlite"), # noqa: PTH118
backend="sqlite",
serializer=CACHE_SERIALIZER,
cache_control=True,
Expand All @@ -51,7 +51,7 @@ def _fetch_endpoint(endpoint: str) -> bytes:
return response.content


def scryfetch() -> ScryfallDataSet: # pylint: disable=too-many-locals
def scryfetch() -> ScryfallDataSet:
"""Retrieve and deserialize Scryfall object data."""
print("Reading data from scryfall")
bulk_data = msgspec.json.decode(
Expand All @@ -66,9 +66,7 @@ def scryfetch() -> ScryfallDataSet: # pylint: disable=too-many-locals
sets_data += sets_list.data

scrylistmigration_decoder = msgspec.json.Decoder(ScryList[ScryMigration])
migrations_list = scrylistmigration_decoder.decode(
_fetch_endpoint(MIGRATIONS_ENDPOINT)
)
migrations_list = scrylistmigration_decoder.decode(_fetch_endpoint(MIGRATIONS_ENDPOINT))
migrations_data = migrations_list.data
while migrations_list.has_more and migrations_list.next_page is not None:
migrations_list = scrylistmigration_decoder.decode(
Expand All @@ -77,11 +75,6 @@ def scryfetch() -> ScryfallDataSet: # pylint: disable=too-many-locals
migrations_data += migrations_list.data

[cards_endpoint] = [bd.download_uri for bd in bulk_data if bd.type == BULK_TYPE]
cards_data = msgspec.json.decode(
_fetch_endpoint(cards_endpoint), type=List[ScryCard]
)
cards_data = msgspec.json.decode(_fetch_endpoint(cards_endpoint), type=List[ScryCard])

scryfall_data = ScryfallDataSet(
sets=sets_data, cards=cards_data, migrations=migrations_data
)
return scryfall_data
return ScryfallDataSet(sets=sets_data, cards=cards_data, migrations=migrations_data)

0 comments on commit dc595b0

Please sign in to comment.