import datetime class Term: """A representation of a term in the CSC LDAP, e.g. 's2021'.""" seasons = ['w', 's', 'f'] def __init__(self, s_term: str): assert len(s_term) == 5 and s_term[0] in self.seasons and \ s_term[1:].isdigit() self.s_term = s_term def __repr__(self): return self.s_term @staticmethod def current(): """Get a Term object for the current date.""" dt = datetime.datetime.now() c = 'w' if 5 <= dt.month <= 8: c = 's' elif 9 <= dt.month: c = 'f' s_term = c + str(dt.year) return Term(s_term) def __add__(self, other): assert type(other) is int and other >= 0 c = self.s_term[0] season_idx = self.seasons.index(c) year = int(self.s_term[1:]) year += other // 3 season_idx += other % 3 if season_idx >= 3: year += 1 season_idx -= 3 s_term = self.seasons[season_idx] + str(year) return Term(s_term) def __eq__(self, other): return isinstance(other, Term) and self.s_term == other.s_term def __lt__(self, other): if not isinstance(other, Term): return NotImplemented c1, c2 = self.s_term[0], other.s_term[0] year1, year2 = int(self.s_term[1:]), int(other.s_term[1:]) return year1 < year2 or ( year1 == year2 and self.seasons.index(c1) < self.seasons.index(c2) ) def __gt__(self, other): if not isinstance(other, Term): return NotImplemented c1, c2 = self.s_term[0], other.s_term[0] year1, year2 = int(self.s_term[1:]), int(other.s_term[1:]) return year1 > year2 or ( year1 == year2 and self.seasons.index(c1) > self.seasons.index(c2) ) def __ge__(self, other): return self > other or self == other def __le__(self, other): return self < other or self == other def to_datetime(self) -> datetime.datetime: c = self.s_term[0] year = int(self.s_term[1:]) month = self.seasons.index(c) * 4 + 1 day = 1 return datetime.datetime(year, month, day)