comments
authorNils Forssén <forssennils@gmail.com>
Wed, 23 Feb 2022 12:22:40 +0000 (13:22 +0100)
committerNils Forssén <forssennils@gmail.com>
Wed, 23 Feb 2022 12:22:40 +0000 (13:22 +0100)
laboration3/pokedex.py

index aa86950e230ea5c213268d5dd1780dfe7cf6ecfe..df4b9dfd8d8696be4e1a6506af8e230dfd521454 100644 (file)
@@ -1,12 +1,16 @@
 # pokedex.py
 import requests
-import json
 import sys
 
 API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/"
 
 
 def main():
+    """Mainloop
+
+    Raises:
+        ValueError: Script is not run with mandatory pokemon argument
+    """
     data = get_json_as_dict(API_SERVER + "api/v2/pokemon/")
     pokemons = data.get("results")
     try:
@@ -19,6 +23,17 @@ def main():
 
 
 def get_json_as_dict(url):
+    """Fetch data from url in JSON-format
+
+    Args:
+        url (str): API url
+
+    Raises:
+        requests.RequestException: data request unsuccessful
+
+    Returns:
+        dict: data in JSON-format
+    """
     r = requests.get(url)
     if r.status_code != 200:
         raise requests.RequestException("Request unsuccessful")
@@ -27,6 +42,19 @@ def get_json_as_dict(url):
 
 
 def get_abilities(pokemon, pokemon_data_list):
+    """Get abilities of named pokemon from lookup list
+
+    Args:
+        pokemon (str): Requested pokemon
+        pokemon_data_list (list): lookup list of all pokemons
+
+    Raises:
+        ValueError: Pokemon requested not found in the given list
+
+    Returns:
+        list: Abilities of pokemon
+    """
+    # Linear search for the requested pokemon and get the pokemon-url
     poke_url = None
     for poke in pokemon_data_list:
         if poke.get("name") == pokemon:
@@ -36,13 +64,20 @@ def get_abilities(pokemon, pokemon_data_list):
         raise ValueError(f"No pokemon named {pokemon}")
 
     pokemon_data = get_json_as_dict(API_SERVER + poke_url)
-
     return pokemon_data.get("abilities")
 
 
 def print_info(name, abilities, language="en"):
+    """Print the name and abilities of the pokemon
+
+    Args:
+        name (str): Name of pokemon
+        abilities (list): list of abilities of the pokemon
+        language (str, optional): Language to print in. Defaults to "en".
+    """
     print(f"{name} has {len(abilities)}.\n")
 
+    # Repeat for every ability
     for ability in abilities:
         abil = ability.get("ability")
         abil_url = abil.get("url")