From 42fd034a92e0306c0967dec683130b71899dc30d Mon Sep 17 00:00:00 2001 From: "Hock, Martin" <martin.hock@fst.tu-darmstadt.de> Date: Thu, 2 Mar 2023 19:20:34 +0100 Subject: [PATCH] Format ausarbeitung.ipynb with black and flake8. --- ausarbeitung.ipynb | 279 ++++++++++++++++++--------------------------- 1 file changed, 110 insertions(+), 169 deletions(-) diff --git a/ausarbeitung.ipynb b/ausarbeitung.ipynb index 8ceadae..fd459e6 100644 --- a/ausarbeitung.ipynb +++ b/ausarbeitung.ipynb @@ -57,12 +57,12 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "id": "b2778dee", "metadata": {}, "outputs": [], "source": [ - "# import modules \n", + "# import modules\n", "import json\n", "import pprint\n", "from functions import calculation_rules\n", @@ -79,172 +79,133 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "id": "0b1f9aff", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'item number': 32073, 'item description': 'Axle 5 studs', 'category': 'axle', 'price [Euro]': 0.001, 'mass [g]': 0.66, 'delivery time [days]': 3, 'Abmessung [studs]': 5}\n", - "front axle\n", - "{'Abmessung [studs]': 5,\n", - " 'category': 'axle',\n", - " 'color': 'grey',\n", - " 'delivery time [days]': 3,\n", - " 'item description': 'Axle 5 studs',\n", - " 'item number': 32073,\n", - " 'label': 'back axle',\n", - " 'mass [g]': 0.66,\n", - " 'price [Euro]': 0.001}\n" - ] - } - ], + "outputs": [], "source": [ - "## ## Create the wheels and axles as single components first\n", - "# Look up the specific item you want from the provided json files, we can load the full file into a dict\n", - "with open('datasheets/axles.json') as json_file:\n", + "# Create the wheels and axles as single components first\n", + "# Look up the specific item you want from the provided json files,\n", + "# we can load the full file into a dict\n", + "with open(\"datasheets/axles.json\") as json_file:\n", " axles = json.load(json_file)\n", "# Pick a specific axle via its 'item number'\n", - "print(axles['32073'])\n", + "print(axles[\"32073\"])\n", "\n", "# Create the component with the dict:\n", - "front_axle = LegoComponent('front axle',axles['32073'])\n", - "# Both label and the data dict are optional parameters. You can view, add or edit the properties just any dict:\n", - "print(front_axle.properties['label'])\n", - "front_axle.properties['color']= 'grey'\n", + "front_axle = LegoComponent(\"front axle\", axles[\"32073\"])\n", + "# Both label and the data dict are optional parameters.\n", + "# You can view, add or edit the properties just any dict:\n", + "print(front_axle.properties[\"label\"])\n", + "front_axle.properties[\"color\"] = \"grey\"\n", "\n", "\n", "# Create the second axle\n", "back_axle = LegoComponent()\n", - "back_axle.properties['label'] = 'back axle'\n", - "back_axle.properties.update(axles['32073']) \n", + "back_axle.properties[\"label\"] = \"back axle\"\n", + "back_axle.properties.update(axles[\"32073\"])\n", "# Do not use = here, otherwise you'd overwrite existing properties.\n", "# Instead use update to have all entries added to properties\n", - "back_axle.properties['color'] = 'grey'\n", - "# Viewing dicts in one line is not easy to read, a better output comes with pretty print (pprint): \n", + "back_axle.properties[\"color\"] = \"grey\"\n", + "# Viewing dicts in one line is not easy to read,\n", + "# a better output comes with pretty print (pprint):\n", "pprint.pprint(back_axle.properties)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "id": "b4a8e8c8", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{'category': 'wheel',\n", - " 'data source': 'https://www.bricklink.com/v2/catalog/catalogitem.page?P=2903c02#T=C',\n", - " 'delivery time [days]': 5,\n", - " 'diameter [mm]': 81.6,\n", - " 'item description': 'wheel 81,6',\n", - " 'item number': 2903,\n", - " 'label': 'front wheel',\n", - " 'mass [g]': 30.0,\n", - " 'paint': 'glossy',\n", - " 'price [Euro]': 1.31,\n", - " 'related items': 2902,\n", - " 'surface': 'rough'}\n" - ] - } - ], + "outputs": [], "source": [ "# Now wheels\n", - "with open('datasheets/wheels.json') as json_file:\n", + "with open(\"datasheets/wheels.json\") as json_file:\n", " wheels = json.load(json_file)\n", "# Adding the color here already as dict, and the surface as key-value argument.\n", "# Multiple of both parameters are supported, but only in this order.\n", - "#front_wheel = LegoComponent('front wheel', wheels['2903'],{'color':'yellow'},winter='true')\n", + "# front_wheel = LegoComponent(\n", + "# 'front wheel', wheels['2903'], {'color':'yellow'},winter='true')\n", "\n", - "front_wheel = LegoComponent('front wheel', wheels['2903'],surface='rough', paint = 'glossy')\n", + "front_wheel = LegoComponent(\n", + " \"front wheel\", wheels[\"2903\"], surface=\"rough\", paint=\"glossy\"\n", + ")\n", "pprint.pprint(front_wheel.properties)\n", "\n", - "# We included a clone function to both Lego classes, so you can easily create duplicate objects:\n", - "back_wheel = front_wheel.clone('back wheel') # Passing the new label is optional" + "# We included a clone function to both Lego classes, so you can easily\n", + "# create duplicate objects. Passing the new label is optional\n", + "back_wheel = front_wheel.clone(\"back wheel\")" ] }, { "cell_type": "code", - "execution_count": 4, + "execution_count": null, "id": "25bd06c5", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "front axle\n" - ] - } - ], + "outputs": [], "source": [ - "## Create subassemblies and add the wheels and axles\n", + "# Create subassemblies and add the wheels and axles\n", "\n", - "# The AggregationLayer must be used, passing the label, or additional properties is optional\n", - "front_wheel_assembly = LegoAssembly(AggregationLayer.SUBASSEMBLY, 'front wheel assembly', \n", - " assembly_method='stick together like lego blocks')\n", + "# The AggregationLayer must be used, passing the label\n", + "# or additional properties is optional\n", + "front_wheel_assembly = LegoAssembly(\n", + " AggregationLayer.SUBASSEMBLY,\n", + " \"front wheel assembly\",\n", + " assembly_method=\"stick together like lego blocks\",\n", + ")\n", "# Add LegoComponents to the LegoAssembly, single or as list\n", - "front_wheel_assembly.add([front_wheel,front_axle])\n", + "front_wheel_assembly.add([front_wheel, front_axle])\n", "# You can access the components of an assembly like this:\n", - "print(front_wheel_assembly.components[1].properties['label'])\n", + "print(front_wheel_assembly.components[1].properties[\"label\"])\n", "\n", - "# Assemblies can be cloned as well (including their children), but don't forget to adjust \n", - "# labels or you might be stuck with a 'front wheel' in your 'back wheel assembly'\n", + "# Assemblies can be cloned as well (including their children),\n", + "# but don't forget to adjust labels or you might be stuck with\n", + "# a 'front wheel' in your 'back wheel assembly'\n", "\n", "# Stick together back wheel parts\n", - "back_wheel_assembly = LegoAssembly(AggregationLayer.SUBASSEMBLY, 'back wheel assembly')\n", - "back_wheel_assembly.add([back_wheel,back_axle])" + "back_wheel_assembly = LegoAssembly(\n", + " AggregationLayer.SUBASSEMBLY, \"back wheel assembly\"\n", + " )\n", + "back_wheel_assembly.add([back_wheel, back_axle])" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "id": "2b6648e1", "metadata": {}, "outputs": [], "source": [ - "## Create frame component and assemble the system\n", + "# Create frame component and assemble the system\n", "\n", - "with open('datasheets/frame.json') as json_file:\n", + "with open(\"datasheets/frame.json\") as json_file:\n", " frame = json.load(json_file)\n", - "scooter_frame = LegoComponent('scooter frame', frame['3702'], {'color' : 'red'})\n", + "scooter_frame = LegoComponent(\"scooter frame\", frame[\"3702\"], {\"color\": \"red\"})\n", "\n", "# The scooter is our system level assembly, you can choose the AggregationLayer\n", "# But the hiercarchy (SYSTEM > ASSEMBLY > SUBASSEMBLY) must be in order.\n", "# Components can be added to all LegoAssembly objects\n", "\n", - "scooter = LegoAssembly(AggregationLayer.SYSTEM,'scooter', manufacturer='FST', comment='Faster! Harder! Scooter!')\n", + "scooter = LegoAssembly(\n", + " AggregationLayer.SYSTEM,\n", + " \"scooter\",\n", + " manufacturer=\"FST\",\n", + " comment=\"Faster! Harder! Scooter!\",\n", + ")\n", "# add frame and subassemblies\n", "scooter.add([scooter_frame, front_wheel_assembly, back_wheel_assembly])" ] }, { "cell_type": "code", - "execution_count": 6, + "execution_count": null, "id": "71324895", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "LegoAssembly faster, harder, scooter [faaf8065-7a4b-4d5b-8ede-72f051cca87f]\n", - "├── LegoAssembly front wheel assembly [97a43799-b91b-49b5-8d78-38bb72257931]\n", - "│ ├── LegoComponent front wheel [9cfcfc76-7ce1-4066-a8ab-ebf30f59683d]\n", - "│ └── LegoComponent front axle [010ba850-ca1b-41e2-90b9-13a3528c49e8]\n", - "├── LegoAssembly back wheel assembly [82f000db-da62-44bd-993d-b8042baf03d3]\n", - "└── LegoComponent scooter frame [87489434-ba11-42d2-91b2-bbf9735b7091]\n" - ] - } - ], + "outputs": [], "source": [ - "## Look at the assembly\n", + "# Look at the assembly\n", "\n", - "# We can get all LegoComponents from this assembly. \n", + "# We can get all LegoComponents from this assembly.\n", "# Without parameter 'max_depth' only direct children will be listed.\n", "scooter.get_component_list(5)" ] @@ -262,18 +223,10 @@ }, { "cell_type": "code", - "execution_count": 21, + "execution_count": null, "id": "7b60d0fb", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "You called the test function.\n" - ] - } - ], + "outputs": [], "source": [ "# test the import\n", "calculation_rules.test_function()" @@ -281,68 +234,58 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "id": "fe4edad6", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Gesamtgewicht: 33.51 g\n" - ] - } - ], + "outputs": [], "source": [ - "## Add mass to assemblies\n", + "# Add mass to assemblies\n", + "\n", "# List all components' mass\n", "combined_weight = 0\n", - "for c in scooter.get_component_list(100):\n", - " combined_weight += c.properties['mass [g]']\n", - "print(\"Gesamtgewicht: \",combined_weight,\"g\")\n", + "for c in scooter.get_component_list(-1):\n", + " combined_weight += c.properties[\"mass [g]\"]\n", + "print(\"Gesamtgewicht: \", combined_weight, \"g\")\n", "# Add KPI to system\n", - "scooter.properties['mass [g]'] = combined_weight\n", + "scooter.properties[\"mass [g]\"] = combined_weight\n", "\n", - "# We can also add the mass to the subassemblies\n", - "# children() returns a dict with a list added parts\n", - "for a in scooter.children()['assemblies']:\n", + "# We can also add the mass to the subassemblies.\n", + "# children() returns a dict with a list of added\n", + "# components and assemblies.\n", + "for a in scooter.children()[\"assemblies\"]:\n", " a_mass = 0\n", - " for c in a.get_component_list(99):\n", - " a_mass += c.properties['mass [g]']\n", - " a.properties['mass [g]'] = a_mass\n", - " \n" + " for c in a.get_component_list(-1):\n", + " a_mass += c.properties[\"mass [g]\"]\n", + " a.properties[\"mass [g]\"] = a_mass" ] }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, + "id": "c26b36c8", + "metadata": {}, + "outputs": [], + "source": [ + "scooter.get_component_list(-1)" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "4d56419f", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "LegoAssembly faster, harder, scooter [faaf8065-7a4b-4d5b-8ede-72f051cca87f]\n", - "├── LegoAssembly front wheel assembly [97a43799-b91b-49b5-8d78-38bb72257931]\n", - "│ ├── LegoComponent front wheel [9cfcfc76-7ce1-4066-a8ab-ebf30f59683d]\n", - "│ └── LegoComponent front axle [010ba850-ca1b-41e2-90b9-13a3528c49e8]\n", - "├── LegoAssembly back wheel assembly [82f000db-da62-44bd-993d-b8042baf03d3]\n", - "└── LegoComponent scooter frame [87489434-ba11-42d2-91b2-bbf9735b7091]\n", - "LegoAssembly front wheel assembly [97a43799-b91b-49b5-8d78-38bb72257931]\n", - "LegoAssembly faster, harder, scooter [faaf8065-7a4b-4d5b-8ede-72f051cca87f]\n" - ] - } - ], + "outputs": [], "source": [ - "## Look at the full assembly with KPI\n", + "# Look at the full assembly with KPI\n", + "\n", "# Print the full assembly with all levels\n", "print_assembly_tree(scooter)" ] }, { "cell_type": "code", - "execution_count": 27, + "execution_count": null, + "id": "b31416d3", "metadata": {}, "outputs": [], "source": [ @@ -363,31 +306,29 @@ "Zusammen mit der Berechnungsvorschrift in `calculation_rules` ist auch die Entstehung des KPI nachvollziehbar und wiederverwendbar dokumentiert und damit 'FAIR'." ] }, + { + "attachments": {}, + "cell_type": "markdown", + "id": "92c77051", + "metadata": {}, + "source": [ + "#### Zusätzliche Details" + ] + }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "id": "b91fed73", "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Child: LegoAssembly front wheel assembly [97a43799-b91b-49b5-8d78-38bb72257931]\n", - "Parent: LegoAssembly faster, harder, scooter [faaf8065-7a4b-4d5b-8ede-72f051cca87f]\n", - "Top parent: LegoAssembly faster, harder, scooter [faaf8065-7a4b-4d5b-8ede-72f051cca87f]\n" - ] - } - ], + "outputs": [], "source": [ - "## Additional details\n", "# Each child knows its parent\n", - "first_child = scooter.children()['assemblies'][0]\n", + "first_child = scooter.children()[\"assemblies\"][0]\n", "print(\"Child:\", first_child)\n", "print(\"Parent:\", first_child.parent)\n", "# Also we can access the \"top\" parent\n", - "latest_child = first_child.children()['components'][0]\n", - "print(\"Top parent: \",latest_child.get_root_assembly())\n", + "latest_child = first_child.children()[\"components\"][0]\n", + "print(\"Top parent: \", latest_child.get_root_assembly())\n", "\n", "# Each part created has a unique identifier (the long number in [])\n", "# Don't try add the identical part again." -- GitLab