diff --git a/example_item_data_dict.json b/example_item_data_dict.json new file mode 100644 index 0000000000000000000000000000000000000000..5cb49f7464cad5d7cf2900f58c5879ff5e1f051d --- /dev/null +++ b/example_item_data_dict.json @@ -0,0 +1,17 @@ +{ + "1":{ + "item_number":1, + "mass":10, + "delivery_time":1 + }, + "2":{ + "item_number":2, + "mass":20, + "delivery_time":2 + }, + "3":{ + "item_number":3, + "mass":30, + "delivery_time":3 + } +} \ No newline at end of file diff --git a/example_item_data_list.json b/example_item_data_list.json new file mode 100644 index 0000000000000000000000000000000000000000..e701f7d0f6fc6ff531ae3d646d99d2cf19119e5a --- /dev/null +++ b/example_item_data_list.json @@ -0,0 +1,17 @@ +[ + { + "item_number":1, + "mass":10, + "delivery_time":1 + }, + { + "item_number":2, + "mass":20, + "delivery_time":2 + }, + { + "item_number":3, + "mass":30, + "delivery_time":3 + } +] \ No newline at end of file diff --git a/functions/classes.py b/functions/classes.py deleted file mode 100644 index f02464845ab52f67ce440f22d2fc93ff4efd6b8b..0000000000000000000000000000000000000000 --- a/functions/classes.py +++ /dev/null @@ -1,8 +0,0 @@ -''' -File consists of several classes for the different elements of a device. -''' - - -if __name__ == "__main__": - print("This script contains classes for the different elements of a device. It is not to be executed independently.") - pass \ No newline at end of file diff --git a/functions/lego_classes.py b/functions/lego_classes.py new file mode 100644 index 0000000000000000000000000000000000000000..19da1899f8b010a5d0e02bc55fb56f58337ddbd8 --- /dev/null +++ b/functions/lego_classes.py @@ -0,0 +1,71 @@ +''' +File consists of several classes for the different elements of a device. +''' +from __future__ import annotations +import uuid +from typing import Any, Union, Literal, TypedDict, TypeVar, Type, List, Optional, Dict + + +class LegoItem: + def __init__(self, item_number: int, mass: float, delivery_time: int) -> None: + # , *args, **kwargs not handling additional/optional specs right now + self.id = uuid.uuid4() + self.item_number = item_number + self.mass = mass + self.delivery_time = delivery_time + self.parent_id = None # This will be set when added to a component + + def __str__(self): + return ( + f"Item(id={self.id}, item_number={self.item_number}, " + f"mass={self.mass}, delivery_time={self.delivery_time}, " + f"parent_id={self.parent_id})" + ) + + +class LegoComponent: + def __init__(self, items=None | LegoItem | list[LegoItem], + components=None) -> None: # TODO: type hinting + self.id = uuid.uuid4() + if (isinstance(items, list) and items + and isinstance(items[0], LegoItem)): + self.items = items + elif isinstance(items, LegoItem): + self.items = [items] + else: + self.items = [] # must be a list for now, + if components is None: + self.components = [] + elif isinstance(components, list): + self.components = components + else: + self.components = [components] # Not checking correct type + self.parent_id = None # This will be set when added to a component + + def add_item(self, item: LegoItem | List[LegoItem]) -> None: + if isinstance(item, list): + self.items.extend(item) + else: + self.items.append(item) + + def add_component(self, component: LegoComponent | List[LegoComponent] + ) -> None: + if isinstance(component, list): + self.components.extend(component) + else: + self.components.append(component) + + def add(self, element) -> None: + if isinstance(element, LegoItem): + self.items.append(element) + elif isinstance(element, LegoComponent): + self.components.append(element) + else: + raise TypeError(f"added elements should be of type LegoItem or " + f"LegoComponent not {type(element)}") + + def children(self) -> Dict[str, List[LegoItem] | List[LegoComponent]]: + return {'items': self.items, 'components': self.components} + + +# TODO: Adjust default output when printing an item or component \ No newline at end of file diff --git a/test-legoclasses.ipynb b/test-legoclasses.ipynb new file mode 100644 index 0000000000000000000000000000000000000000..2a45d1fa0962e2008f62614d1a8d36a895944694 --- /dev/null +++ b/test-legoclasses.ipynb @@ -0,0 +1,176 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# import functions.lego_classes as lego_classes\n", + "from functions.lego_classes import LegoItem\n", + "from functions.lego_classes import LegoComponent" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[<functions.lego_classes.LegoComponent object at 0x000002720F4BF7F0>, <functions.lego_classes.LegoComponent object at 0x000002720F4BF790>]\n" + ] + } + ], + "source": [ + "# Test manually creating some item and components\n", + "\n", + "item1 = LegoItem(1,0.10,10)\n", + "item2 = LegoItem(3,0.30,30)\n", + "item3 = LegoItem(2, 0.2,20)\n", + "\n", + "\n", + "\n", + "component1 = LegoComponent()\n", + "# component1.add_item(item1)\n", + "component2 = LegoComponent(item2) \n", + "\n", + "componentall = LegoComponent([component1,component2]) # This will be saved as items for some reasaon\n", + "# component3 = LegoComponent(item3,component1)\n", + "# component3.add(item2) # Can't really use an item twice\n", + "# component3.add(component2)\n", + "print(componentall.items)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "# import tkinter as tk\n", + "\n", + "def read_json(file_path):\n", + " with open(file_path, 'r') as f:\n", + " data = json.load(f)\n", + " return data\n", + "json_data = read_json('example_item_data_list.json')\n", + "# that produces a list, we'd prefer a dict\n", + "\n", + "with open('example_item_data_dict.json') as json_file:\n", + " json_dict = json.load(json_file)\n", + " print(type(json_dict))\n", + " print(json_dict[\"1\"])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Clever way to just create an item for each listed element\n", + "def create_all_items(data):\n", + " items = []\n", + " for item_data in data:\n", + " item_number = item_data.get('item_number')\n", + " mass = item_data.get('mass')\n", + " delivery_time = item_data.get('delivery_time')\n", + " # specific_data = item_data.get('specific_data', {})\n", + " item = LegoItem(item_number, mass, delivery_time)\n", + " items.append(item)\n", + " return items\n", + "\n", + "all_items = create_all_items(json_data) \n", + "\n", + "# How to get entries for a specific item\n", + "def get_item_by_number(data, item_number):\n", + " datasheet = [item for item in data if item['item_number' ] == item_number] \n", + " return datasheet\n", + "\n", + "sheet1 = get_item_by_number(json_data, 2) # this is a list? meh\n", + "\n", + "dict1 = \n", + "# Create an item for this:\n", + "def create_item_from_sheet(dict):\n", + " item_number = dict(\"\")\n", + "\n", + " item = LegoItem(item_number, mass, delivery_time)\n", + " return item\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(sheet1['item_number'])" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "print(json_data)\n", + "print(all_items[1])\n", + "print(sheet1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Lets build some items\n", + "# lets say item 2 twice and one of item 3\n", + "\n", + "\n", + "\n", + "gearbox = LegoComponent()\n", + "gearbox.add_item()" + ] + } + ], + "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.10.7" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "369f2c481f4da34e4445cda3fffd2e751bd1c4d706f27375911949ba6bb62e1c" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/test-legoclasses.py b/test-legoclasses.py new file mode 100644 index 0000000000000000000000000000000000000000..69d88cfcb79fd5afb7a1f7a9fcd7cb982b1190f7 --- /dev/null +++ b/test-legoclasses.py @@ -0,0 +1,27 @@ + +# import functions.lego_classes as lego_classes +from functions.lego_classes import LegoItem +from functions.lego_classes import LegoComponent + +# Test manually creating some item and components + +item1 = LegoItem(1,0.10,10) +item2 = LegoItem(3,0.30,30) +item3 = LegoItem(2, 0.2,20) + + + +component1 = LegoComponent() +# component1.add_item(item1) +component2 = LegoComponent(item2) + +componentall = LegoComponent([item1,item2],[]) # Some combinations might result in errors +# component3 = LegoComponent(item3,component1) +# component3.add(item2) # Can't really use an item twice +# component3.add(component2) +print(componentall.items) +x = componentall.children() +print(x) + +print(type(x)) +alist = ["abc","def"]