Repository: dimitarOnGithub/temporals

Issue:
TimePeriod implementation

Discussion:

Patch:
diff --git a/temporals/periods.py b/temporals/periods.py
index e5704ad..64b0248 100644
--- a/temporals/periods.py
+++ b/temporals/periods.py
@@ -1,3 +1,4 @@
+from abc import abstractmethod
 from datetime import time, date, datetime, timedelta
 from typing import Union
 from .exceptions import TimeAmbiguityError
@@ -21,18 +22,20 @@ class Period:
     def __contains__(self, item):
         raise NotImplementedError('Period class does not contain __eq__ method, inheriting classes must override it')
 
-    def __lt__(self, other):
-        raise NotImplementedError('Period class does not contain __lt__ method, inheriting classes must override it')
-
-    def __gt__(self, other):
-        raise NotImplementedError('Period class does not contain __lt__ method, inheriting classes must override it')
-
     def __repr__(self):
         return f"{self.__class__.__name__}(start={self.start.__repr__()}, end={self.end.__repr__()})"
 
     def __str__(self):
         return f'{self.start.isoformat()}/{self.end.isoformat()}'
 
+    @abstractmethod
+    def is_before(self, other):
+        raise NotImplementedError("Parent class 'Period' does not implement is_before method")
+
+    @abstractmethod
+    def is_after(self, other):
+        raise NotImplementedError("Parent class 'Period' does not implement is_after method")
+
 
 class TimePeriod(Period):
     """ The TimePeriod class is responsible for time periods within a 24-hour day. Instances of this class offer the
@@ -113,13 +116,50 @@ class TimePeriod(Period):
             return self.start <= item <= self.end
         return False
 
-    def __lt__(self, other):
-        # TODO: think about if < makes sense
-        pass
+    def is_before(self, other: Union['TimePeriod', time]) -> bool:
+        """ Test if this period ends before the provided `other` value. This check will evaluate as True in the
+        following scenario:
+
+        time object:
+        1000 This period:   1400
+        |====================|
+                                |
+                              1500
+
+        TimePeriod object:
+        1000 This period:   1400
+        |====================|
+                                |====|
+                              1500  1700
+        """
+        if isinstance(other, time):
+            return self.end <= other
+        elif isinstance(other, TimePeriod):
+            return self.end <= other.start
+        return False
 
-    def __gt__(self, other):
-        # TODO: think about if > makes sense
-        pass
+    def is_after(self, other: Union['TimePeriod', time]) -> bool:
+        """ Test if this period starts after the provided `other` value. This check will evaluate as True in the
+        following scenario:
+
+        time object:
+                        1000       This period:          1700
+                        |==================================|
+                    |
+                  0900
+
+        TimePeriod object:
+                        1000       This period:          1700
+                        |==================================|
+            |=======|
+          0700    0900
+
+        """
+        if isinstance(other, time):
+            return other <= self.start
+        elif isinstance(other, TimePeriod):
+            return other.end <= self.start
+        return False
 
     def overlaps_with(self,
                       other: Union['TimePeriod', 'DatetimePeriod']


Test patch:
diff --git a/tests/test_timeperiod.py b/tests/test_timeperiod.py
index a7ca693..b535b9c 100644
--- a/tests/test_timeperiod.py
+++ b/tests/test_timeperiod.py
@@ -77,6 +77,42 @@ class TestTimePeriod:
         self.period_dt = DatetimePeriod(start=self.start_dt, end=self.end_dt)
         assert self.period_dt in self.period
 
+    def test_is_before(self):
+        self.start = time(10, 0, 0)
+        self.end = time(12, 0, 0)
+        self.period = TimePeriod(start=self.start, end=self.end)
+
+        assert self.period.is_before(time(13, 0)) is True
+        assert self.period.is_before(time(10, 0)) is False
+
+        self.other_start = time(12, 0, 0)
+        self.other_end = time(13, 0, 0)
+        self.other_period = TimePeriod(start=self.other_start, end=self.other_end)
+        assert self.period.is_before(self.other_period) is True
+
+        self.other_start = time(8, 0, 0)
+        self.other_end = time(10, 0, 0)
+        self.other_period = TimePeriod(start=self.other_start, end=self.other_end)
+        assert self.period.is_before(self.other_period) is False
+
+    def test_is_after(self):
+        self.start = time(10, 0, 0)
+        self.end = time(12, 0, 0)
+        self.period = TimePeriod(start=self.start, end=self.end)
+
+        assert self.period.is_after(time(13, 0)) is False
+        assert self.period.is_after(time(10, 0)) is True
+
+        self.other_start = time(12, 0, 0)
+        self.other_end = time(13, 0, 0)
+        self.other_period = TimePeriod(start=self.other_start, end=self.other_end)
+        assert self.period.is_after(self.other_period) is False
+
+        self.other_start = time(8, 0, 0)
+        self.other_end = time(10, 0, 0)
+        self.other_period = TimePeriod(start=self.other_start, end=self.other_end)
+        assert self.period.is_after(self.other_period) is True
+
     def test_overlaps(self):
         """
            0800     Period 1    1000