mirror of
https://github.com/LazoCoder/Pokemon-Terminal
synced 2024-11-27 06:10:29 +00:00
Merge pull request #97 from cclauss/patch-7
Create the pokemon_list outside of the Database (round II)
This commit is contained in:
commit
854ae82e9c
2 changed files with 136 additions and 0 deletions
89
load_all_pokemon.py
Normal file
89
load_all_pokemon.py
Normal file
|
@ -0,0 +1,89 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import collections
|
||||
import os
|
||||
from database import Pokemon
|
||||
|
||||
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
|
||||
DATA_DIR = os.path.join(SCRIPT_DIR, 'Data')
|
||||
IMAGES_DIR = os.path.join(SCRIPT_DIR, 'Images')
|
||||
EXTRA_DIR = os.path.join(IMAGES_DIR, 'Extra')
|
||||
|
||||
region_info = collections.namedtuple('region_info', 'start end dir_name')
|
||||
region_dict = {
|
||||
'kanto': region_info(1, 151, 'Generation I - Kanto'),
|
||||
'johto': region_info(152, 251, 'Generation II - Johto'),
|
||||
'hoenn': region_info(252, 386, 'Generation III - Hoenn'),
|
||||
'sinnoh': region_info(387, 493, 'Generation IV - Sinnoh'),
|
||||
'extra': region_info(0, 0, 'Extra')
|
||||
}
|
||||
|
||||
g_pokemon_dict = None # potential fathers for extra pokemon
|
||||
|
||||
|
||||
def region_name_by_id(id):
|
||||
if not id:
|
||||
return 'extra'
|
||||
for name, region in region_dict.items():
|
||||
if region.start <= int(id) <= region.end:
|
||||
return name
|
||||
assert False, 'region_name_by_id({})'.format(id)
|
||||
|
||||
|
||||
def make_a_pokemon(id, line):
|
||||
id = '{:03}'.format(id)
|
||||
line = line.strip().split()
|
||||
while len(line) < 4: # add '' as the subtype if it is missing
|
||||
line.append('')
|
||||
name, threshold, main_type, subtype = line
|
||||
name = name.lower()
|
||||
threshold = float(threshold)
|
||||
region = region_name_by_id(id)
|
||||
dir_name = region_dict[region].dir_name
|
||||
path = os.path.join(IMAGES_DIR, dir_name, id + '.jpg')
|
||||
return Pokemon(id, name, region, path, main_type, subtype, threshold)
|
||||
|
||||
|
||||
def load_pokemon(filename='pokemon.txt'):
|
||||
"""Load everything but the Pokemon from the 'Extra' folder"""
|
||||
with open(os.path.join(DATA_DIR, filename)) as in_file:
|
||||
return [make_a_pokemon(i, line) for i, line in enumerate(in_file, 1)]
|
||||
|
||||
|
||||
def make_an_extra_pokemon(filename, in_ext='.jpg'):
|
||||
name, ext = os.path.splitext(filename)
|
||||
if ext.lower() == in_ext:
|
||||
name = name.lower()
|
||||
path = os.path.join(EXTRA_DIR, filename)
|
||||
father = g_pokemon_dict.get(name.split('-')[0])
|
||||
if father:
|
||||
return Pokemon(None, name,
|
||||
father.get_region(), path,
|
||||
father.get_pkmn_type(),
|
||||
father.get_pkmn_type_secondary(),
|
||||
father.get_dark_threshold())
|
||||
else:
|
||||
return Pokemon(None, name, None, path, None, None, None)
|
||||
assert False, 'Bad file extention: {} != {}'.format(ext, in_ext)
|
||||
|
||||
|
||||
def load_extra(image_dir=None):
|
||||
"""Load all the file names of the images in the Extra folder."""
|
||||
filenames = os.listdir(image_dir or EXTRA_DIR)
|
||||
return [make_an_extra_pokemon(filename) for filename in filenames]
|
||||
|
||||
|
||||
def load_all_pokemon():
|
||||
global g_pokemon_dict
|
||||
pokemon_list = load_pokemon()
|
||||
g_pokemon_dict = {pokemon.get_name(): pokemon for pokemon in pokemon_list}
|
||||
return pokemon_list + load_extra()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
pokemon_list = load_all_pokemon()
|
||||
pokemon_dict = {pokemon.get_name(): pokemon for pokemon in pokemon_list}
|
||||
print(len(pokemon_list), len(set(pokemon_list)), len(pokemon_dict))
|
||||
for i in (0, -1):
|
||||
pokemon = pokemon_list[i]
|
||||
print(pokemon.is_extra(), pokemon)
|
47
test_load.py
Normal file
47
test_load.py
Normal file
|
@ -0,0 +1,47 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# To run the tests, use: python3 -m pytest --capture=sys
|
||||
|
||||
from database import Database, Pokemon
|
||||
from load_all_pokemon import load_all_pokemon
|
||||
from test_utils import expected_len, MAX_ID
|
||||
|
||||
|
||||
def compare_pokemon(a, b):
|
||||
assert isinstance(a, Pokemon)
|
||||
assert isinstance(b, Pokemon)
|
||||
assert a.get_id() == b.get_id()
|
||||
assert a.get_name() == b.get_name()
|
||||
assert a.get_region() == b.get_region()
|
||||
assert a.get_path() == b.get_path()
|
||||
assert a.get_dark_threshold() == b.get_dark_threshold()
|
||||
assert a.get_pkmn_type() == b.get_pkmn_type()
|
||||
assert a.get_pkmn_type_secondary() == b.get_pkmn_type_secondary()
|
||||
# print(a.get_name())
|
||||
|
||||
|
||||
def test_len():
|
||||
assert len(Database()) == len(load_all_pokemon()) == MAX_ID + expected_len('extra')
|
||||
|
||||
|
||||
def test_lists():
|
||||
db = Database()
|
||||
load_list = load_all_pokemon()
|
||||
for db_p, load_p in zip(db.get_all(), load_list):
|
||||
assert str(db_p) == str(load_p)
|
||||
compare_pokemon(db_p, load_p)
|
||||
# db_p != load_p but the hidden __attributes stifle complete testing
|
||||
# assert db_p == load_p, '\n{}\n{}'.format(db_p, load_p)
|
||||
# the lists are not identical but hidden __attributes stifle complete tests
|
||||
# assert db.get_all() == load_list
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# Test runner: Runs all functions whose name begins with `test_`
|
||||
# locals() changes when trying to do this without the list comprehension!!!
|
||||
name_funcs = [(n, f) for n, f in locals().items() if n.startswith('test_')]
|
||||
for name, func in name_funcs:
|
||||
if callable(func):
|
||||
func()
|
||||
else:
|
||||
print(name + ' is not callable()!')
|
Loading…
Reference in a new issue