From 0bc83fedae2c23815a08cce6575968c469cd2a9d Mon Sep 17 00:00:00 2001 From: Dion Moult Date: Sun, 31 Mar 2024 22:11:06 +1100 Subject: [PATCH] Use indices instead of WorkTimeDatesInterface Checking start/end dates are done very, very often in date calculations (often looping through every day in a date range). So I'd prefer more opaque code but less overhead. --- .../api/sequence/edit_work_time.py | 9 ++--- .../ifcopenshell/util/data.py | 34 ------------------- .../ifcopenshell/util/sequence.py | 15 ++++---- .../test/api/sequence/test_edit_work_time.py | 6 ++-- 4 files changed, 13 insertions(+), 51 deletions(-) diff --git a/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_work_time.py b/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_work_time.py index c9c76dfee2..76a0521478 100644 --- a/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_work_time.py +++ b/src/ifcopenshell-python/ifcopenshell/api/sequence/edit_work_time.py @@ -17,7 +17,6 @@ # along with IfcOpenShell. If not, see . import ifcopenshell.util.date -from ifcopenshell.util.data import WorkTimeDatesInterface class Usecase: @@ -55,10 +54,12 @@ class Usecase: self.settings = {"work_time": work_time, "attributes": attributes or {}} def execute(self): - work_time_dates = WorkTimeDatesInterface(self.settings["work_time"]) for name, value in self.settings["attributes"].items(): - if name in ("Start", "Finish", "StartDate", "FinishDate"): + if name in ("Start", "StartDate"): value = ifcopenshell.util.date.datetime2ifc(value, "IfcDate") - setattr(work_time_dates, name, value) + self.settings["work_time"][4] = value + elif name in ("Finish", "FinishDate"): + value = ifcopenshell.util.date.datetime2ifc(value, "IfcDate") + self.settings["work_time"][5] = value else: setattr(self.settings["work_time"], name, value) diff --git a/src/ifcopenshell-python/ifcopenshell/util/data.py b/src/ifcopenshell-python/ifcopenshell/util/data.py index e6761edd5d..a66ea80252 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/data.py +++ b/src/ifcopenshell-python/ifcopenshell/util/data.py @@ -93,37 +93,3 @@ class Clipping: second_operand = ifc_file.createIfcHalfSpaceSolid(plane, False) return ifc_file.createIfcBooleanClippingResult("DIFFERENCE", first_operand, second_operand) - - -class WorkTimeDatesInterface: - def __init__(self, work_time: ifcopenshell.entity_instance): - """Utility class for IfcWorkTime dates allowing refer - to it's dates as `.Start`/`.Finish` or `.StartDate`/`.FinishDate` - regardless of the schema version - """ - if hasattr(work_time, "StartDate"): # since IFC4X3 - self.start_attr, self.finish_attr = "StartDate", "FinishDate" - else: - self.start_attr, self.finish_attr = "Start", "Finish" - - self.work_time = work_time - - @property - def Start(self): - return getattr(self.work_time, self.start_attr) - - @Start.setter - def Start(self, value): - return setattr(self.work_time, self.start_attr, value) - - StartDate = Start - - @property - def Finish(self): - return getattr(self.work_time, self.finish_attr) - - @Finish.setter - def Finish(self, value): - return setattr(self.work_time, self.finish_attr, value) - - FinishDate = Finish diff --git a/src/ifcopenshell-python/ifcopenshell/util/sequence.py b/src/ifcopenshell-python/ifcopenshell/util/sequence.py index f450dbf5ed..720b81c151 100644 --- a/src/ifcopenshell-python/ifcopenshell/util/sequence.py +++ b/src/ifcopenshell-python/ifcopenshell/util/sequence.py @@ -18,7 +18,6 @@ import datetime import ifcopenshell.util.date -from ifcopenshell.util.data import WorkTimeDatesInterface from math import floor from functools import lru_cache from collections import namedtuple @@ -182,15 +181,14 @@ def is_day_in_work_time(day, work_time): is_day_in_work_time = True if isinstance(day, datetime.datetime): day = datetime.date(day.year, day.month, day.day) - work_time_dates = WorkTimeDatesInterface(work_time) - if work_time_dates.Start: - start = ifcopenshell.util.date.ifc2datetime(work_time_dates.Start) + if work_time[4]: + start = ifcopenshell.util.date.ifc2datetime(work_time[4]) if day > start: is_day_in_work_time = True else: is_day_in_work_time = False - if work_time_dates.Finish: - finish = ifcopenshell.util.date.ifc2datetime(work_time_dates.Finish) + if work_time[5]: + finish = ifcopenshell.util.date.ifc2datetime(work_time[5]) if day < finish: is_day_in_work_time = True else: @@ -207,17 +205,16 @@ def is_work_time_applicable_to_day(work_time, day): if isinstance(day, datetime.datetime): day = datetime.date(day.year, day.month, day.day) recurrence = work_time.RecurrencePattern - work_time_dates = WorkTimeDatesInterface(work_time) if recurrence.RecurrenceType == "DAILY": if not recurrence.Interval and not recurrence.Occurrences: return True - if not work_time_dates.Start: + if not work_time[4]: return False return False # TODO elif recurrence.RecurrenceType == "WEEKLY": if not recurrence.Interval and not recurrence.Occurrences: return (day.weekday() + 1) in recurrence.WeekdayComponent - if not work_time_dates.Start: + if not work_time[4]: return False return False # TODO elif recurrence.RecurrenceType == "MONTHLY_BY_DAY_OF_MONTH": diff --git a/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py b/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py index d91abe2a31..87b484cef8 100644 --- a/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py +++ b/src/ifcopenshell-python/test/api/sequence/test_edit_work_time.py @@ -19,7 +19,6 @@ import datetime import test.bootstrap import ifcopenshell.api -from ifcopenshell.util.data import WorkTimeDatesInterface class TestEditWorkTime(test.bootstrap.IFC4): @@ -35,13 +34,12 @@ class TestEditWorkTime(test.bootstrap.IFC4): "Finish": datetime.datetime(2020, 2, 1), } ifcopenshell.api.run("sequence.edit_work_time", self.file, work_time=work_time, attributes=attributes) - worktime_dates = WorkTimeDatesInterface(work_time) assert work_time.Name == attributes["Name"] assert work_time.DataOrigin == attributes["DataOrigin"] assert work_time.UserDefinedDataOrigin == attributes["UserDefinedDataOrigin"] assert work_time.RecurrencePattern == attributes["RecurrencePattern"] - assert worktime_dates.Start == "2020-01-01" - assert worktime_dates.Finish == "2020-02-01" + assert work_time.Start == "2020-01-01" + assert work_time.Finish == "2020-02-01" class TestEditWorkTimeIFC4X3(test.bootstrap.IFC4X3):