From ec2cf1a0a70438eaed3d011d4b473b7da7ae2775 Mon Sep 17 00:00:00 2001 From: Nanda Scott Date: Wed, 18 Apr 2018 20:55:11 -0400 Subject: [PATCH 1/2] Updated key check functions for less spaghetti, also fixed sets and symbology to be consistent. --- Changelog.md | 8 +++++ scrython/cards/cards_object.py | 8 ++--- scrython/catalog/catalogs_object.py | 4 +-- scrython/rulings/rulings_object.py | 8 ++--- scrython/sets/sets.py | 48 ++++++++++---------------- scrython/sets/sets_object.py | 6 +++- scrython/symbology/parse_mana.py | 20 ++++------- scrython/symbology/symbology.py | 36 +++++++------------ scrython/symbology/symbology_object.py | 8 +++++ 9 files changed, 63 insertions(+), 83 deletions(-) diff --git a/Changelog.md b/Changelog.md index 775fbca..79890ab 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,13 @@ # Changelog +## 1.5.0 + +Changes + +- Key errors are now handled more cleanly, and doesn't return two traceback errors. +- Updated Sets to be more like the other classes in structure. +- Updated symbology to be more like the other classes in structure. + ## 1.4.2 Bugfixes diff --git a/scrython/cards/cards_object.py b/scrython/cards/cards_object.py index 4047a6a..5453ffb 100644 --- a/scrython/cards/cards_object.py +++ b/scrython/cards/cards_object.py @@ -103,15 +103,11 @@ class CardsObject(object): raise Exception(self.scryfallJson['details']) def _checkForKey(self, key): - try: - return self.scryfallJson[key] - except Exception: + if not key in self.scryfallJson: raise KeyError('This card has no key \'{}\''.format(key)) def _checkForTupleKey(self, parent, num, key): - try: - return self.scryfallJson[parent][num][key] - except Exception: + if not key in self.scryfallJson[parent][num]: raise KeyError('This tuple has no key \'{}\''.format(key)) def object(self): diff --git a/scrython/catalog/catalogs_object.py b/scrython/catalog/catalogs_object.py index fe7bd37..fc564e8 100644 --- a/scrython/catalog/catalogs_object.py +++ b/scrython/catalog/catalogs_object.py @@ -46,9 +46,7 @@ class CatalogsObject(object): raise Exception(self.scryfallJson['details']) def _checkForKey(self, key): - try: - return self.scryfallJson[key] - except Exception: + if not key in self.scryfallJson: raise KeyError('This card has no key \'{}\''.format(key)) def object(self): diff --git a/scrython/rulings/rulings_object.py b/scrython/rulings/rulings_object.py index e1c5272..2d4100f 100644 --- a/scrython/rulings/rulings_object.py +++ b/scrython/rulings/rulings_object.py @@ -60,15 +60,11 @@ class RulingsObject(object): raise Exception(self.scryfallJson['details']) def _checkForKey(self, key): - try: - return self.scryfallJson[key] - except Exception: + if not key in self.scryfallJson: raise KeyError('This object has no key \'{}\''.format(key)) def _checkForTupleKey(self, parent, num, key): - try: - return self.scryfallJson[parent][num][key] - except Exception: + if not key in self.scryfallJson[parent][num]: raise KeyError('This ruling has no key \'{}\''.format(key)) def object(self): diff --git a/scrython/sets/sets.py b/scrython/sets/sets.py index fb88bb9..9c4ffe3 100644 --- a/scrython/sets/sets.py +++ b/scrython/sets/sets.py @@ -41,105 +41,93 @@ class Sets(SetsObject): self._url = 'sets?' super(Sets, self).__init__(self._url) - def _checkForKey(self, key): - try: - return self.scryfallJson[key] - except Exception: - raise KeyError('This object has no key \'{}\''.format(key)) - - def _checkForTupleKey(self, parent, num, key): - try: - return self.scryfallJson[parent][num][key] - except Exception: - raise KeyError('This object has no key \'{}\''.format(key)) - def object(self): - self._checkForKey('object') + super(Sets, self)._checkForKey('object') return self.scryfallJson['object'] def has_more(self): - self._checkForKey('has_more') + super(Sets, self)._checkForKey('has_more') return self.scryfallJson['has_more'] def data(self): - self._checkForKey('data') + super(Sets, self)._checkForKey('data') return self.scryfallJson['data'] def data_length(self): - self._checkForKey('data') + super(Sets, self)._checkForKey('data') return len(self.scryfallJson['data']) def set_object(self, num): - self._checkForTupleKey('data', num, 'object') + super(Sets, self)._checkForTupleKey('data', num, 'object') return self.scryfallJson['data'][num]['object'] def set_code(self, num): - self._checkForTupleKey('data', num, 'code') + super(Sets, self)._checkForTupleKey('data', num, 'code') return self.scryfallJson['data'][num]['code'] def set_mtgo_code(self, num): - self._checkForTupleKey('data', num, 'mtgo_code') + super(Sets, self)._checkForTupleKey('data', num, 'mtgo_code') return self.scryfallJson['data'][num]['mtgo_code'] def set_name(self, num): - self._checkForTupleKey('data', num, 'name') + super(Sets, self)._checkForTupleKey('data', num, 'name') return self.scryfallJson['data'][num]['name'] def set_set_type(self, num): - self._checkForTupleKey('data', num, 'set_type') + super(Sets, self)._checkForTupleKey('data', num, 'set_type') return self.scryfallJson['data'][num]['set_type'] def set_released_at(self, num): - self._checkForTupleKey('data', num, 'released_at') + super(Sets, self)._checkForTupleKey('data', num, 'released_at') return self.scryfallJson['data'][num]['released_at'] def set_block_code(self, num): - self._checkForTupleKey('data', num, 'block_code') + super(Sets, self)._checkForTupleKey('data', num, 'block_code') return self.scryfallJson['data'][num]['block_code'] def set_block(self, num): - self._checkForTupleKey('data', num, 'block') + super(Sets, self)._checkForTupleKey('data', num, 'block') return self.scryfallJson['data'][num]['block'] def set_parent_set_code(self, num): - self._checkForTupleKey('data', num, 'parent_set_code') + super(Sets, self)._checkForTupleKey('data', num, 'parent_set_code') return self.scryfallJson['data'][num]['parent_set_code'] def set_card_count(self, num): - self._checkForTupleKey('data', num, 'card_count') + super(Sets, self)._checkForTupleKey('data', num, 'card_count') return self.scryfallJson['data'][num]['card_count'] def set_digital(self, num): - self._checkForTupleKey('data', num, 'digital') + super(Sets, self)._checkForTupleKey('data', num, 'digital') return self.scryfallJson['data'][num]['digital'] def set_foil(self, num): - self._checkForTupleKey('data', num, 'foil') + super(Sets, self)._checkForTupleKey('data', num, 'foil') return self.scryfallJson['data'][num]['foil'] def set_icon_svg_uri(self, num): - self._checkForTupleKey('data', num, 'icon_svg_uri') + super(Sets, self)._checkForTupleKey('data', num, 'icon_svg_uri') return self.scryfallJson['data'][num]['icon_svg_uri'] def set_search_uri(self, num): - self._checkForTupleKey('data', num, 'search_uri') + super(Sets, self)._checkForTupleKey('data', num, 'search_uri') return self.scryfallJson['data'][num]['search_uri'] diff --git a/scrython/sets/sets_object.py b/scrython/sets/sets_object.py index aac8cdd..8ca43fd 100644 --- a/scrython/sets/sets_object.py +++ b/scrython/sets/sets_object.py @@ -56,8 +56,12 @@ class SetsObject(object): raise Exception(self.scryfallJson['details']) def _checkForKey(self, key): + if not key in self.scryfallJson: + raise KeyError('This object has no key \'{}\''.format(key)) + + def _checkForTupleKey(self, parent, num, key): try: - return self.scryfallJson[key] + return self.scryfallJson[parent][num][key] except Exception: raise KeyError('This object has no key \'{}\''.format(key)) diff --git a/scrython/symbology/parse_mana.py b/scrython/symbology/parse_mana.py index 951b409..1f32977 100644 --- a/scrython/symbology/parse_mana.py +++ b/scrython/symbology/parse_mana.py @@ -28,43 +28,37 @@ class ParseMana(SymbologyObject): self.url = 'symbology/parse-mana?cost=' + self.cost super(ParseMana, self).__init__(self.url) - def _checkForKey(self, key): - try: - return self.scryfallJson[key] - except Exception: - raise KeyError('This object has no key \'{}\''.format(key)) - def object(self): - self._checkForKey('object') + super(ParseMana, self)._checkForKey('object') return self.scryfallJson['object'] def mana_cost(self): - self._checkForKey('cost') + super(ParseMana, self)._checkForKey('cost') return self.scryfallJson['cost'] def cmc(self): - self._checkForKey('cmc') + super(ParseMana, self)._checkForKey('cmc') return self.scryfallJson['cmc'] def colors(self): - self._checkForKey('colors') + super(ParseMana, self)._checkForKey('colors') return self.scryfallJson['colors'] def colorless(self): - self._checkForKey('colorless') + super(ParseMana, self)._checkForKey('colorless') return self.scryfallJson['colorless'] def monocolored(self): - self._checkForKey('monocolored') + super(ParseMana, self)._checkForKey('monocolored') return self.scryfallJson['monocolored'] def multicolored(self): - self._checkForKey('multicolored') + super(ParseMana, self)._checkForKey('multicolored') return self.scryfallJson['multicolored'] diff --git a/scrython/symbology/symbology.py b/scrython/symbology/symbology.py index 1331368..bd67d85 100644 --- a/scrython/symbology/symbology.py +++ b/scrython/symbology/symbology.py @@ -33,74 +33,62 @@ class Symbology(SymbologyObject): self.url = 'symbology?' super(Symbology, self).__init__(self.url) - def _checkForKey(self, key): - try: - return self.scryfallJson[key] - except Exception: - raise KeyError('This object ahs no key \'{}\''.format(key)) - - def _checkForTupleKey(self, parent, num, key): - try: - return self.scryfallJson[parent][num][key] - except Exception: - raise KeyError('This object has no key \'{}\''.format(key)) - def object(self): - self._checkForKey('object') + super(Symbology, self)._checkForKey('object') return self.scryfallJson['object'] def has_more(self): - self._checkForKey('has_more') + super(Symbology, self)._checkForKey('has_more') return self.scryfallJson['has_more'] def data(self): - self._checkForKey('has_more') + super(Symbology, self)._checkForKey('has_more') return self.scryfallJson['data'] def data_length(self): - self._checkForKey('data') + super(Symbology, self)._checkForKey('data') return len(self.scryfallJson['data']) def symbol_symbol(self, num): - self._checkForTupleKey('data', num, 'symbol') + super(Symbology, self)._checkForTupleKey('data', num, 'symbol') return self.scryfallJson['data'][num]['symbol'] def symbol_loose_variant(self, num): - self._checkForTupleKey('data', num, 'loose_variant') + super(Symbology, self)._checkForTupleKey('data', num, 'loose_variant') return self.scryfallJson['data'][num]['loose_variant'] def symbol_transposable(self, num): - self._checkForTupleKey('data', num, 'transposable') + super(Symbology, self)._checkForTupleKey('data', num, 'transposable') return self.scryfallJson['data'][num]['transposable'] def symbol_represents_mana(self, num): - self._checkForTupleKey('data', num, 'represents_mana') + super(Symbology, self)._checkForTupleKey('data', num, 'represents_mana') return self.scryfallJson['data'][num]['represents_mana'] def symbol_cmc(self, num): - self._checkForTupleKey('data', num, 'cmc') + super(Symbology, self)._checkForTupleKey('data', num, 'cmc') return self.scryfallJson['data'][num]['cmc'] def symbol_appears_in_mana_costs(self, num): - self._checkForTupleKey('data', num, 'appears_in_mana_costs') + super(Symbology, self)._checkForTupleKey('data', num, 'appears_in_mana_costs') return self.scryfallJson['data'][num]['appears_in_mana_costs'] def symbol_funny(self, num): - self._checkForTupleKey('data', num, 'funny') + super(Symbology, self)._checkForTupleKey('data', num, 'funny') return self.scryfallJson['data'][num]['funny'] def symbol_colors(self, num): - self._checkForTupleKey('data', num, 'colors') + super(Symbology, self)._checkForTupleKey('data', num, 'colors') return self.scryfallJson['data'][num]['colors'] diff --git a/scrython/symbology/symbology_object.py b/scrython/symbology/symbology_object.py index c19b215..3008c16 100644 --- a/scrython/symbology/symbology_object.py +++ b/scrython/symbology/symbology_object.py @@ -41,3 +41,11 @@ class SymbologyObject(object): if self.scryfallJson['object'] == 'error': raise Exception(self.scryfallJson['details']) + + def _checkForKey(self, key): + if not key in self.scryfallJson: + raise KeyError('This object ahs no key \'{}\''.format(key)) + + def _checkForTupleKey(self, parent, num, key): + if not key in self.scryfallJson[parent][num]: + raise KeyError('This object has no key \'{}\''.format(key)) \ No newline at end of file From dd54ddfab2c804e9466fcc4c4244ddae32f8cccb Mon Sep 17 00:00:00 2001 From: Nanda Scott Date: Wed, 18 Apr 2018 20:56:17 -0400 Subject: [PATCH 2/2] Incremented version number so I don't forget. --- Changelog.md | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Changelog.md b/Changelog.md index 79890ab..f8dcbca 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,7 +5,7 @@ Changes - Key errors are now handled more cleanly, and doesn't return two traceback errors. -- Updated Sets to be more like the other classes in structure. +- Updated sets to be more like the other classes in structure. - Updated symbology to be more like the other classes in structure. ## 1.4.2 diff --git a/setup.py b/setup.py index 225371f..f33130b 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup setup( name='scrython', packages=['scrython', 'scrython.cards', 'scrython.rulings', 'scrython.catalog', 'scrython.sets', 'scrython.symbology'], - version='1.4.2', + version='1.5.0', description='A wrapper for using the Scryfall API.', long_description='https://github.com/NandaScott/Scrython/blob/master/README.md', url='https://github.com/NandaScott/Scrython',