Mach es besser als Timo (2024)

Aufgabe

Nimm den in der Vorlesung am Tag 7 erarbeiteten Code und

  • ergänze Code
  • ergänze Typehinting
  • Verwende sinnvolle Variablen-, Klassen- und Funktionsnamen
  • passe die Struktur an (Klassen und Teilfunktion)

Hier der Code

import os
import requests
import pprint

class Kadse():
    """
    Represents a cat breed with its name and age.

    Attributes:
        breed (str): The name of the cat breed.
        age (int): The min(lifespan) of the cat breed in years.

    Methods:
        __init__(breed: str, age: str) -> None: Initializes a new Kadse instance.
        __str__() -> str: Returns a formatted string representation of the Kadse instance.
        parse_age(age: str) -> int: Parses the age string to extract the min(lifespan) value.
    """

    def __init__(self, breed: str, age: str, eidi: str) -> None:
        self.breed = breed
        self.age = self.parse_age(age)
        self.eidi = eidi

    def __str__(self) -> str:
        return f'Die Katzenrasse {self.breed} wird {self.age} Jahre alt.'

    def parse_age(self, age: str) -> int:
        """
        Parses the age string to extract the lifespan value.

        Args:
            age (str): A string containing the breed name and lifespan, e.g., "12-15 years".

        Returns:
            int: The parsed lifespan value in years.
        """
        return int(age.split(' ')[0])

def get_all_the_breedz() -> list[Kadse]:
    """
    Fetches data about various cat breeds from The Cat API and creates instances of the Kadse class.

    Returns:
        list: A list of Kadse instances representing different cat breeds.

    Note:
        This function makes an HTTP GET request to the Cat API endpoint.
    """
    breeds_endpoint = 'https://api.thecatapi.com/v1/breeds'
    response = requests.get(breeds_endpoint)
    katzenliste = []
    for kadse in response.json():
        #print(kadse['name'])
        #print(kadse['life_span'])
        neue_katze = Kadse(breed=kadse['name'], age=kadse['life_span'], eidi=kadse['id'])
        katzenliste.append(neue_katze)
        #print(neue_katze)

    return katzenliste

def filter_all_my_cats(katzenliste: list[Kadse], min_alter: int) -> list[Kadse]:
    gefilterte_katzen = []
    for kadse in katzenliste:
        if kadse.age >= min_alter:
            #print(f'{kadse.breed} käme in Frage weil alter={kadse.age}')
            gefilterte_katzen.append(kadse)

    return gefilterte_katzen

def get_cat_images(katzenliste: list[Kadse]) -> None:
    for kadse in katzenliste:
        response = requests.get(f'https://api.thecatapi.com/v1/images/search?limit=10&breed_ids={kadse.eidi}')
        os.makedirs(f'./{kadse.eidi}')

        for image in response.json():
            katzenbild = requests.get(image['url'])
            f = open(f'{kadse.eidi}/{image['url'].split('/')[-1]}', "wb")
            f.write(katzenbild.content)
            f.close()


def main():
    katzenliste = get_all_the_breedz()
    kuerzere_katzenliste = filter_all_my_cats(katzenliste, 15)
    get_cat_images(kuerzere_katzenliste)

if __name__ == '__main__':
    main()