From 54344782ba02826c18a0544533d3785eedb37caa Mon Sep 17 00:00:00 2001 From: Benjamin Gutzmann Date: Tue, 16 Jun 2020 01:35:47 +0200 Subject: [PATCH] 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. --- .../enumerations/period_type_enumeration.py | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/python_dwd/enumerations/period_type_enumeration.py b/python_dwd/enumerations/period_type_enumeration.py index 1e7e2bc6f..909163744 100644 --- a/python_dwd/enumerations/period_type_enumeration.py +++ b/python_dwd/enumerations/period_type_enumeration.py @@ -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