Skip to content
Snippets Groups Projects
Commit 8ce85863 authored by Hock, Martin's avatar Hock, Martin
Browse files

Merge branch 'development' into 'main'

Merge to main to create "WS2223" Version

See merge request fst-tuda/projects/lehre/praktikum_digitalisierung/quality-kpi!3
parents 1a8b9b89 6f74024a
No related branches found
No related tags found
No related merge requests found
......@@ -32,21 +32,23 @@ before_script:
test:
tags:
tags:
- env:docker
script:
#- python setup.py test
- pip install flake8 # you can also use tox
- pip install flake8==5.0.4 flake8-nb==0.5.2 # you can also use tox
- pwd
- ls -lh
- flake8 ./trial_json.py
- flake8 --max-line-length 88 ./functions/*.py
- flake8-nb --max-line-length 88 ausarbeitung.ipynb
run:
tags:
- env:docker
script:
- pip install -r requirements.txt
- python trial_json.py
#run:
#tags:
#- env:docker
#script:
#- pip install -r requirements.txt
#- python trial_json.py
# an alternative approach is to install and run:
# - pip install dist/*
# run the command here
......@@ -55,7 +57,7 @@ run:
# - dist/*.whl
#pages:
# tags:
# tags:
# - env:docker
# script:
# - pip install sphinx sphinx-rtd-theme
......
# Lerneinheit FAIRe Qualitäts-KPIs
## Einführung
Siehe Aufgabenstellung in Moodle.
## Materialien
Die Aufgabenstellung im PDF-Format steht in Moodle bereit.
In diesem GitLab Repo finden Sie:
- Package *functions* (`functions/`): Beinhaltet die Module *classes* und *calculation_rules*
- Modul classes (`functions/classes.py`): Werkzeuge zum Aufbau und zur Bearbeitung der LEGO Konstruktionen
- Modul calculation_rules (`functions/calculation_rules.py`): Funktionen zum Berechnen der FAIR Quality KPIs
- Datenblätter (`datasheets/`): Datenblätter ausgewählter LEGO Komponenten im JSON-Format
- Jupyter Notebook (`ausarbeitung.ipynb`): Zur Bearbeitung der Aufgaben und Abgabe. Beinhaltet ein Minimalbeispiel zur Verwendung der Werkzeuge.
## Anwendung
## Ausarbeitung
Die Ausarbeitung erfolgt im Notebook `ausarbeitung.ipynb`. In diesem ist bereits eine Gliederung vorgegeben.
......
%% Cell type:markdown id:c9328cd1 tags:
# FAIRe Qualitäts-KPIs
# FAIR Quality KPIs
Name:
Datum:
%% Cell type:markdown id:2ee8746d tags:
## Einführung
FAIRe Qualitäts-KPIs schaffen eine transparente und nachvollziehbare Entscheidungsgrundlage. In dieser Lerneinheit sollen sie die Methodik erlernen, indem sie LEGO-Autos zusammenbauen, deren Qualitäts-KPIs bestimmen und miteinander vergleichen.
FAIR Quality KPIs schaffen eine transparente und nachvollziehbare Entscheidungsgrundlage. In dieser Lerneinheit sollen sie die Methodik erlernen, indem sie LEGO Autos zusammenbauen, deren Quality KPIs bestimmen und miteinander vergleichen.
### Werkzeug für den Zusammenbau
Der Zusammenbau der Autos erfolgt virtuell. Als Werkzeug steht Ihnen das Modul `classes` zur Verfügung. In diesem sind unterschiedliche Klassen und Methoden implementiert (Objektorientierung). Sie erzeugen jeweils Objekte der Klassen. Ein Objekt ist dabei die virtuelle Abbildung eines realen Bauteiles. Die Eigenschaften des Bauteiles werden über die Eigenschaften des Objektes abgebildet.
### Berechnung der Qualitäts-KPIs
KPIs (Key Performance Indikatoren) sind Kenngrößen des Systems. Sie werden über Berechnungsvorschriften (`calculation_rules`) bestimmt. Diese Berechnungsvorschriften sind von Ihnen als python-Funktionen im Modul `calculation_results` zu implementieren.
### Berechnung der Quality KPIs
KPIs (Key Performance Indikatoren) sind Kenngrößen des Systems. Sie werden über Berechnungsvorschriften bestimmt. Diese Berechnungsvorschriften sind von Ihnen als python-Funktionen im Modul `calculation_rules` zu implementieren.
### Datenblätter
Für den Zusammenbau stehen Ihnen verschiedene Bauteile in unterschiedlichen Ausführungen zur Verfügung. Sie nutzen die Datenblätter, die als `json-Dateien` zur Verfügung gestellt werden, um Ihre Autos zusammenzubauen.
%% Cell type:markdown id:1c702114 tags:
## Eigene Module und Minimalbeispiel
Für die Ausarbeitung nutzen wir zwei eigene Module (Python-Skripte), die im Ordner `functions` gespeichert sind.
Das Modul `calculation_rules`erweitern Sie während der Ausarbeitung. Um die Änderungen zu nutzen, müssen Sie das Notebook neu starten.
Für die Ausarbeitung nutzen wir zwei eigene Module (Python-Dateien), die im Ordner `functions` gespeichert sind.
Das Modul `calculation_rules` erweitern Sie während der Ausarbeitung. Um die Änderungen zu nutzen, müssen Sie das Notebook neu starten.
Im Modul `classes` befinden sich die komplette Klassen und Funktionen zur Verwendung.
Mit einem Minimalbeispiel wird Ihnen gezeigt, wie sie die Module nutzen.
%% Cell type:markdown id:6d3310be tags:
### Modul classes
Enthält `LegoComponent, LegoAssembly, AggregationLayer, KPIEncoder` und die Funktion `print_assembly_tree`.
`LegoComponent` bildet einzelne Komponenten ab, während `LegoAssembly` zusammengesetzte Aggregationsebenen abdeckt, also Bauteil, Baugruppe und System. Zur Unterscheidung dient die Klasse Aggregationlayer, diese ist für `LegoComponent` immer `Component` (Komponente), muss für `LegoAssembly` entsprechend auf `SYSTEM`(System) , `ASSEMBLY`(Baugruppe) oder `SUBASSEMBLY`(Bauteil) gesetzt werden.
Wir bauen aus Achse, Rahmen und Reifen einen Tretroller zusammen.
%% Cell type:code id:b2778dee tags:
``` python
# import modules
import json
import pprint
from functions import calculation_rules
from functions import classes
# TO DO import further modules if necessary
# Importing all modules one by one to provide an overview
# The next commented line would provide the same result in one line
# from functions.classes import *
from functions.classes import LegoComponent
from functions.classes import LegoAssembly
from functions.classes import AggregationLayer
from functions.classes import KPIEncoder
from functions.classes import print_assembly_tree
# When you are writing code yourself later, you might want to copy
# these imports to avoid rerunning the full notebook after restart
```
%% Cell type:markdown id:3b69752c tags:
%% Cell type:code id:0b1f9aff tags:
``` python
# Create the wheels and axles as single components first
# Look up the specific item you want from the provided json files,
# we can load the full file into a dict
with open("datasheets/axles.json") as json_file:
axles = json.load(json_file)
# Pick a specific axle via its 'item number'
print(axles["32073"])
### Modul calculation_results
Sie können die unterschiedlichen Funktionen (Berechnungsvorschriften) aufrufen. Beachten Sie dabei die Übergabe- und Rückgabewerte.
# Create the component with the dict:
front_axle = LegoComponent("front axle", axles["32073"])
# Both label and the data dict are optional parameters.
# You can view, add or edit the properties just any dict:
print(front_axle.properties["label"])
front_axle.properties["color"] = "grey"
%% Cell type:code id:294c680b tags:
# Create the second axle
back_axle = LegoComponent()
back_axle.properties["label"] = "back axle"
back_axle.properties.update(axles["32073"])
# Do not use = here, otherwise you'd overwrite existing properties.
# Instead use update to have all entries added to properties
back_axle.properties["color"] = "grey"
# Viewing dicts in one line is not easy to read,
# a better output comes with pretty print (pprint):
pprint.pprint(back_axle.properties)
```
%% Cell type:code id:b4a8e8c8 tags:
``` python
# Now wheels
with open("datasheets/wheels.json") as json_file:
wheels = json.load(json_file)
# Adding the color here already as dict, and the surface as key-value argument.
# Multiple of both parameters are supported, but only in this order.
# front_wheel = LegoComponent(
# 'front wheel', wheels['2903'], {'color':'yellow'},winter='true')
front_wheel = LegoComponent(
"front wheel", wheels["2903"], surface="rough", paint="glossy"
)
pprint.pprint(front_wheel.properties)
# We included a clone function to both Lego classes, so you can easily
# create duplicate objects. Passing the new label is optional
back_wheel = front_wheel.clone("back wheel")
```
%% Cell type:code id:25bd06c5 tags:
``` python
# Create subassemblies and add the wheels and axles
# The AggregationLayer must be used, passing the label
# or additional properties is optional
front_wheel_assembly = LegoAssembly(
AggregationLayer.SUBASSEMBLY,
"front wheel assembly",
assembly_method="stick together like lego blocks",
)
# Add LegoComponents to the LegoAssembly, single or as list
front_wheel_assembly.add([front_wheel, front_axle])
# You can access the components of an assembly like this:
print(front_wheel_assembly.components[1].properties["label"])
# Assemblies can be cloned as well (including their children),
# but don't forget to adjust labels or you might be stuck with
# a 'front wheel' in your 'back wheel assembly'
# Stick together back wheel parts
back_wheel_assembly = LegoAssembly(
AggregationLayer.SUBASSEMBLY, "back wheel assembly"
)
back_wheel_assembly.add([back_wheel, back_axle])
```
%% Cell type:code id:2b6648e1 tags:
``` python
# Create frame component and assemble the system
with open("datasheets/frame.json") as json_file:
frame = json.load(json_file)
scooter_frame = LegoComponent("scooter frame", frame["3702"], {"color": "red"})
# The scooter is our system level assembly, you can choose the AggregationLayer
# But the hiercarchy (SYSTEM > ASSEMBLY > SUBASSEMBLY) must be in order.
# Components can be added to all LegoAssembly objects
scooter = LegoAssembly(
AggregationLayer.SYSTEM,
"scooter",
manufacturer="FST",
comment="Faster! Harder! Scooter!",
)
# add frame and subassemblies
scooter.add([scooter_frame, front_wheel_assembly, back_wheel_assembly])
```
%% Cell type:code id:71324895 tags:
``` python
# Look at the assembly
# We can get all LegoComponents from this assembly.
# Without parameter 'max_depth' only direct children will be listed.
scooter.get_component_list(5)
```
%% Cell type:markdown id:001f1c77 tags:
### Modul calculation_rules
Um für unser System "Tretroller" ein KPI für das Gesamtgewicht zu erzeugen, wurde in `functions.calculation_rules` die Funktion `kpi_sum` definiert. Zusammen mit den Hilfsfunktionen der Klasse können wir nun das KPI Gewicht für das System hinzufügen. Die Massen der einzelnen Komponenten sind in den Datenblättern unter `mass [g]` enthalten.
%% Cell type:code id:7b60d0fb tags:
``` python
# test the import
calculation_rules.test_function()
```
%% Cell type:markdown id:a2c87a3c tags:
%% Cell type:code id:fe4edad6 tags:
### Modul classes
Wir bauen ein Auto zusammen.
``` python
# Add mass to assemblies
# List all components' mass
combined_weight = 0
for c in scooter.get_component_list(-1):
combined_weight += c.properties["mass [g]"]
print("Gesamtgewicht: ", combined_weight, "g")
# Add KPI to system
scooter.properties["mass [g]"] = combined_weight
# We can also add the mass to the subassemblies.
# children() returns a dict with a list of added
# components and assemblies.
for a in scooter.children()["assemblies"]:
a_mass = 0
for c in a.get_component_list(-1):
a_mass += c.properties["mass [g]"]
a.properties["mass [g]"] = a_mass
```
%% Cell type:code id:8db386db tags:
%% Cell type:code id:c26b36c8 tags:
``` python
scooter.get_component_list(-1)
```
%% Cell type:code id:8f2ae9f4 tags:
%% Cell type:code id:4d56419f tags:
``` python
# Look at the full assembly with KPI
# Print the full assembly with all levels
print_assembly_tree(scooter)
```
%% Cell type:code id:b31416d3 tags:
``` python
# Dump to json file with to_dict() and KPIEncoder
with open("scooter.json", "w") as fp:
json.dump(scooter.to_dict(), fp, cls=KPIEncoder)
# full view is too big for Juypter (try it)
# pprint.pprint(scooter.to_dict())
```
%% Cell type:markdown id:53793ae8 tags:
In dieser exportierten json-Datei ('scooter.json') sind die Werte maschinen- und menschenlesbar hinterlegt.
Zusammen mit der Berechnungsvorschrift in `calculation_rules` ist auch die Entstehung des KPI nachvollziehbar und wiederverwendbar dokumentiert und damit 'FAIR'.
%% Cell type:markdown id:92c77051 tags:
#### Zusätzliche Details
%% Cell type:code id:b91fed73 tags:
``` python
# Each child knows its parent
first_child = scooter.children()["assemblies"][0]
print("Child:", first_child)
print("Parent:", first_child.parent)
# Also we can access the "top" parent
latest_child = first_child.children()["components"][0]
print("Top parent: ", latest_child.get_root_assembly())
# Each part created has a unique identifier (the long number in [])
# Don't try add the identical part again.
```
%% Cell type:markdown id:de070039 tags:
## Aufgabe: Aufbau eines ersten Fahrzeugs
Bauen Sie ein erstes Fahrzeug aus den gegebenen LEGO-Teilen auf.
Das Fahrzeug muss aus Baugruppen, Bauteilen und Komponenten bestehen. Es muss mindestens vier Räder haben und sich durch den elektrischen Antrieb fortbewegen können.
%% Cell type:markdown id:05a8eb21 tags:
### Beschreibung des Fahrzeuges
Beschreiben Sie kurz und präzise Ihr Fahrzeug.
%% Cell type:code id:ccaf3043 tags:
``` python
# initialize componentens
```
%% Cell type:code id:1ea61422 tags:
``` python
# read out properties from datasheets
```
%% Cell type:code id:36f981df tags:
``` python
# set properties
```
%% Cell type:code id:24400d94 tags:
``` python
# export car and its properties
```
%% Cell type:markdown id:c1fef7f0 tags:
## Aufgabe: Bestimmung der Qualität mittels KPIs
Bestimmen Sie die Qualität Ihres Fahrzeugs mittels KPIs.
Die Qualität des Fahrzeugs ist mit mindestens einem KPI je Qualitätsdimension (Aufwand, Verfügbarkeit, Akzeptanz) zu bestimmen. Enwickeln Sie zunächst sinnvolle KPIs, welche mit den gegebenen Daten umsetzbar sind. Halten Sie die Berechnungsvorschriften im Jupyter Notebook fest. Implementieren Sie deren Berechnung für das Gesamtsystem "Fahrzeug" mittels einzelner Funktionen im Skript `calculation_rules`. Sie können zusätzlich Ihre Methoden auch auf die niedrigeren Aggregationsebenen anwenden.
%% Cell type:markdown id:d5f02096 tags:
### Beschreibung KPI
Beschreiben Sie den jeweiligen KPI und geben Sie seine Berechnungsvorschrift an.
$$
a = \frac{b}{c} + d
$$
%% Cell type:code id:59eabafc tags:
``` python
# calculate the KPIs for your car
```
%% Cell type:code id:c774b381 tags:
``` python
# print your KPIs
```
%% Cell type:markdown id:89c75440 tags:
## Aufgabe: Aufbau eines zweiten Fahrzeugs
Setzen Sie sich ein Ziel, welche Qualitätsdimensionen in einem zweiten Fahrzeug verbessert werden sollen und bauen Sie dementsprechend ein zweites Fahrzeug aus den gegebenen LEGO-Teilen auf.
Das Fahrzeug muss aus Baugruppen, Bauteilen und Komponenten bestehen. Es muss mindestens vier Räder haben und sich durch den elektrischen Antrieb fortbewegen können. Die Verwendung eines Getriebes zwischen Motor und Antriebsachse(n) ist verpflichtend. Es sind nur die gegebenen LEGO-Teile zu verwenden.
%% Cell type:markdown id:3cef0828 tags:
### Beschreibung
Beschreiben Sie, welche Verbesserung Sie vornehmen.
...
%% Cell type:markdown id:73c454f2 tags:
### Aufbau des zweiten Fahrzeuges
Beschreiben Sie kurz und präzise den Aufbau des zweiten Fahrzeugs
%% Cell type:code id:ea18e735 tags:
``` python
# build a second car
```
%% Cell type:code id:b3438fc4 tags:
``` python
# read out properties from datasheets
```
%% Cell type:code id:0b7336fb tags:
``` python
# set properties
```
%% Cell type:code id:446abbca tags:
``` python
# export car and its properties
```
%% Cell type:markdown id:89e54480 tags:
### KPIs des zweiten Fahrzeuges
Bestimmen Sie die KPIs des zweiten Fahrzeuges
%% Cell type:code id:762a1e93 tags:
``` python
# calculate the KPIs for your car
```
%% Cell type:code id:1ed67328 tags:
``` python
# print your KPIs
```
%% Cell type:markdown id:e413cd84 tags:
## Aufgabe: Darstellung und Vergleich der Ergebnisse
Stellen Sie die Ergebnisse für beide Fahrzeuge übersichtlich dar.
Die entwickelten KPIs müssen dargestellt und die Datengrundlage ersichtlich sein.
Eine geeignete grafische Darstellung für die Gegenüberstellung der Ergebnisse für beide Fahrzeuge ist zu wählen.
%% Cell type:code id:b0f93e22 tags:
``` python
# plot the data, save diagramm as svg-file
```
%% Cell type:markdown id:62ff04b2 tags:
### Interpretation der Ergebnisse
Interpretieren Sie ihre Ergebnisse. Vergleichen Sie die KPIs ihrer Autos. Konnten Sie ihre gewünschte Verbesserung erzielen?
%% Cell type:markdown id:a0840fbf tags:
## Aufgabe: Exportieren Sie ihre Daten
Exportieren/ Speichern Sie alle relevaten Daten ab.
%% Cell type:code id:a4bdfc7a tags:
``` python
# export all relevant data
```
......
{
"32073":{
"item number":32073,
"item description":"Axle 5 studs",
"category":"axle",
"price [Euro]":0.001,
"mass [g]":0.66,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32073#T=S&O={%22iconly%22:0}",
"dimension [studs]":5
},
"44294":{
"item number":44294,
"item description":"Axle 7 studs",
"category":"axle",
"price [Euro]":0.01,
"mass [g]":1.05,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=44294#T=S&O={%22iconly%22:0}",
"dimension [studs]":7
},
"3707":{
"item number":3707,
"item description":"Axle 8 studs",
"category":"axle",
"price [Euro]":0.01,
"mass [g]":1.18,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3707#T=S&O={%22iconly%22:0}",
"dimension [studs]":8
},
"60485":{
"item number":60485,
"item description":"Axle 9 studs",
"category":"axle",
"price [Euro]":0.01,
"mass [g]":1.3,
"delivery time [days]":7,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=60485#T=S&O={%22iconly%22:0}",
"dimension [studs]":9
},
"3737":{
"item number":3737,
"item description":"Axle 10 studs",
"category":"axle",
"price [Euro]":0.01,
"mass [g]":1.49,
"delivery time [days]":7,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3737#T=S&O={%22iconly%22:0}",
"dimension [studs]":10
},
"23948":{
"item number":23948,
"item description":"Axle 11 studs",
"category":"axle",
"price [Euro]":0.15,
"mass [g]":1.65,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=23948#T=S&O={%22iconly%22:0}",
"dimension [studs]":11
},
"3708":{
"item number":3708,
"item description":"Axle 12 studs",
"category":"axle",
"price [Euro]":0.02,
"mass [g]":1.82,
"delivery time [days]":7,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3708#T=S&O={%22iconly%22:0}",
"dimension [studs]":12
},
"50451":{
"item number":50451,
"item description":"Axle 16 studs",
"category":"axle",
"price [Euro]":0.75,
"mass [g]":2.37,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=50451#T=S&O={%22iconly%22:0}",
"dimension [studs]":16
}
}
\ No newline at end of file
{
"8878-1":{
"item number":"8878-1",
"item description":"Power Functions Rechargeable Battery Box",
"category":"battery",
"price [Euro]":55,
"mass [g]":83.94,
"delivery time [days]":8,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?S=8878-1#T=S&O={%22iconly%22:0}",
"output voltage [V]":7.4,
"dimensions [cm]":"15 x 15 x 3,5"
},
"8881-1":{
"item number":"8881-1",
"item description":"Power Functions Battery Box",
"category":"battery",
"price [Euro]":12,
"mass [g]":179.4,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?S=8881-1#T=S&O={%22iconly%22:0}",
"output voltage [V]":9.0,
"dimensions [cm]":"8,8 x 6,3 x 3,2"
},
"88000-1":{
"item number":"88000-1",
"item description":"Lego AAA Battery Box",
"category":"battery",
"price [Euro]":30,
"mass [g]":97.24,
"delivery time [days]":6,
"data source":"https:\/\/www.brickowl.de\/catalog\/lego-aaa-battery-box-set-88000",
"output voltage [V]":9.0,
"dimensions [cm]":"15,3 x 15,1 x 3,9"
},
"2847c01":{
"item number":"2847c01",
"item description":"Electric 9V Battery Box",
"category":"battery",
"price [Euro]":2,
"mass [g]":172.7,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=2847c01&name=Electric%209V%20Battery%20Box%204%20x%2014%20x%204%20with%20Red%20Buttons%20and%20Contact%20Plate%20with%20Dark%20Gray%20Base%20(2846%20\/%202847c00)&category=%5BElectric,%20Battery%20Box%5D#T=S&O={%22iconly%22:0}",
"output voltage [V]":9.0,
"dimensions [cm]":"11,2 x 3,2 x 3,2"
},
"59510":{
"item number":59510,
"item description":"LEGO Power Functions Battery Box ",
"category":"battery",
"price [Euro]":18,
"mass [g]":165.3,
"delivery time [days]":3,
"data source":"https:\/\/www.brickowl.de\/catalog\/lego-power-functions-battery-box-with-beam-connectors-with-on-off-sticker-59510",
"output voltage [V]":9.0,
"dimensions [cm]":"8,8 x 6,4 x 3,2"
},
"84599":{
"item number":84599,
"item description":"Electric 9V Power Functions Battery Box (Rechargeable)",
"category":"battery",
"price [Euro]":120,
"mass [g]":82.0,
"delivery time [days]":8,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=84599&name=Electric%209V%20Battery%20Box%20Power%20Functions%20(Rechargeable)%20with%20Dark%20Bluish%20Gray%20Bottom&category=%5BElectric,%20Battery%20Box%5D#T=S&O={%22iconly%22:0}",
"output voltage [V]":9.0,
"dimensions [cm]":"15 x 15 x 3,5"
}
}
\ No newline at end of file
"""Create json metadata files from an excel file."""
import os
import sys
from collections import namedtuple
from typing import Dict, List, NamedTuple
import pandas as pd
# pandas is using another dependency called openpyxl
# both need to be installed
COLUMN_FOR_INDEX_IN_EXCEL = 0
KEYWORD_TO_IGNORE_COLUMN = "ignore"
KEYWORD_TO_IGNORE_SHEET = "ignore"
HELP_TXT = """
1. The source excel file. (e.g. test.xlsx)
2. The destination folder for the json file. (e.g. test_folder)
3. Override the folder if existing. (OPTIONAL, False)
Providing only '-- help' will show this text and exit.
"""
def read_terminal_arguments() -> NamedTuple:
"""Read terminal arguments.
1. File to read from.
2. Name of the folder to store the json files.
3. Override the destination folder if existing, defaults False.
"""
terminal_args = namedtuple(
"terminal_args",
["source", "destination", "override"],
defaults=["test.xlsx", "test", True]
)
if len(sys.argv) == 1: # No arguments passed.
return terminal_args() # noqa
if sys.argv[1] in ["help", "--help", "-h", "?", "h"]:
print(HELP_TXT)
raise SystemExit
try:
arg_1 = sys.argv[1]
except IndexError:
raise SystemExit
try:
arg_2 = sys.argv[2]
except IndexError:
raise SystemExit
try:
arg_3 = sys.argv[3]
except IndexError:
arg_3 = False
terminal_args = terminal_args(
source=arg_1,
destination=arg_2,
override=bool(arg_3)
)
return terminal_args
def read_excel_file(path: str) -> Dict[str, pd.DataFrame]:
"""Read the sheets of the Excel file into multiple dataframes."""
if not os.path.exists(path):
raise FileNotFoundError(
f"No file found at '{path}'"
)
sheets_as_dataframes = {}
with pd.ExcelFile(path) as xls:
for sheet_name in xls.sheet_names:
if sheet_name.startswith(KEYWORD_TO_IGNORE_SHEET):
continue
excel_file = pd.read_excel(
xls,
sheet_name,
index_col=COLUMN_FOR_INDEX_IN_EXCEL
)
sheets_as_dataframes.update({sheet_name: excel_file})
return sheets_as_dataframes
def make_json(dataframes: Dict[str, pd.DataFrame]) -> Dict[str, str]:
"""Create the json strings from the excel data."""
json_files = {}
for sheet_name, dataframe in dataframes.items():
dataframe.dropna(inplace=True, axis=1)
dropped_columns = [column for column in dataframe.columns
if column.startswith(KEYWORD_TO_IGNORE_COLUMN)]
dataframe.drop(dropped_columns, axis=1, inplace=True,
errors="ignore")
print(sheet_name)
json_string = dataframe.to_json(orient="index", indent=4, force_ascii=False)
json_files.update({sheet_name: json_string})
return json_files
def save_json_files(folder_name: str, jsons_files: Dict[str, str],
override_files: bool) -> None:
"""Save the json strings to a '.json' file format."""
path_to_json_folder = os.path.join(
os.path.abspath(__file__),
os.pardir,
folder_name
)
path_to_json_folder = os.path.abspath(path_to_json_folder)
path_exists = os.path.exists(path_to_json_folder)
if path_exists and not override_files:
raise FileExistsError(
f"Folder: {path_to_json_folder} exists. Pass override argument "
f"to override the folder. See --help for more."
)
if not path_exists:
os.mkdir(path_to_json_folder)
for json_name, json_sting in jsons_files.items():
file_name = f"{json_name}.json"
file_path = os.path.join(path_to_json_folder, file_name)
with open(file_path, "w") as f:
f.write(json_sting)
def main():
"""The main function."""
# Get terminal arguments.
terminal_arguments = read_terminal_arguments()
# Read the Excel file.
path_to_file = os.path.abspath(os.path.join(
os.path.abspath(__file__),
os.pardir,
terminal_arguments.source)
)
excel_sheets_as_dataframes = read_excel_file(path_to_file)
excel_sheets_as_json = make_json(excel_sheets_as_dataframes)
save_json_files(terminal_arguments.destination, excel_sheets_as_json,
terminal_arguments.override)
if __name__ == "__main__":
t = main()
{
"Pumps":[
{"Name": "Pump_1",
"Manufacturer": "Company A",
"Unit": "Percentage",
"Efficiency": 43},
{"Name": "Pump_2",
"Manufacturer": "Company B",
"Unit": "Percentage",
"Efficiency": 56
}
]
}
{
"39790":{
"item number":39790,
"item description":"Technic, Liftarm, Modified Frame Thick 11 x 15 Open Center",
"category":"frame",
"price [Euro]":2.19,
"mass [g]":12.96,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=39790#T=C",
"dimension [studs]":"11 x 15 x 1"
},
"32532":{
"item number":32532,
"item description":"Technic, Brick 6 x 8 Open Center",
"category":"frame",
"price [Euro]":0.18,
"mass [g]":8.0,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32532#T=C",
"dimension [studs]":"6 x 8 x 1"
},
"32531":{
"item number":32531,
"item description":"Technic, Brick 4 x 6 Open Center",
"category":"frame",
"price [Euro]":0.1,
"mass [g]":5.0,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32531&idColor=11#T=C&C=11",
"dimension [studs]":"4 x 6 x 1"
},
"3700":{
"item number":3700,
"item description":"Technic, Brick 1 x 2 with Hole",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.82,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3700#T=C",
"dimension [studs]":"1 x 2"
},
"3701":{
"item number":3701,
"item description":"Technic, Brick 1 x 4 with Holes",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":1.46,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3701#T=C",
"dimension [studs]":"1 x 4 x 1"
},
"3702":{
"item number":3702,
"item description":"Technic, Brick 1 x 8 with Holes",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":2.85,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3702#T=C",
"dimension [studs]":"1 x 8 x 1"
},
"2730":{
"item number":2730,
"item description":"Technic, Brick 1 x 10 with Holes",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":3.67,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=2730#T=C",
"dimension [studs]":"1 x 10 x 1"
},
"3895":{
"item number":3895,
"item description":"Technic, Brick 1 x 12 with Holes",
"category":"frame",
"price [Euro]":0.03,
"mass [g]":4.2,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3895#T=C",
"dimension [studs]":"1 x 12 x 1"
},
"32018":{
"item number":32018,
"item description":"Technic, Brick 1 x 14 with Holes",
"category":"frame",
"price [Euro]":0.03,
"mass [g]":4.92,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32018#T=C",
"dimension [studs]":"1 x 14 x 1"
},
"3703":{
"item number":3703,
"item description":"Technic, Brick 1 x 16 with Holes",
"category":"frame",
"price [Euro]":0.07,
"mass [g]":5.87,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3703#T=C",
"dimension [studs]":"1 x 16 x 1"
},
"32524":{
"item number":32524,
"item description":"Technic, Liftarm Thick 1 x 7",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":1.79,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32524&name=Technic,%20Liftarm%20Thick%201%20x%207&category=%5BTechnic,%20Liftarm%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"1 x 7"
},
"40490":{
"item number":40490,
"item description":"Technic, Liftarm Thick 1 x 9",
"category":"frame",
"price [Euro]":0.02,
"mass [g]":2.59,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=40490&name=Technic,%20Liftarm%20Thick%201%20x%209&category=%5BTechnic,%20Liftarm%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"1 x 9"
},
"32525":{
"item number":32525,
"item description":"Technic, Liftarm Thick 1 x 11",
"category":"frame",
"price [Euro]":0.02,
"mass [g]":2.8,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32525&name=Technic,%20Liftarm%20Thick%201%20x%2011&category=%5BTechnic,%20Liftarm%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"1 x 11"
},
"41239":{
"item number":41239,
"item description":"Technic, Liftarm Thick 1 x 13",
"category":"frame",
"price [Euro]":0.05,
"mass [g]":3.3,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=41239&name=Technic,%20Liftarm%20Thick%201%20x%2013&category=%5BTechnic,%20Liftarm%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"1 x 13"
},
"32278":{
"item number":32278,
"item description":"Technic, Liftarm Thick 1 x 15",
"category":"frame",
"price [Euro]":0.05,
"mass [g]":4.0,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32278&name=Technic,%20Liftarm%20Thick%201%20x%2015&category=%5BTechnic,%20Liftarm%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"1 x 15"
},
"3713":{
"item number":3713,
"item description":"Technic Bush",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.14,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3713&name=Technic%20Bush&category=%5BTechnic%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":1
},
"32123":{
"item number":32123,
"item description":"Technic Bush 1\/2 Smooth",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.01,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=4265c&name=Technic%20Bush%201\/2%20Smooth&category=%5BTechnic%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":1
},
"3749":{
"item number":3749,
"item description":"Technic, Axle 1L with Pin without Friction Ridges",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.22,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3749&name=Technic,%20Axle%20%201L%20with%20Pin%20without%20Friction%20Ridges&category=%5BTechnic,%20Axle%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"0,75 x 2 x 0,55"
},
"6538":{
"item number":6538,
"item description":"Technic, Axle Connector 2L (Ridged Undetermined Type)",
"category":"frame",
"price [Euro]":0.03,
"mass [g]":0.4,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=6538#T=C",
"dimension [studs]":"?"
},
"6536":{
"item number":6536,
"item description":"Technic, Axle and Pin Connector Perpendicular",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.39,
"delivery time [days]":7,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=6536&name=Technic,%20Axle%20and%20Pin%20Connector%20Perpendicular&category=%5BTechnic,%20Connector%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"1x 2 x 1"
},
"32138":{
"item number":32138,
"item description":"Technic, Pin Double with Axle Hole",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.96,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32138&name=Technic,%20Pin%20Double%20with%20Axle%20Hole&category=%5BTechnic,%20Pin%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"?"
},
"14720":{
"item number":14720,
"item description":"Technic, Liftarm, Modified H-Shape Thick 3 x 5 Perpendicular",
"category":"frame",
"price [Euro]":0.08,
"mass [g]":2.29,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=14720&name=Technic,%20Liftarm,%20Modified%20H-Shape%20Thick%203%20x%205%20Perpendicular&category=%5BTechnic,%20Liftarm%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"3 x 5 x 1"
},
"48989":{
"item number":48989,
"item description":"Technic, Pin Connector Perpendicular 3L with 4 Pins",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":1.22,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=48989&name=Technic,%20Pin%20Connector%20Perpendicular%203L%20with%204%20Pins&category=%5BTechnic,%20Connector%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"3 x 3 x 1"
},
"55615":{
"item number":55615,
"item description":"Technic, Pin Connector Perpendicular 3 x 3 Bent with 4 Pins",
"category":"frame",
"price [Euro]":0.1,
"mass [g]":1.9,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=55615&name=Technic,%20Pin%20Connector%20Perpendicular%203%20x%203%20Bent%20with%204%20Pins&category=%5BTechnic,%20Connector%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":"4 x 4 x 1"
},
"32556":{
"item number":32556,
"item description":"Technic, Pin 3L without Friction Ridges",
"category":"frame",
"price [Euro]":0.1,
"mass [g]":0.25,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32556&name=Technic,%20Pin%203L%20without%20Friction%20Ridges&category=%5BTechnic,%20Pin%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":1
},
"3673":{
"item number":3673,
"item description":"Technic, Pin without Friction Ridges",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.16,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3673&name=Technic,%20Pin%20without%20Friction%20Ridges&category=%5BTechnic,%20Pin%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":1
},
"32054":{
"item number":32054,
"item description":"Technic, Pin 4L with Friction Ridges and Stop Bush",
"category":"frame",
"price [Euro]":0.01,
"mass [g]":0.33,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32054&name=Technic,%20Pin%203L%20with%20Friction%20Ridges%20and%20Stop%20Bush&category=%5BTechnic,%20Pin%5D#T=S&O={%22iconly%22:0}",
"dimension [studs]":1
}
}
\ No newline at end of file
{
"3647":{
"item number":3647,
"item description":"Gear 8 Tooth",
"category":"gear",
"price [Euro]":0.14,
"mass [g]":0.16,
"delivery time [days]":11,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3647#T=C"
},
"94925":{
"item number":94925,
"item description":"Gear 16 Tooth",
"category":"gear",
"price [Euro]":0.2,
"mass [g]":0.7,
"delivery time [days]":12,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=94925#T=C"
},
"32269":{
"item number":32269,
"item description":"Gear 20 Tooth",
"category":"gear",
"price [Euro]":0.36,
"mass [g]":1.4,
"delivery time [days]":13,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32269#T=C"
},
"3648":{
"item number":3648,
"item description":"Gear 24 Tooth",
"category":"gear",
"price [Euro]":0.32,
"mass [g]":1.17,
"delivery time [days]":11,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3648#T=C"
},
"3650":{
"item number":3650,
"item description":"Gear 24 Tooth Crown",
"category":"gear",
"price [Euro]":0.09,
"mass [g]":1.03,
"delivery time [days]":13,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3650#T=C"
},
"3649":{
"item number":3649,
"item description":"Gear 40 Tooth",
"category":"gear",
"price [Euro]":0.81,
"mass [g]":3.76,
"delivery time [days]":11,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3649#T=C"
},
"32498":{
"item number":32498,
"item description":"Gear 36 Tooth",
"category":"gear",
"price [Euro]":0.88,
"mass [g]":3.5,
"delivery time [days]":12,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=32498#T=C"
},
"6588":{
"item number":6588,
"item description":"Gear Worm Gearbox",
"category":"gear",
"price [Euro]":1.63,
"mass [g]":4.5,
"delivery time [days]":11,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=6588#T=C"
},
"4716":{
"item number":4716,
"item description":"Gear Worm Screw",
"category":"gear",
"price [Euro]":0.54,
"mass [g]":0.6,
"delivery time [days]":12,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=4716#T=C"
}
}
\ No newline at end of file
File added
File added
{
"8882-1":{
"item number":"8882-1",
"item description":"Power Functions XL-Motor",
"category":"motor",
"related items":8881,
"idle current [mA]":80,
"idle speed [rev per min]":220,
"locking torque [Ncm]":40,
"price [Euro]":30,
"mass [g]":72.85,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?S=8882-1&name=Power%20Functions%20XL-Motor&category=%5BPower%20Functions%5D#T=S&O={%22iconly%22:0}",
"compatible battery":8881,
"input voltage [V]":9,
"dimensions [cm]":"13.6 x 12.2 x 3.9"
},
"8883-1":{
"item number":"8883-1",
"item description":"Power Functions M-Motor",
"category":"motor",
"related items":"8881, 8878, 45517, 88000",
"idle current [mA]":65,
"idle speed [rev per min]":405,
"locking torque [Ncm]":11,
"price [Euro]":20,
"mass [g]":35.0,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?S=8883-1&name=Power%20Functions%20M-Motor&category=%5BPower%20Functions%5D#T=S&O={%22iconly%22:0}",
"compatible battery":"8881, 8878, 45517, 88000",
"input voltage [V]":9,
"dimensions [cm]":"13.8 x 11.9 x 2.3"
},
"88003-1":{
"item number":"88003-1",
"item description":"Power Functions L-Motor",
"category":"motor",
"related items":"8881, 8878, 88000",
"idle current [mA]":120,
"idle speed [rev per min]":390,
"locking torque [Ncm]":18,
"price [Euro]":15,
"mass [g]":48.0,
"delivery time [days]":3,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?S=88003-1&name=Power%20Functions%20L-Motor&category=%5BPower%20Functions%5D#T=S&O={%22iconly%22:0}",
"compatible battery":"8881, 8878, 88000",
"input voltage [V]":9,
"dimensions [cm]":"13 x 11 x 2.5"
}
}
\ No newline at end of file
{
"4265":{
"item number":4265,
"item description":"wheel 14",
"category":"wheel",
"related items":59895,
"price [Euro]":0.02,
"mass [g]":0.5,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=4265cc01#T=C",
"diameter [mm]":14.0
},
"3482":{
"item number":3482,
"item description":"wheel 24",
"category":"wheel",
"related items":3483,
"price [Euro]":0.01,
"mass [g]":3.0,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=3482c01#T=C",
"diameter [mm]":24.0
},
"56904":{
"item number":56904,
"item description":"wheel 43,2",
"category":"wheel",
"related items":30699,
"price [Euro]":0.11,
"mass [g]":13.0,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=56904c02#T=C",
"diameter [mm]":43.2
},
"41896":{
"item number":41896,
"item description":"wheel 56",
"category":"wheel",
"related items":41897,
"price [Euro]":0.45,
"mass [g]":23.0,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=41896c04#T=C",
"diameter [mm]":56.0
},
"2903":{
"item number":2903,
"item description":"wheel 81,6",
"category":"wheel",
"related items":2902,
"price [Euro]":1.31,
"mass [g]":30.0,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=2903c02#T=C",
"diameter [mm]":81.6
},
"88517":{
"item number":88517,
"item description":"wheel 100,6",
"category":"wheel",
"related items":11957,
"price [Euro]":1.25,
"mass [g]":40.0,
"delivery time [days]":5,
"data source":"https:\/\/www.bricklink.com\/v2\/catalog\/catalogitem.page?P=88517c02#T=C",
"diameter [mm]":100.6
}
}
\ No newline at end of file
'''
"""
File consists of several functions for the calculation rules of FAIR Quality KPIs
'''
"""
def test_function():
"""Test function to check module functionality"""
print("You called the test function.")
def kpi_sum(*args):
"""
Calculates the sum of one or more integers or lists.
Args:
*args: One or more integers or lists.
Returns:
total (int): The sum of all given integers and/or the sum
of all items in all given lists.
Raises:
TypeError: If an argument with unsupported type is passed
(i.e. anything other than int or list).
"""
total = 0
for arg in args:
if isinstance(arg, int): # Check if argument is an integer
total += arg
elif isinstance(arg, list): # Check if argument is a list
total += sum(arg)
else:
raise TypeError(
f"Unsupported type {type(arg)} passed to kpi_sum()")
return total
# if arguments are handed over not as a list: sum(list(args))
# Add new functions for calculating some real complicated metrics
if __name__ == "__main__":
print("This script contains functions for calculating the FAIR Quality KPIs. It is not to be executed independently.")
pass
\ No newline at end of file
"""
Function to inform that functions in this module is
intended for import not usage as script
"""
print(
"This script contains functions for calculating the FAIR Quality KPIs."
"It is not to be executed independently."
)
'''
File consists of several classes for the different elements of a device.
'''
"""
File consists of several classes to model elements and assembly
layers of a composed device.
"""
from __future__ import annotations
import uuid
import json
import operator
from enum import Enum, auto
from typing import List, Dict, Optional
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
class AggregationLayer(Enum):
"""Describes the levl of aggregation for the objects LegoComponent
and LegoAssembly and provides the 4 applicable layers.
"""
SYSTEM = auto()
ASSEMBLY = auto()
SUBASSEMBLY = auto()
COMPONENT = auto()
class LegoComponent:
"""
A class for storing information about a single Lego component.
...
Attributes
----------
uuid : UUID
A randomly generated unique identifier for the component.
parent : None | LegoAssembly
The parent of the component. Can be either None or a LegoAssembly object.
layer : AggregationLayer
An enumeration indicating the hierarchy level. For compoennts, this is
COMPONENT by default.
properties : dict
Dictionary that holds all properties of the component that can be saved
in a dictionary format.
Methods
-------
clone(new_label=None)
Returns a new instance of LegoComponent identical to the current instance.
get_root_assembly()
Returns the top-level assembly in which the component belongs.
to_dict()
Returns the current instance represented as a dictionary.
"""
def __init__(
self,
label: Optional[str] = None,
datasheet: Optional[dict] = None,
*more_properties: dict,
**kwargs,
) -> None:
"""
Constructs all the necessary attributes for the LegoComponent object.
Parameters
----------
label : str, optional
The name of the component to add.
datasheet : dict, optional
Metadata describing the component, read from datasheet.
more_properties : tuple of dict, dict
Additional dictionaries representing custom properties.
kwargs
Arbitrary keyword arguments representing custom properties.
Raises
------
ValueError
If the type of more_properties argument is not a dictionary.
"""
self.uuid: uuid.UUID = uuid.uuid4()
self.parent: None | LegoAssembly = None
self.layer: AggregationLayer = AggregationLayer.COMPONENT
self.properties: dict = {}
if label is not None:
self.properties["label"] = label
if datasheet is not None:
self.properties.update(datasheet)
for prop in more_properties:
if isinstance(prop, dict):
self.properties.update(prop)
else:
raise ValueError(f"Unexpected argument type: {type(more_properties)}")
for key, value in kwargs.items():
self.properties[key] = value
def clone(self, new_label: Optional[str] = None) -> LegoComponent:
"""
Returns a new instance of LegoComponent identical to the current instance.
This method creates a new instance of LegoComponent that is identical to the
current instance with an optional different label.
The assigned uuid changes and elements are copied.
Parameters
----------
new_label : str, optional
A new label string to use. Defaults to None.
Returns
-------
LegoComponent
A new instance of LegoComponent that is identical to the current instance.
"""
if new_label is None:
new_label = self.properties["label"]
clone = LegoComponent(None, None, self.properties)
clone.properties["label"] = new_label
return clone
def get_root_assembly(self):
"""
Returns the top-level assembly in which the component belongs.
This method traverses the parent hierarchy of a LegoComponent object until it
finds the root-level LegoAssembly, returning it to the caller. A parent is
assigned when a LegoComponent or LegoItem is added to a LegoAssembly object.
Returns
-------
None | LegoAssembly
The root-level LegoAssembly or None if the component has no parent.
"""
if self.parent is None:
return None
current_assembly = self.parent
while current_assembly.parent is not None:
current_assembly = current_assembly.parent
return current_assembly
def to_dict(self) -> Dict:
"""
Returns the current instance represented as a dictionary.
This method returns a dictionary representation of the LegoComponent object
suitable for serialization as JSON.
Returns
-------
dict
A dictionary representation of the object.
"""
dict_ = {
"uuid": self.uuid,
"properties": self.properties,
"layer": self.layer,
}
return {"component": dict_}
def __str__(self):
"""
Simple string representation of the object.
"""
return self.__repr__()
def __repr__(self):
"""
String representation of the object including the component label and UUID.
"""
return f"LegoComponent {self.properties['label']} [{self.uuid}]"
class LegoAssembly:
"""
Represents a Lego assembly that can contain Lego Components and subassemblies.
Attributes:
uuid (uuid.UUID): The unique ID of the assembly.
parent (LegoAssembly or None): The parent assembly containing this one, if any.
properties (dict): Optional properties for the assembly, such as a label.
layer (AggregationLayer): The aggregation layer of the assembly.
components (List[LegoComponent]): The list of contained components.
assemblies (List[LegoAssembly]): The list of contained subassemblies.
"""
def __init__(
self,
layer: AggregationLayer,
label: Optional[str] = None,
*properties: dict,
**kwargs,
) -> None:
"""
Initializes a new LegoAssembly instance.
Args:
layer (AggregationLayer): The aggregation layer of the assembly.
label (Optional[str], optional): Optional label for the assembly.
Defaults to None.
properties (dict): Optional properties for the assembly, such as a label.
**kwargs: Optional keyword arguments for additional properties.
"""
self.uuid: uuid.UUID = uuid.uuid4()
self.parent: None | LegoAssembly = None
self.properties: dict = {}
self.layer: AggregationLayer = layer
if label is not None:
self.properties["label"] = label
for prop in properties:
if isinstance(prop, dict):
self.properties.update(prop)
else:
raise ValueError(f"Unexpected argument type: {type(properties)}")
for key, value in kwargs.items():
self.properties[key] = value
self.components: List[LegoComponent] = []
self.assemblies: List[LegoAssembly] = []
def add_component(self, component: LegoComponent | List[LegoComponent]) -> None:
"""
Adds a Lego component to the current assembly.
Use add() to handle both components and assemblies.
Args:
component (LegoComponent or List[LegoComponent]):
The component or list of components to add.
Raises:
TypeError: If the argument is not a LegoComponent instance
or a list of LegoComponent instances.
AssertionError: If the component or a component with the same UUID
is already contained in the assembly or any of its child assemblies.
"""
if isinstance(component, list):
for c in component:
self.add_component(c)
return
if not isinstance(component, LegoComponent):
raise TypeError(
f"Argument should be of type {LegoComponent.__name__}, "
f"got {type(component).__name__} instead."
)
if self.get_root_assembly().contains_uuid(component.uuid):
raise AssertionError(
f"This assembly or a subassembly already contains "
f"the component with ID "
f"{component.uuid}."
)
component.parent = self
self.components.append(component)
def add_assembly(self, assembly: LegoAssembly | List[LegoAssembly]) -> None:
"""
Adds a subassembly to the current assembly.
Use add() to handle both components and assemblies.
Args:
assembly (LegoAssembly or List[LegoAssembly]):
The subassembly or list of subassemblies to add.
Raises:
TypeError: If the argument is not a LegoAssembly instance
or a list of LegoAssembly instances.
AssertionError: If the subassembly or a subassembly with the same UUID
is already contained in the assembly or any of its child assemblies.
"""
if isinstance(assembly, list):
for a in assembly:
self.add_assembly(a)
return
if not isinstance(assembly, LegoAssembly):
raise TypeError(
f"Argument should be of type {LegoAssembly.__name__}, "
f"got {type(assembly).__name__} instead."
)
if self.get_root_assembly().contains_uuid(assembly.uuid):
raise AssertionError(
f"This assembly or a subassembly already contains "
f"the assembly with ID {assembly.uuid}."
)
assembly.parent = self
self.assemblies.append(assembly)
def add(
self, part: LegoAssembly | LegoComponent | List[LegoAssembly | LegoComponent]
) -> None:
"""
Adds either a Lego component, a subassembly or a (mixed) list of them to the
current assembly. Uses internal functions add_component() and add_assembly().
Args:
part (LegoAssembly or LegoComponent or List[LegoAssembly or LegoComponent]):
The part or parts to add.
Raises:
TypeError: If the argument is not a LegoAssembly instance or a LegoComponent
instance, or a (mixed) list of them.
"""
if isinstance(part, LegoComponent):
self.add_component(part)
elif isinstance(part, LegoAssembly):
self.add_assembly(part)
elif isinstance(part, list):
for p in part:
self.add(p)
else:
raise TypeError(
f"Argument should be of types {LegoAssembly.__name__}, "
f"{LegoComponent.__name__} or a list of them. "
f"Got {type(part).__name__} instead."
)
def children(self) -> Dict[str, List[LegoComponent] | List[LegoAssembly]]:
"""
Returns a dictionary of the assembly's children (components and
sub-assemblies), sorted by category.
Returns:
A dictionary with two keys - "components" and "assemblies" - and
corresponding lists of LegoComponents and LegoAssemblies as values.
"""
return {"components": self.components, "assemblies": self.assemblies}
def get_component_list(self, max_depth: int = -1) -> List[LegoComponent]:
"""
Gets a full list of all components contained in the assembly or any of
its sub-assemblies.
Args:
max_depth: An integer indicating the maximum depth of recursion.
-1 means unlimited depth. 0 returns only direc children.
Returns:
A list of all LegoComponents contained in the current assembly or
its sub-assemblies.
"""
component_list = []
component_list.extend(self.components)
if max_depth > 0 or max_depth < 0:
for assembly in self.assemblies:
component_list.extend(assembly.get_component_list(max_depth - 1))
return component_list
def get_root_assembly(self) -> LegoAssembly:
"""
Returns the root LegoAssembly of the current assembly by recursively
searching up the tree until the top-most parent (the root) is found.
Returns:
The root LegoAssembly of the current LegoAssembly instance.
"""
current_assembly = self
while current_assembly.parent is not None:
current_assembly = current_assembly.parent
return current_assembly
def contains_uuid(self, uuid_: uuid.UUID):
"""
Recursively searches through the assembly and component
tree to determine whether the specified UUID exists anywhere
within it.
Args:
uuid_: A UUID object representing the ID to search for.
Returns:
True if the UUID exists in the assembly or any of its sub-assemblies,
False otherwise.
"""
component_ids = list(map(lambda c: c.uuid, self.components))
if uuid_ in component_ids:
return True
assembly_ids = list(map(lambda a: a.uuid, self.assemblies))
if uuid_ in assembly_ids:
return True
for assembly in self.assemblies:
if assembly.contains_uuid(uuid_):
return True
return False
def to_dict(self) -> Dict:
"""
Serializes the current LegoAssembly instance and its descendants into a dict.
Returns:
A dictionary representation of the current assembly, including all
of its component and sub-assembly children.
"""
dict_ = {
"uuid": self.uuid,
"properties": self.properties,
"layer": self.layer,
}
dict_["components"] = [component.to_dict() for component in self.components]
dict_["assemblies"] = [assembly.to_dict() for assembly in self.assemblies]
return {"assembly": dict_}
def __repr__(self):
"""
String representation of the object including the component label and UUID.
"""
return f"LegoAssembly {self.properties['label']} [{self.uuid}]"
def clone(self, label: Optional[str] = None) -> LegoAssembly:
"""
Creates a deep clone of the current LegoAssembly instance, including
all of its component and sub-assembly children. The assigned uuid changes.
Optionally a new label can be passed.
Args:
label: The label (name) for the cloned assembly. If none is passed,
uses the same label as the original assembly.
Returns:
The cloned LegoAssembly instance.
"""
if label is None:
label = self.properties["label"]
clone = LegoAssembly(self.layer, None, self.properties)
clone.properties["label"] = label
for component in self.components:
clone.add_component(component.clone())
for assembly in self.assemblies:
clone.add_assembly(assembly.clone())
return clone
def print_assembly_tree(root, level=0, is_last=False):
"""
Prints the assembly tree starting from root with a visualization
implemented with text characters.
Args:
root (LegoAssembly): The root of the assembly tree to print.
level (int): The indentation level. Defaults to 0.
is_last (bool): Determines whether the current node is the last in level.
Defaults to False.
"""
if not isinstance(root, LegoAssembly):
raise TypeError(
f"Argument should be of type {LegoAssembly.__name__}, "
f"got {type(root).__name__} instead."
)
""" Print the items. """
assembly_padding = ""
if level > 0:
assembly_padding += "" * (level - 1)
if is_last:
assembly_padding += "└── "
else:
assembly_padding += "├── "
print(f"{assembly_padding}{root}")
""" Recursively print child components. """
for i, assembly in enumerate(root.assemblies):
is_last_ = i == len(root.assemblies) - 1 and len(root.components) == 0
print_assembly_tree(assembly, level + 1, is_last_)
""" Print the components. """
for i, item in enumerate(root.components):
component_padding = "" * level if not is_last else " "
component_padding += "├── " if i < len(root.components) - 1 else "└── "
print(f"{component_padding}{item}")
def correct_aggregation_hierarchy(root: LegoAssembly, strict: bool = False):
"""
Recursively checks whether the aggregation hierarchy from `root` is correct.
Args:
root (LegoAssembly): The root of the assembly tree.
strict (bool): If True, the function will return False if any assembly
or component with a layer level equal to root's is found.
Defaults to False.
Returns:
True if the aggregation hierarchy is correct. False otherwise.
"""
if not isinstance(root, LegoAssembly):
raise TypeError(
f"Argument should be of type {LegoAssembly.__name__}, "
f"got {type(root).__name__} instead."
)
higher_level = operator.le
if strict:
higher_level = operator.lt
for component in root.components:
if not higher_level(root.layer.value, component.layer.value):
return False
for assembly in root.assemblies:
if not higher_level(root.layer.value, assembly.layer.value):
return False
if not correct_aggregation_hierarchy(assembly, strict):
return False
return True
class KPIEncoder(json.JSONEncoder):
"""
JSON encoder that handles special class types for KPI serialization.
"""
def default(self, o):
"""
Overrides default method to handle special conversion cases.
Args:
o : Object to be converted.
Returns:
Converted object or super method if no applicable case is found.
"""
if isinstance(o, uuid.UUID):
return "kpi-" + str(o)
if isinstance(o, (AggregationLayer)):
return "kpi-" + o.name
return super().default(o)
'''
FAIR Quality KPIs
'''
def main ():
print("Hello ;-)")
pass
if __name__ == "__main__":
main()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment