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.
This commit is contained in:
Dion Moult
2024-03-31 22:11:06 +11:00
parent c16d49d683
commit 33f5db2e26
4 changed files with 13 additions and 51 deletions
@@ -17,7 +17,6 @@
# along with IfcOpenShell. If not, see <http://www.gnu.org/licenses/>.
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)
@@ -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
@@ -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":
@@ -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):