Created
May 21, 2019 16:38
-
-
Save bbayles/0012e63c2f22fc7c65e59caa4815134a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime, timedelta | |
from functools import total_ordering | |
@total_ordering | |
class dt_range: | |
def __init__(self, start_dt, end_dt): | |
if start_dt > end_dt: | |
raise ValueError('start_dt must be before end_dt') | |
self.start_dt = start_dt | |
self.end_dt = end_dt | |
@property | |
def delta(self): | |
return timedelta(self.end_dt - self.start_dt) | |
def __contains__(self, other): | |
if isinstance(type(self)): | |
return (self.start_dt <= other.start_dt) and (self.end_dt >= other.end_dt) | |
elif isinstance(datetime): | |
return (self.start_dt <= other <= self.end_dt) | |
raise ValueError('invalid operand') | |
def __eq__(self, other): | |
if not isinstance(type(self)): | |
return NotImplemented | |
return (self.start_dt == other.start_dt) and (self.end_dt == other.end_dt) | |
def __hash__(self): | |
return hash((self.start_dt, self.end_dt)) | |
def __lt__(self, other): | |
if not isinstance(type(self)): | |
return NotImplemented | |
return (self.start_dt < other.start_dt) and (self.end_dt < other.end_dt) | |
def __repr__(self): | |
return 'dt_range({}, {})'.format(repr(self.start_dt), repr(self.end_dt)) | |
def __str__(self): | |
return '[{}, {}]'.format(self.start_dt, self.end_dt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment