From 619608936ea8356656fceeda944634212734fa1a Mon Sep 17 00:00:00 2001 From: huynv44 Date: Thu, 12 Dec 2024 01:09:54 +0700 Subject: [PATCH] Fixed: can perform ShiftRange.get() --- pyproject.toml | 2 +- pyshiftsla/shift.py | 12 ++++++------ pyshiftsla/shiftrange.py | 9 ++++++++- tests/test_readme_showcases.py | 1 + 4 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 425c254..90c6422 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pyshiftsla" -version = "0.3.0" +version = "0.4.0" description = "Highly customizable SLA and work-shifts Generator, for any time-keeping and days-off policies" authors = ["Huy Vu Nguyen "] readme = "README.md" diff --git a/pyshiftsla/shift.py b/pyshiftsla/shift.py index ac6d628..e9ef387 100644 --- a/pyshiftsla/shift.py +++ b/pyshiftsla/shift.py @@ -185,15 +185,15 @@ def get_overlap(self, other: "Shift") -> RESOLVED_OVERLAPPED_SHIFT: def get_outer( self, other: "Shift", - resovled_overlap: RESOLVED_OVERLAPPED_SHIFT | None = None, + resolved_overlap: RESOLVED_OVERLAPPED_SHIFT | None = None, ) -> RESOLVED_OUTER_SHIFTS: """Get the outer `Shift`s of 2 overlapped `Shift`s, return both of them, if not overlapped """ - if resovled_overlap is None: - resovled_overlap = self.get_overlap(other) + if resolved_overlap is None: + resolved_overlap = self.get_overlap(other) outer_shifts = [self] - match resovled_overlap["compare_result"]: + match resolved_overlap["compare_result"]: case "smaller" | "greater": outer_shifts.append(other) case "end-connects-start" | "start-connects-end": @@ -209,10 +209,10 @@ def get_outer( outer_shifts = [ Shift( start=min([self.start, other.start]), - end=resovled_overlap["overlapped"].start, + end=resolved_overlap["overlapped"].start, ), Shift( - start=resovled_overlap["overlapped"].end, + start=resolved_overlap["overlapped"].end, end=max([self.end, other.end]), ), ] diff --git a/pyshiftsla/shiftrange.py b/pyshiftsla/shiftrange.py index 17513d0..f929076 100644 --- a/pyshiftsla/shiftrange.py +++ b/pyshiftsla/shiftrange.py @@ -9,13 +9,20 @@ class ShiftRange(RootModel): root: Dict[date, DailyShift] - def __getitem__(self, key: date) -> DailyShift | None: + def __getitem__(self, key: date) -> DailyShift: if not isinstance(key, date): raise NotImplementedError( f"Only accept key as `date` not {type(key)}" ) return self.root[key] + def get(self, key: date) -> DailyShift | None: + if not isinstance(key, date): + raise NotImplementedError( + f"Only accept key as `date` not {type(key)}" + ) + return self.root.get(key) + @property def _start_date(self) -> date: return max(self.root) diff --git a/tests/test_readme_showcases.py b/tests/test_readme_showcases.py index 36a5958..eca574d 100644 --- a/tests/test_readme_showcases.py +++ b/tests/test_readme_showcases.py @@ -78,6 +78,7 @@ def test_readme_first_showcase_us_woman(): with pytest.raises(KeyError): # KeyError: datetime.date(2024, 7, 4) generated_shiftrange[date(2024, 7, 4)] + assert generated_shiftrange.get(date(2024, 7, 4)) is None def test_readme_second_showcase_us_woman_sla():