forked from earthobservations/wetterdienst
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make PeriodType enumeration sortable
Ordering is required for the PeriodType enumeration in order for it to be sorted. PeriodType needs to be sortable for the request definition where for the case of requesting data for several periods, we want preferably the historical data with quality marks and drop overlapping values from other periods.
- Loading branch information
Showing
1 changed file
with
45 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,55 @@ | ||
""" enumeration for period_type """ | ||
from enum import Enum | ||
import functools | ||
|
||
|
||
""" | ||
Following this: https://stackoverflow.com/questions/39268052/how-to-compare-enums-in-python | ||
Ordering is required for the PeriodType enumeration in order for it to be sorted. | ||
PeriodType needs to be sortable for the request definition where for the case of | ||
requesting data for several periods, we want preferably the historical data with | ||
quality marks and drop overlapping values from other periods. | ||
""" | ||
|
||
|
||
@functools.total_ordering | ||
class PeriodType(Enum): | ||
""" enumeration for different period types of storages on dwd server""" | ||
""" enumeration for different period types of storage on dwd server""" | ||
@property | ||
def _period_type_order_mapping(self): | ||
# IMPORTANT: THIS DEPENDS ON THE NAMING CONVENTIONS USED IN THE PeriodType | ||
# ENUMERATION AS SHOWN BELOW | ||
return { | ||
"HISTORICAL": 0, | ||
"RECENT": 1, | ||
"NOW": 2, | ||
"ROW": 3 | ||
} | ||
|
||
HISTORICAL = "historical" | ||
RECENT = "recent" | ||
NOW = "now" | ||
ROW = "" | ||
|
||
def __lt__(self, other): | ||
if self.__class__ is other.__class__: | ||
return self._period_type_order_mapping[self.name] < \ | ||
self._period_type_order_mapping[other.name] | ||
return NotImplemented | ||
|
||
def __gt__(self, other): | ||
if self.__class__ is other.__class__: | ||
return self._period_type_order_mapping[self.name] > \ | ||
self._period_type_order_mapping[other.name] | ||
return NotImplemented | ||
|
||
def __ge__(self, other): | ||
if self.__class__ is other.__class__: | ||
return not self.__lt__(other) | ||
return NotImplemented | ||
|
||
def __le__(self, other): | ||
if self.__class__ is other.__class__: | ||
return not self.__gt__(other) | ||
return NotImplemented |