|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import enum |
| 4 | +from typing import Any, Tuple, TypeVar |
| 5 | + |
| 6 | +from openfisca_core.indexed_enums import Enum |
| 7 | + |
| 8 | +T = TypeVar("T", bound = "DateUnit") |
| 9 | + |
| 10 | + |
| 11 | +class DateUnitMeta(enum.EnumMeta): |
| 12 | + |
| 13 | + def __contains__(self, item: Any) -> bool: |
| 14 | + if isinstance(item, str): |
| 15 | + return super().__contains__(self[item.upper()]) |
| 16 | + |
| 17 | + return super().__contains__(item) |
| 18 | + |
| 19 | + def __getitem__(self, key: object) -> T: |
| 20 | + if not isinstance(key, (int, slice, str, DateUnit)): |
| 21 | + return NotImplemented |
| 22 | + |
| 23 | + if isinstance(key, (int, slice)): |
| 24 | + return self[self.__dict__["_member_names_"][key]] |
| 25 | + |
| 26 | + if isinstance(key, str): |
| 27 | + return super().__getitem__(key.upper()) |
| 28 | + |
| 29 | + return super().__getitem__(key.value.upper()) |
| 30 | + |
| 31 | + @property |
| 32 | + def ethereal(self) -> Tuple[DateUnit, ...]: |
| 33 | + """Creates a :obj:`tuple` of ``key`` with ethereal items. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + tuple(str): A :obj:`tuple` containing the ``keys``. |
| 37 | +
|
| 38 | + Examples: |
| 39 | + >>> DateUnit.ethereal |
| 40 | + (<DateUnit.DAY(day)>, <DateUnit.MONTH(month)>, <DateUnit.YEAR(year)>) |
| 41 | +
|
| 42 | + >>> DateUnit.DAY in DateUnit.ethereal |
| 43 | + True |
| 44 | +
|
| 45 | + >>> "DAY" in DateUnit.ethereal |
| 46 | + True |
| 47 | +
|
| 48 | + >>> "day" in DateUnit.ethereal |
| 49 | + True |
| 50 | +
|
| 51 | + >>> "eternity" in DateUnit.ethereal |
| 52 | + False |
| 53 | +
|
| 54 | + """ |
| 55 | + |
| 56 | + return DateUnit.DAY, DateUnit.MONTH, DateUnit.YEAR |
| 57 | + |
| 58 | + @property |
| 59 | + def eternal(self) -> Tuple[DateUnit, ...]: |
| 60 | + """Creates a :obj:`tuple` of ``key`` with eternal items. |
| 61 | +
|
| 62 | + Returns: |
| 63 | + tuple(str): A :obj:`tuple` containing the ``keys``. |
| 64 | +
|
| 65 | + Examples: |
| 66 | + >>> DateUnit.eternal |
| 67 | + (<DateUnit.ETERNITY(eternity)>,) |
| 68 | +
|
| 69 | + >>> DateUnit.ETERNITY in DateUnit.eternal |
| 70 | + True |
| 71 | +
|
| 72 | + >>> "ETERNITY" in DateUnit.eternal |
| 73 | + True |
| 74 | +
|
| 75 | + >>> "eternity" in DateUnit.eternal |
| 76 | + True |
| 77 | +
|
| 78 | + >>> "day" in DateUnit.eternal |
| 79 | + False |
| 80 | +
|
| 81 | + """ |
| 82 | + |
| 83 | + return (DateUnit.ETERNITY,) |
| 84 | + |
| 85 | + |
| 86 | +class DateUnit(Enum, metaclass = DateUnitMeta): |
| 87 | + """The date units of a rule system. |
| 88 | +
|
| 89 | + Attributes: |
| 90 | + index (:obj:`int`): The ``index`` of each item. |
| 91 | + name (:obj:`str`): The ``name`` of each item. |
| 92 | + value (tuple(str, int)): The ``value`` of each item. |
| 93 | +
|
| 94 | + Examples: |
| 95 | + >>> repr(DateUnit) |
| 96 | + "<enum 'DateUnit'>" |
| 97 | +
|
| 98 | + >>> repr(DateUnit.DAY) |
| 99 | + '<DateUnit.DAY(day)>' |
| 100 | +
|
| 101 | + >>> str(DateUnit.DAY) |
| 102 | + 'DateUnit.DAY' |
| 103 | +
|
| 104 | + >>> dict([(DateUnit.DAY, DateUnit.DAY.value)]) |
| 105 | + {<DateUnit.DAY(day)>: 'day'} |
| 106 | +
|
| 107 | + >>> tuple(DateUnit) |
| 108 | + (<DateUnit.WEEK_DAY(week_day)>, <DateUnit.WEEK(week)>, <DateUnit.DA...) |
| 109 | +
|
| 110 | + >>> len(DateUnit) |
| 111 | + 6 |
| 112 | +
|
| 113 | + >>> DateUnit["DAY"] |
| 114 | + <DateUnit.DAY(day)> |
| 115 | +
|
| 116 | + >>> DateUnit["day"] |
| 117 | + <DateUnit.DAY(day)> |
| 118 | +
|
| 119 | + >>> DateUnit[2] |
| 120 | + <DateUnit.DAY(day)> |
| 121 | +
|
| 122 | + >>> DateUnit[-4] |
| 123 | + <DateUnit.DAY(day)> |
| 124 | +
|
| 125 | + >>> DateUnit[DateUnit.DAY] |
| 126 | + <DateUnit.DAY(day)> |
| 127 | +
|
| 128 | + >>> DateUnit("day") |
| 129 | + <DateUnit.DAY(day)> |
| 130 | +
|
| 131 | + >>> DateUnit.DAY in DateUnit |
| 132 | + True |
| 133 | +
|
| 134 | + >>> "DAY" in DateUnit |
| 135 | + True |
| 136 | +
|
| 137 | + >>> "day" in DateUnit |
| 138 | + True |
| 139 | +
|
| 140 | + >>> DateUnit.DAY == DateUnit.DAY |
| 141 | + True |
| 142 | +
|
| 143 | + >>> "DAY" == DateUnit.DAY |
| 144 | + True |
| 145 | +
|
| 146 | + >>> "day" == DateUnit.DAY |
| 147 | + True |
| 148 | +
|
| 149 | + >>> DateUnit.DAY < DateUnit.DAY |
| 150 | + False |
| 151 | +
|
| 152 | + >>> DateUnit.DAY > DateUnit.DAY |
| 153 | + False |
| 154 | +
|
| 155 | + >>> DateUnit.DAY <= DateUnit.DAY |
| 156 | + True |
| 157 | +
|
| 158 | + >>> DateUnit.DAY >= DateUnit.DAY |
| 159 | + True |
| 160 | +
|
| 161 | + >>> "DAY" < DateUnit.DAY |
| 162 | + False |
| 163 | +
|
| 164 | + >>> "DAY" > DateUnit.DAY |
| 165 | + False |
| 166 | +
|
| 167 | + >>> "DAY" <= DateUnit.DAY |
| 168 | + True |
| 169 | +
|
| 170 | + >>> "DAY" >= DateUnit.DAY |
| 171 | + True |
| 172 | +
|
| 173 | + >>> "day" < DateUnit.DAY |
| 174 | + False |
| 175 | +
|
| 176 | + >>> "day" > DateUnit.DAY |
| 177 | + False |
| 178 | +
|
| 179 | + >>> "day" <= DateUnit.DAY |
| 180 | + True |
| 181 | +
|
| 182 | + >>> "day" >= DateUnit.DAY |
| 183 | + True |
| 184 | +
|
| 185 | + >>> DateUnit.DAY.index |
| 186 | + 2 |
| 187 | +
|
| 188 | + >>> DateUnit.DAY.name |
| 189 | + 'DAY' |
| 190 | +
|
| 191 | + >>> DateUnit.DAY.value |
| 192 | + 'day' |
| 193 | +
|
| 194 | + .. versionadded:: 35.9.0 |
| 195 | +
|
| 196 | + """ |
| 197 | + |
| 198 | + # Attributes |
| 199 | + |
| 200 | + index: int |
| 201 | + name: str |
| 202 | + value: str |
| 203 | + |
| 204 | + # Members |
| 205 | + |
| 206 | + WEEK_DAY = "week_day" |
| 207 | + WEEK = "week" |
| 208 | + DAY = "day" |
| 209 | + MONTH = "month" |
| 210 | + YEAR = "year" |
| 211 | + ETERNITY = "eternity" |
| 212 | + |
| 213 | + __hash__ = object.__hash__ |
| 214 | + |
| 215 | + def __eq__(self, other): |
| 216 | + if isinstance(other, str): |
| 217 | + return self.value == other.lower() |
| 218 | + |
| 219 | + return NotImplemented |
| 220 | + |
| 221 | + def __lt__(self, other: Any) -> bool: |
| 222 | + if isinstance(other, str): |
| 223 | + return self.index < DateUnit[other.upper()].index |
| 224 | + |
| 225 | + return self.index < other |
| 226 | + |
| 227 | + def __le__(self, other: Any) -> bool: |
| 228 | + if isinstance(other, str): |
| 229 | + return self.index <= DateUnit[other.upper()].index |
| 230 | + |
| 231 | + return self.index <= other |
| 232 | + |
| 233 | + def __gt__(self, other: Any) -> bool: |
| 234 | + if isinstance(other, str): |
| 235 | + return self.index > DateUnit[other.upper()].index |
| 236 | + |
| 237 | + return self.index > other |
| 238 | + |
| 239 | + def __ge__(self, other: Any) -> bool: |
| 240 | + if isinstance(other, str): |
| 241 | + return self.index >= DateUnit[other.upper()].index |
| 242 | + |
| 243 | + return self.index >= other |
| 244 | + |
| 245 | + def upper(self) -> str: |
| 246 | + """Uppercases the :class:`.Unit`. |
| 247 | +
|
| 248 | + Returns: |
| 249 | + :obj:`str`: The uppercased :class:`.Unit`. |
| 250 | +
|
| 251 | + Examples: |
| 252 | + >>> DateUnit.DAY.upper() |
| 253 | + 'DAY' |
| 254 | +
|
| 255 | + """ |
| 256 | + |
| 257 | + return self.value.upper() |
| 258 | + |
| 259 | + def lower(self) -> str: |
| 260 | + """Lowecases the :class:`.Unit`. |
| 261 | +
|
| 262 | + Returns: |
| 263 | + :obj:`str`: The lowercased :class:`.Unit`. |
| 264 | +
|
| 265 | + Examples: |
| 266 | + >>> DateUnit.DAY.lower() |
| 267 | + 'day' |
| 268 | +
|
| 269 | + """ |
| 270 | + |
| 271 | + return self.value.lower() |
0 commit comments