From bf1bb5034da3785ace2f2835bf006f4c9e06d7d0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Nils=20Forss=C3=A9n?= Date: Wed, 23 Feb 2022 13:22:40 +0100 Subject: [PATCH] comments --- laboration3/pokedex.py | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/laboration3/pokedex.py b/laboration3/pokedex.py index aa86950..df4b9df 100644 --- a/laboration3/pokedex.py +++ b/laboration3/pokedex.py @@ -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") -- 2.30.2