From: Nils Forssén Date: Tue, 22 Feb 2022 09:16:11 +0000 (+0100) Subject: msg X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=18d45163f9fb8515ed33999b1044b6c59340f0fb;p=TDDE44.git msg --- diff --git a/laboration3/pokedex.py b/laboration3/pokedex.py index 947fe7a..aa86950 100644 --- a/laboration3/pokedex.py +++ b/laboration3/pokedex.py @@ -3,23 +3,68 @@ import requests import json import sys -API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/api/v2/" +API_SERVER = "https://www.ida.liu.se/~TDDE44/pokeapi/" + def main(): - data = get_json_as_dict(API_SERVER + "pokemon/") + data = get_json_as_dict(API_SERVER + "api/v2/pokemon/") pokemons = data.get("results") - for pokemon in pokemons: - print(pokemon.get("name")) - - + try: + request_name = sys.argv[1] + except IndexError: + raise ValueError("Must specify pokemon to request data of") + + abilities = get_abilities(request_name, pokemons) + print_info(request_name, abilities) + def get_json_as_dict(url): r = requests.get(url) - assert r.status_code == 200, "Request unsuccessful" + if r.status_code != 200: + raise requests.RequestException("Request unsuccessful") return r.json() - - + + +def get_abilities(pokemon, pokemon_data_list): + poke_url = None + for poke in pokemon_data_list: + if poke.get("name") == pokemon: + poke_url = poke.get("url") + break + else: + 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(f"{name} has {len(abilities)}.\n") + + for ability in abilities: + abil = ability.get("ability") + abil_url = abil.get("url") + abil_data = get_json_as_dict(API_SERVER + abil_url) + + # Linear search for correct language of ability + abil_name = None + for name in abil_data.get("names"): + if name.get("language").get("name") == language: + abil_name = name.get("name") + break + + # Linear search for correct language ft-entry + abil_entry = None + for entry in abil_data.get("flavor_text_entries"): + if entry.get("language").get("name") == language: + abil_entry = entry + break + + print(f"Ability {abil_name}: ") + print(f"{abil_entry.get('flavor_text')}\n") + + if __name__ == "__main__": main() - \ No newline at end of file diff --git a/laboration3/uppgift_3.py b/laboration3/uppgift_3.py index 5ee88a9..4074a6f 100644 --- a/laboration3/uppgift_3.py +++ b/laboration3/uppgift_3.py @@ -6,6 +6,7 @@ def fac_rec(n): else: return n * fac_rec(n-1) + def fib(n): if n == 0: return 0 @@ -23,6 +24,7 @@ def pascal(row, col): else: return pascal(row-1, col) + pascal(row-1, col - 1) + def keep_if_even(lst): if not lst: return list() @@ -41,13 +43,14 @@ def reverse_rec(lst): ret.extend(reverse_rec(lst[:-1])) return ret + def keep_if_even_all(lst): if not lst: return list() even_inner = list() if isinstance(lst[0], list): even_inner = keep_if_even_all(lst[0]) - even_inner = [even_inner] #Ibland e man bara lat + even_inner = [even_inner] # Ibland e man bara lat elif not lst[0] % 2: ret = [lst[0]] ret.extend(keep_if_even_all(lst[1:])) @@ -56,6 +59,7 @@ def keep_if_even_all(lst): even_inner.extend(keep_if_even_all(lst[1:])) return even_inner + def reverse_rec_all(lst): if not lst: return [] @@ -69,6 +73,7 @@ def reverse_rec_all(lst): reverse_inner.extend(reverse_rec_all(lst[:-1])) return reverse_inner + def is_in_list(lst, elm): if not lst: return False @@ -96,7 +101,8 @@ def subst_all(lst, elm, new_value): return list() if isinstance(lst[0], list): inner = subst_all(lst[0], elm, new_value) - inner = [inner] # Pallar inte skriva om snyggt s blir lite fulkod ibland men det funkar + # Pallar inte skriva om snyggt s blir lite fulkod ibland men det funkar + inner = [inner] elif lst[0] == elm: inner = [new_value] else: @@ -105,6 +111,7 @@ def subst_all(lst, elm, new_value): inner.extend(subst_all(lst[1:], elm, new_value)) return inner + def linear_search(key, seq): if not seq: return False