mirror of
https://github.com/LazoCoder/Pokemon-Terminal
synced 2024-11-24 04:43:10 +00:00
Merge pull request #42 from cclauss/patch-1
List comprehensions, and other simplifications
This commit is contained in:
commit
4958046edf
1 changed files with 39 additions and 79 deletions
118
database.py
118
database.py
|
@ -17,10 +17,8 @@ class Pokemon:
|
||||||
self.__path = path
|
self.__path = path
|
||||||
|
|
||||||
def get_id(self):
|
def get_id(self):
|
||||||
if self.is_extra():
|
# Pokemon from folder 'Extra' have no ID.
|
||||||
return "---" # Pokemon from folder 'Extra' have no ID.
|
return "---" if self.is_extra() else self.__id
|
||||||
else:
|
|
||||||
return self.__id
|
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
return self.__name
|
return self.__name
|
||||||
|
@ -54,15 +52,12 @@ class Database:
|
||||||
self.__load_extra()
|
self.__load_extra()
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
string = ""
|
return "\n".join(str(element) for element in self.__pokemon_list)
|
||||||
for element in self.__pokemon_list:
|
|
||||||
string += str(element) + "\n"
|
|
||||||
return string[:-1] # Remove the final new line ("\n").
|
|
||||||
|
|
||||||
def __contains__(self, pokemon):
|
def __contains__(self, pokemon):
|
||||||
# Check for a Pokemon by ID or name.
|
# Check for a Pokemon by ID or name.
|
||||||
if type(pokemon) is int or str(pokemon).isdigit():
|
if isinstance(pokemon, int) or str(pokemon).isdigit():
|
||||||
return self.pokemon_id_exists(pokemon)
|
return self.pokemon_id_exists(int(pokemon))
|
||||||
else:
|
else:
|
||||||
return self.pokemon_name_exists(pokemon)
|
return self.pokemon_name_exists(pokemon)
|
||||||
|
|
||||||
|
@ -71,10 +66,8 @@ class Database:
|
||||||
|
|
||||||
def get_all(self):
|
def get_all(self):
|
||||||
# Get all the Pokemon.
|
# Get all the Pokemon.
|
||||||
result = []
|
return [pokemon for pokemon in self.__pokemon_list]
|
||||||
for pokemon in self.__pokemon_list:
|
# or... return self.__pokemon_list[:] # return a copy of self.__pokemon_list
|
||||||
result.append(pokemon)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_regions(self):
|
def get_regions(self):
|
||||||
# Get all the supported regions.
|
# Get all the supported regions.
|
||||||
|
@ -102,30 +95,21 @@ class Database:
|
||||||
|
|
||||||
def __get_region(self, region):
|
def __get_region(self, region):
|
||||||
# Helper method for getting all the Pokemon of a specified region.
|
# Helper method for getting all the Pokemon of a specified region.
|
||||||
result = []
|
return [pokemon for pokemon in self.__pokemon_list
|
||||||
for pokemon in self.__pokemon_list:
|
if pokemon.get_region() == region]
|
||||||
if pokemon.get_region() == region:
|
|
||||||
result.append(pokemon)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def get_random(self):
|
def get_random(self):
|
||||||
# Select a random Pokemon from the database.
|
# Select a random Pokemon from the database.
|
||||||
random_int = random.randint(0, len(self.__pokemon_list) - 1)
|
return random.choice(self.__pokemon_list)
|
||||||
return self.__pokemon_list[random_int]
|
|
||||||
|
|
||||||
def get_random_from_region(self, region):
|
def get_random_from_region(self, region):
|
||||||
# Get a random Pokemon from a specific region.
|
# Get a random Pokemon from a specific region.
|
||||||
region_pokemon = self.__get_region(region)
|
return random.choice(self.__get_region(region))
|
||||||
random_int = random.randint(0, len(region_pokemon) - 1)
|
|
||||||
return region_pokemon[random_int]
|
|
||||||
|
|
||||||
def pokemon_id_exists(self, identifier):
|
def pokemon_id_exists(self, identifier):
|
||||||
# Check for Pokemon by ID.
|
# Check for Pokemon by ID.
|
||||||
identifier = int(identifier)
|
identifier = int(identifier)
|
||||||
if identifier < 1 or identifier > self.__MAX_ID:
|
return 0 < identifier <= self.__MAX_ID
|
||||||
return False
|
|
||||||
else:
|
|
||||||
return True
|
|
||||||
|
|
||||||
def pokemon_name_exists(self, name):
|
def pokemon_name_exists(self, name):
|
||||||
# Check for Pokemon by Name.
|
# Check for Pokemon by Name.
|
||||||
|
@ -133,18 +117,18 @@ class Database:
|
||||||
|
|
||||||
def get_pokemon(self, pokemon):
|
def get_pokemon(self, pokemon):
|
||||||
# Get a Pokemon by name or ID.
|
# Get a Pokemon by name or ID.
|
||||||
if type(pokemon) is not int and type(pokemon) is not str:
|
if not isinstance(pokemon, (int, str)):
|
||||||
raise Exception("The parameter Pokemon must be of type integer or string.")
|
raise Exception("The parameter Pokemon must be of type integer or string.")
|
||||||
if not self.__contains__(pokemon):
|
if pokemon not in self:
|
||||||
raise Exception("No such Pokemon in the database.")
|
raise Exception("No such Pokemon in the database.")
|
||||||
if type(pokemon) is int or str(pokemon).isdigit():
|
if isinstance(pokemon, int) or str(pokemon).isdigit():
|
||||||
return self.get_pokemon_by_id(pokemon)
|
return self.get_pokemon_by_id(int(pokemon))
|
||||||
else:
|
else:
|
||||||
return self.get_pokemon_by_name(pokemon)
|
return self.get_pokemon_by_name(pokemon)
|
||||||
|
|
||||||
def get_pokemon_by_name(self, name):
|
def get_pokemon_by_name(self, name):
|
||||||
# Get a Pokemon by its name.
|
# Get a Pokemon by its name.
|
||||||
if type(name) is not str:
|
if not isinstance(name, str):
|
||||||
raise TypeError("The type of name must be a string.")
|
raise TypeError("The type of name must be a string.")
|
||||||
if not self.pokemon_name_exists(name):
|
if not self.pokemon_name_exists(name):
|
||||||
raise Exception("No such Pokemon in the database.")
|
raise Exception("No such Pokemon in the database.")
|
||||||
|
@ -152,41 +136,34 @@ class Database:
|
||||||
|
|
||||||
def get_pokemon_by_id(self, identifier):
|
def get_pokemon_by_id(self, identifier):
|
||||||
# Get a Pokemon by its ID.
|
# Get a Pokemon by its ID.
|
||||||
if type(identifier) is not int and not str(identifier).isdigit():
|
if not isinstance(identifier, int) and not str(identifier).isdigit():
|
||||||
raise TypeError("The Pokemon ID must be a number.")
|
raise TypeError("The Pokemon ID must be a number.")
|
||||||
|
identifier = int(identifier)
|
||||||
if not self.pokemon_id_exists(identifier):
|
if not self.pokemon_id_exists(identifier):
|
||||||
raise Exception("The Pokemon ID must be between 1 and " + str(self.__MAX_ID) + " inclusive.")
|
raise Exception("The Pokemon ID must be between 1 and " + str(self.__MAX_ID) + " inclusive.")
|
||||||
return self.__pokemon_list[int(identifier) - 1] # Subtract 1 to convert to 0 base indexing.
|
return self.__pokemon_list[identifier - 1] # Subtract 1 to convert to 0 base indexing.
|
||||||
|
|
||||||
def names_with_prefix(self, prefix):
|
def names_with_prefix(self, prefix):
|
||||||
# Return Pokemon who's names begin with the specified prefix.
|
# Return Pokemon who's names begin with the specified prefix.
|
||||||
result = []
|
return [pokemon for pokemon in self.__pokemon_list
|
||||||
for pokemon in self.__pokemon_list:
|
if str(pokemon.get_name()).startswith(prefix)]
|
||||||
if str(pokemon.get_name()).startswith(prefix):
|
|
||||||
result.append(pokemon)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def names_with_infix(self, infix):
|
def names_with_infix(self, infix):
|
||||||
# Return Pokemon who's names contains the specified infix.
|
# Return Pokemon who's names contains the specified infix.
|
||||||
result = []
|
return [pokemon for pokemon in self.__pokemon_list
|
||||||
for pokemon in self.__pokemon_list:
|
if infix in str(pokemon.get_name())]
|
||||||
if infix in str(pokemon.get_name()):
|
|
||||||
result.append(pokemon)
|
|
||||||
return result
|
|
||||||
|
|
||||||
def __load_data(self):
|
def __load_data(self):
|
||||||
# Load all the Pokemon data. This does not include the 'Extra' Pokemon.
|
# Load all the Pokemon data. This does not include the 'Extra' Pokemon.
|
||||||
path = "/./Data/pokemon.txt"
|
with open(self.directory + "/./Data/pokemon.txt", 'r') as data_file:
|
||||||
data_file = open(self.directory + path, 'r')
|
for line in data_file: # Load everything but the Pokemon from the 'Extra' folder.
|
||||||
for line in data_file: # Load everything but the Pokemon from the 'Extra' folder.
|
identifier, _, name = line.strip().partition(' ')
|
||||||
identifier = line.split(' ')[0] # First part of the line is the id.
|
identifier = '{:03}'.format(identifier)
|
||||||
name = line[len(identifier)+1:-1].lower() # The rest is the name (minus the new line at the end).
|
region = self.__determine_region(identifier)
|
||||||
identifier = self.__add_zeroes(identifier) # This statement cannot occur before name has been created.
|
path = self.__determine_folder(identifier) + "/" + identifier + ".png"
|
||||||
region = self.__determine_region(identifier)
|
pokemon = Pokemon(identifier, name, region, path)
|
||||||
path = self.__determine_folder(identifier) + "/" + identifier + ".png"
|
self.__pokemon_list.append(pokemon)
|
||||||
pokemon = Pokemon(identifier, name, region, path)
|
self.__pokemon_dictionary[pokemon.get_name()] = pokemon
|
||||||
self.__pokemon_list.append(pokemon)
|
|
||||||
self.__pokemon_dictionary[pokemon.get_name()] = pokemon
|
|
||||||
|
|
||||||
def __load_extra(self):
|
def __load_extra(self):
|
||||||
# Load all the file names of the images in the Extra folder.
|
# Load all the file names of the images in the Extra folder.
|
||||||
|
@ -201,16 +178,6 @@ class Database:
|
||||||
self.__pokemon_list.append(pokemon)
|
self.__pokemon_list.append(pokemon)
|
||||||
self.__pokemon_dictionary[pokemon.get_name()] = pokemon
|
self.__pokemon_dictionary[pokemon.get_name()] = pokemon
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def __add_zeroes(number):
|
|
||||||
# Add zeroes to the front so that it begins with 3 digits. Example: "2" -> "002".
|
|
||||||
zeroes = ""
|
|
||||||
if int(number) < 10:
|
|
||||||
zeroes = "00"
|
|
||||||
elif int(number) < 100:
|
|
||||||
zeroes = "0"
|
|
||||||
return zeroes + str(number)
|
|
||||||
|
|
||||||
def __determine_region(self, identifier):
|
def __determine_region(self, identifier):
|
||||||
# Determine which region a Pokemon is from.
|
# Determine which region a Pokemon is from.
|
||||||
identifier = int(identifier)
|
identifier = int(identifier)
|
||||||
|
@ -229,16 +196,9 @@ class Database:
|
||||||
|
|
||||||
def __determine_folder(self, identifier):
|
def __determine_folder(self, identifier):
|
||||||
# Determine which folder a Pokemon is from.
|
# Determine which folder a Pokemon is from.
|
||||||
identifier = int(identifier)
|
suffix_dict = {"kanto": "I - Kanto",
|
||||||
if identifier < 1:
|
"johto": "II - Johto",
|
||||||
raise Exception("Pokemon ID cannot be less than 1.")
|
"hoenn": "III - Hoenn",
|
||||||
if identifier < 152:
|
"sinnoh": "IV - Sinnoh"}
|
||||||
return self.directory + "/./Images/Generation I - Kanto"
|
suffix = suffix_dict.get(self.__determine_region(identifier))
|
||||||
elif identifier < 252:
|
return "{}/./Images/Generation {}".format(self.directory, suffix)
|
||||||
return self.directory + "/./Images/Generation II - Johto"
|
|
||||||
elif identifier < 387:
|
|
||||||
return self.directory + "/./Images/Generation III - Hoenn"
|
|
||||||
elif identifier < 494:
|
|
||||||
return self.directory + "/./Images/Generation IV - Sinnoh"
|
|
||||||
else:
|
|
||||||
raise Exception("Pokemon ID cannot be greater than 493.")
|
|
||||||
|
|
Loading…
Reference in a new issue