Skip to content

Commit

Permalink
Improve performance slightly, mostly through lxml
Browse files Browse the repository at this point in the history
  • Loading branch information
gwax committed Jan 28, 2017
1 parent eb87f59 commit 7930d9a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
12 changes: 6 additions & 6 deletions mtg_ssm/mtg/card_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ def load_mtg_json(self, mtg_json_data, include_online_only=False):

def rebuild_indexes(self):
"""Rebuild the printing indexes."""
self.cards = list(self.name_to_card.values())
self.card_sets = list(self.code_to_card_set.values())
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.set_code_to_printings = collections.defaultdict(list)
self.card_name_to_printings = collections.defaultdict(list)
Expand All @@ -87,10 +91,6 @@ def rebuild_indexes(self):
for key in snnm_index_keys:
self.set_name_num_mv_to_printings[key].append(printing)

# Sort indexes
self.cards.sort(key=lambda card: card.name)
self.card_sets.sort(key=lambda cset: cset.release_date)

for printings in self.set_code_to_printings.values():
printings.sort(key=set_code_to_printings_key)

Expand Down
14 changes: 6 additions & 8 deletions mtg_ssm/mtg/counts.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ class CountTypes(enum.Enum):

def new_print_counts() -> Mapping[str, Mapping[CountTypes, int]]:
"""Get an appropriate defaultdict set up for use ase print counts."""
return collections.defaultdict(
lambda: collections.defaultdict(int))
return collections.defaultdict(collections.Counter)


def find_printing(cdb, set_code, name, set_number, multiverseid, strict=True):
Expand Down Expand Up @@ -125,12 +124,11 @@ def aggregate_print_counts(cdb, card_rows, strict):

def merge_print_counts(*print_counts_args):
"""Merge two sets of print_counts."""
print_counts = new_print_counts()
for in_print_counts in print_counts_args:
for print_id, counts in in_print_counts.items():
for key, value in counts.items():
print_counts[print_id][key] += value
return print_counts
merged_counts = new_print_counts()
for print_counts in print_counts_args:
for print_id, counts in print_counts.items():
merged_counts[print_id].update(counts)
return merged_counts


def diff_print_counts(left, right):
Expand Down
1 change: 0 additions & 1 deletion mtg_ssm/mtgjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import requests
import requests.exceptions


MTGJSON_ADDRESS = 'http://mtgjson.com/json/'
VERSION_FILENAME = 'version-full.json'
ALLSETS_FILENAME = 'AllSets-x.json.zip'
Expand Down
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@
'Topic :: Games/Entertainment',
]

EXTRAS = {
'lxml': 'lxml>=3.7.2',
}

setuptools.setup(
name='mtg_ssm',
version=__version__,
Expand All @@ -61,6 +65,7 @@
install_requires=DEPENDENCIES,
setup_requires=SETUP_DEPENDENCIES,
tests_require=TEST_DEPENDENCIES,
extras_require=EXTRAS,
entry_points={
'console_scripts': [
'mtg-ssm = mtg_ssm.ssm:main',
Expand Down

0 comments on commit 7930d9a

Please sign in to comment.