Module peprock.datetime.period
Datetime period model.
Examples
>>> period = Period(
... start=datetime.datetime(2022, 1, 1, 12, 0),
... end=datetime.datetime(2022, 1, 2, 12, 0),
... )
>>> period.duration
datetime.timedelta(days=1)
>>> period.midpoint
datetime.datetime(2022, 1, 2, 0, 0)
>>> datetime.datetime(2022, 1, 1) in period
False
>>> period.start in period
True
>>> period.midpoint in period
True
>>> period.end in period
True
>>> datetime.datetime(2022, 1, 3) in period
False
>>> period in period
True
>>> Period(
... start=datetime.datetime(2022, 1, 1),
... end=datetime.datetime(2022, 1, 3),
... ) in period
False
Classes
class Period (start: datetime.datetime, end: datetime.datetime)
-
Datetime period supporting arithmetic operations.
Expand source code
@dataclasses.dataclass(frozen=True) class Period( collections.abc.Container["Period | datetime.datetime"], ): """Datetime period supporting arithmetic operations.""" start: datetime.datetime end: datetime.datetime @functools.cached_property def duration(self: Self) -> datetime.timedelta: """Return duration of period.""" return self.end - self.start @functools.cached_property def midpoint(self: Self) -> datetime.datetime: """Return midpoint of period.""" return self.start + self.duration / 2 def __contains__(self: Self, item: object) -> bool: """Return True if item is in period.""" match item: case Period(): # noinspection PyUnresolvedReferences return self.start <= item.start and item.end <= self.end case datetime.datetime(): # noinspection PyTypeChecker return self.start <= item <= self.end msg: str = f"expected peprock.datetime.Period | datetime.datetime, got {item!r}" raise TypeError(msg)
Ancestors
- collections.abc.Container
Class variables
var end : datetime.datetime
var start : datetime.datetime
Instance variables
var duration
-
Return duration of period.
Expand source code
def __get__(self, instance, owner=None): if instance is None: return self if self.attrname is None: raise TypeError( "Cannot use cached_property instance without calling __set_name__ on it.") try: cache = instance.__dict__ except AttributeError: # not all objects have __dict__ (e.g. class defines slots) msg = ( f"No '__dict__' attribute on {type(instance).__name__!r} " f"instance to cache {self.attrname!r} property." ) raise TypeError(msg) from None val = cache.get(self.attrname, _NOT_FOUND) if val is _NOT_FOUND: val = self.func(instance) try: cache[self.attrname] = val except TypeError: msg = ( f"The '__dict__' attribute on {type(instance).__name__!r} instance " f"does not support item assignment for caching {self.attrname!r} property." ) raise TypeError(msg) from None return val
var midpoint
-
Return midpoint of period.
Expand source code
def __get__(self, instance, owner=None): if instance is None: return self if self.attrname is None: raise TypeError( "Cannot use cached_property instance without calling __set_name__ on it.") try: cache = instance.__dict__ except AttributeError: # not all objects have __dict__ (e.g. class defines slots) msg = ( f"No '__dict__' attribute on {type(instance).__name__!r} " f"instance to cache {self.attrname!r} property." ) raise TypeError(msg) from None val = cache.get(self.attrname, _NOT_FOUND) if val is _NOT_FOUND: val = self.func(instance) try: cache[self.attrname] = val except TypeError: msg = ( f"The '__dict__' attribute on {type(instance).__name__!r} instance " f"does not support item assignment for caching {self.attrname!r} property." ) raise TypeError(msg) from None return val