Jupyter-ITVitae/assignment.ipynb

144 lines
7.6 KiB
Plaintext
Raw Normal View History

2024-02-29 13:25:58 +01:00
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
2024-02-29 13:25:58 +01:00
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'String' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m/home/lillian/Coding/jupyter/assignment.ipynb Cell 1\u001b[0m line \u001b[0;36m7\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=68'>69</a>\u001b[0m \u001b[39mfor\u001b[39;00m cat \u001b[39min\u001b[39;00m cat_json:\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=69'>70</a>\u001b[0m \u001b[39myield\u001b[39;00m Cat(\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=70'>71</a>\u001b[0m cat\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mid\u001b[39m\u001b[39m\"\u001b[39m),\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=71'>72</a>\u001b[0m cat\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39murl\u001b[39m\u001b[39m\"\u001b[39m),\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=72'>73</a>\u001b[0m cat\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mwidth\u001b[39m\u001b[39m\"\u001b[39m),\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=73'>74</a>\u001b[0m cat\u001b[39m.\u001b[39mget(\u001b[39m\"\u001b[39m\u001b[39mheight\u001b[39m\u001b[39m\"\u001b[39m),\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=74'>75</a>\u001b[0m )\n\u001b[0;32m---> <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=77'>78</a>\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mget_cat_image\u001b[39m(url: String) \u001b[39m-\u001b[39m\u001b[39m>\u001b[39m Image:\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=78'>79</a>\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"Fetch an image url of the cat provided, returns an image file.\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=79'>80</a>\u001b[0m \n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=80'>81</a>\u001b[0m \u001b[39m Args:\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=84'>85</a>\u001b[0m \u001b[39m Image: An image file parsed with the PIL library, read from a bytestream from the cats API.\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=85'>86</a>\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell:/home/lillian/Coding/jupyter/assignment.ipynb#W0sZmlsZQ%3D%3D?line=86'>87</a>\u001b[0m cr \u001b[39m=\u001b[39m requests\u001b[39m.\u001b[39mget(url)\n",
"\u001b[0;31mNameError\u001b[0m: name 'String' is not defined"
]
}
],
2024-02-29 13:25:58 +01:00
"source": [
"from collections.abc import Generator\n",
"from io import BytesIO\n",
"\n",
2024-02-29 13:25:58 +01:00
"import requests\n",
"from PIL import Image\n",
"\n",
"\n",
"class Cat:\n",
" \"\"\"Cat object storing the request data and image file from the cat API.\"\"\"\n",
"\n",
" def __init__(self, id, url, width, height):\n",
" self.id = id\n",
" self.url = url\n",
" self.width = width\n",
" self.height = height\n",
" self._image = get_cat_image(url)\n",
2024-02-29 13:25:58 +01:00
"\n",
" def get_image(self) -> Image:\n",
" \"\"\"Return the image of the cat object, cropped correctly if the width and height are changed.\n",
"\n",
" Returns:\n",
" Image: Image object that has been edited by the crop function.\n",
" \"\"\"\n",
" if (self.width, self.height) != self.image.size:\n",
" return self.image.resize((self.width, self.height))\n",
" return self.image\n",
"\n",
" def save_image(self, path: str) -> bool:\n",
" \"\"\"Save image in cat to disk\n",
"\n",
" Args:\n",
" path (str): the path to save the cat picture to.\n",
"\n",
" Returns:\n",
" bool: return if the function executed correctly, passthrough of the save function of Image.\n",
" \"\"\"\n",
" return self.get_image().save(f\"{path}/{self.id}.png\")\n",
"\n",
2024-02-29 13:25:58 +01:00
"\n",
"def get_cat_data(count: int = 1) -> list[dict]:\n",
2024-02-29 13:25:58 +01:00
" \"\"\"Fetch a cat from the cat API, return a parsed JSON object.\n",
"\n",
" Args:\n",
" count (int): the amount of cats to return.\n",
"\n",
2024-02-29 13:25:58 +01:00
" Returns:\n",
" list[dict]: Returns the parsed data, the cat API returns a list with length 1 with a dict in it.\n",
" \"\"\"\n",
" response = requests.get(f\"https://api.thecatapi.com/v1/images/search?limit={count}\")\n",
2024-02-29 13:25:58 +01:00
" if response.status_code != 200:\n",
" return None\n",
" return response.json()\n",
"\n",
"\n",
"def get_cats(cat_json: list[dict]) -> Generator[Cat]:\n",
2024-02-29 13:25:58 +01:00
" \"\"\"Create a cat image from a JSON object formatted like the cat API\n",
"\n",
" Args:\n",
" cat_json (list[dict]): Expects a list with a length > 1 with dicts in it that contains at least the fields:\n",
" (str) id\n",
" (str) url\n",
" (str) width\n",
" (str) height\n",
2024-02-29 13:25:58 +01:00
"\n",
" Returns:\n",
2024-02-29 14:32:35 +01:00
" Generator[Cat]: A Generator object creating cat objects from the JSON object.\n",
2024-02-29 13:25:58 +01:00
" \"\"\"\n",
"\n",
" for cat in cat_json:\n",
" yield Cat(\n",
" cat.get(\"id\"),\n",
" cat.get(\"url\"),\n",
" cat.get(\"width\"),\n",
" cat.get(\"height\"),\n",
" )\n",
2024-02-29 13:25:58 +01:00
"\n",
"\n",
"def get_cat_image(url: str) -> Image:\n",
2024-02-29 21:34:13 +01:00
" \"\"\"Fetch an image url of the cat provided, returns an image file.\n",
2024-02-29 13:25:58 +01:00
"\n",
" Args:\n",
" url (str): the amount of cats to return.\n",
"\n",
2024-02-29 13:25:58 +01:00
" Returns:\n",
" Image: An image file parsed with the PIL library, read from a bytestream from the cats API.\n",
" \"\"\"\n",
" cr = requests.get(url)\n",
" if cr.status_code != 200:\n",
" print(f\"Error fetching cat image, error code {cr.status_code}\")\n",
" return None\n",
" return Image.open(BytesIO(cr.content))\n",
"\n",
"\n",
"cats = get_cats(get_cat_data(count=5))\n",
"for cat in cats:\n",
" cat.height = 720\n",
" cat.width = 1080\n",
" cat.save_image(\"./cats\")"
2024-02-29 13:25:58 +01:00
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}