From afa803ea62f7f000399743f4babef7931928867a Mon Sep 17 00:00:00 2001 From: Totally a booplicate <53382877+Booplicate@users.noreply.github.com> Date: Sun, 4 Jun 2023 14:36:46 +0300 Subject: [PATCH] implement _try_purge per review comment about repeating code --- .../ifcopenshell/api/pset/edit_pset.py | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/pset/edit_pset.py b/src/ifcopenshell-python/ifcopenshell/api/pset/edit_pset.py index b463661272..a55d04bc0e 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/pset/edit_pset.py +++ b/src/ifcopenshell-python/ifcopenshell/api/pset/edit_pset.py @@ -180,6 +180,19 @@ class Usecase: """ return prop.Name in self.settings["properties"] + def _try_purge(self, prop) -> bool: + """ + Tried to remove the property + if successful, returns True, otherwise False + NOTE: Assumes the prop exists + """ + if not self.settings["should_purge"]: + return False + + del self.settings["properties"][prop.Name] + self.file.remove(prop) + return True + # TODO - Add support for changing property types? # For example - IfcPropertyEnumeratedValue to # IfcPropertySingleValue. Or maybe the user should @@ -197,7 +210,7 @@ class Usecase: def update_existing_enum(self, prop): """ - NOTE: assumes the prop exists + NOTE: Assumes the prop exists """ value = self.settings["properties"][prop.Name] unit, value = self.unpack_unit_value(value) @@ -217,9 +230,7 @@ class Usecase: and value.is_a("IfcPropertyEnumeratedValue") ): if not value.EnumerationReference.EnumerationValues: - if self.settings["should_purge"]: - del self.settings["properties"][prop.Name] - self.file.remove(prop) + if self._try_purge(prop): return prop.EnumerationReference.EnumerationValues = None prop.EnumerationValues = None @@ -234,14 +245,12 @@ class Usecase: def update_existing_property(self, prop): """ - NOTE: assumes the prop exists + NOTE: Assumes the prop exists """ value = self.settings["properties"][prop.Name] unit, value = self.unpack_unit_value(value) if value is None: - if self.settings["should_purge"]: - del self.settings["properties"][prop.Name] - self.file.remove(prop) + if self._try_purge(prop): return prop.NominalValue = None elif isinstance(value, ifcopenshell.entity_instance):