Skip to content
Snippets Groups Projects
Commit 8f295e11 authored by Lamm, Sascha's avatar Lamm, Sascha :cow2:
Browse files

Added comparison methods to LegoComponent and LegoAssembly. Small updates to...

Added comparison methods to LegoComponent and LegoAssembly. Small updates to docstrings in modul level functions
parent c5150a6d
No related branches found
No related tags found
No related merge requests found
__pycache__/
\ No newline at end of file
...@@ -137,15 +137,27 @@ class LegoComponent: ...@@ -137,15 +137,27 @@ class LegoComponent:
} }
return {"component": dict_} return {"component": dict_}
def __str__(self): def __eq__(self, obj: object):
"""Handle the conversion of LegoComponent objects to str objects. """Check if provided object is equal to this component.
Args:
obj (object): Object to compare to.
Returns: Returns:
str: A string converted from the LegoComponent instance. bool: True if UUID, properties, and layer match. False otherwise.
""" """
if self.properties.get("label") is None: # in case of mismatching class
return f"LegoComponent [{self.uuid}]" if not isinstance(obj, LegoComponent):
return f"LegoComponent {self.properties['label']} [{self.uuid}]" return False
if (
self.uuid == obj.uuid
and self.layer == obj.layer
and self.properties == obj.properties
):
return True
else:
return False
def __repr__(self): def __repr__(self):
"""Create a machine-readable representation of the instance. """Create a machine-readable representation of the instance.
...@@ -153,9 +165,19 @@ class LegoComponent: ...@@ -153,9 +165,19 @@ class LegoComponent:
Returns: Returns:
str: A string representing the LegoComponent instance. str: A string representing the LegoComponent instance.
""" """
return f"LegoComponent({self.properties if self.properties else ""})" return f"LegoComponent({self.properties if self.properties else ""})"
def __str__(self):
"""Handle the conversion of LegoComponent objects to str objects.
Returns:
str: A string converted from the LegoComponent instance.
"""
if self.properties.get("label") is None:
return f"LegoComponent [{self.uuid}]"
return f"LegoComponent {self.properties['label']} [{self.uuid}]"
class LegoAssembly: class LegoAssembly:
""" """
...@@ -386,12 +408,6 @@ class LegoAssembly: ...@@ -386,12 +408,6 @@ class LegoAssembly:
dict_["assemblies"] = [assembly.to_dict() for assembly in self.assemblies] dict_["assemblies"] = [assembly.to_dict() for assembly in self.assemblies]
return {"assembly": dict_} 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: def clone(self, label: Optional[str] = None) -> LegoAssembly:
""" """
Creates a deep clone of the current LegoAssembly instance, including Creates a deep clone of the current LegoAssembly instance, including
...@@ -415,8 +431,39 @@ class LegoAssembly: ...@@ -415,8 +431,39 @@ class LegoAssembly:
clone.add_assembly(assembly.clone()) clone.add_assembly(assembly.clone())
return clone return clone
def __eq__(self, obj: object) -> bool:
"""Check if provided object is equal to this assembly.
Args:
obj (object): Object to compare to.
Returns:
bool: True if UUID, properties, layer, components and assemblies match.
False otherwise.
"""
# in case of mismatching class
if not isinstance(obj, LegoAssembly):
return False
if (
self.uuid == obj.uuid
and self.properties == obj.properties
and self.layer == obj.layer
and self.components == obj.components
and self.assemblies == obj.assemblies
):
return True
else:
return False
def __repr__(self):
"""
String representation of the object including the component label and UUID.
"""
return f"LegoAssembly {self.properties['label']} [{self.uuid}]"
def print_assembly_tree(root, levels=None): def print_assembly_tree(root: LegoAssembly, levels: list[bool] = None) -> None:
""" """
Prints the assembly tree starting from root with a visualization Prints the assembly tree starting from root with a visualization
implemented with text characters. implemented with text characters.
...@@ -424,7 +471,7 @@ def print_assembly_tree(root, levels=None): ...@@ -424,7 +471,7 @@ def print_assembly_tree(root, levels=None):
Args: Args:
root (LegoAssembly): The root of the assembly tree to print. root (LegoAssembly): The root of the assembly tree to print.
levels (list[bool]): Internally used by recursion to know where levels (list[bool]): Internally used by recursion to know where
to print vertical connection. Defaults to an empty list. to print vertical connection. Defaults to None.
""" """
if not isinstance(root, LegoAssembly): if not isinstance(root, LegoAssembly):
raise TypeError( raise TypeError(
...@@ -485,12 +532,12 @@ class KPIEncoder(json.JSONEncoder): ...@@ -485,12 +532,12 @@ class KPIEncoder(json.JSONEncoder):
JSON encoder that handles special class types for KPI serialization. JSON encoder that handles special class types for KPI serialization.
""" """
def default(self, o): def default(self, o: Any):
""" """
Overrides default method to handle special conversion cases. Overrides default method to handle special conversion cases.
Args: Args:
o : Object to be converted. o (Any): Object to be converted.
Returns: Returns:
Converted object or super method if no applicable case is found. Converted object or super method if no applicable case is found.
......
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