File size: 5,130 Bytes
618c5a9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
"""
.. codeauthor:: Tsuyoshi Hombashi <[email protected]>
"""
import abc
from decimal import Decimal
from typing import Any, List, Optional, Sequence, Union
from typepy import RealNumber
T = Union[int, float, Decimal]
NAN = Decimal("nan")
class AbstractContainer(metaclass=abc.ABCMeta):
@abc.abstractproperty
def min_value(self) -> Optional[Decimal]: # pragma: no cover
pass
@abc.abstractproperty
def max_value(self) -> Optional[Decimal]: # pragma: no cover
pass
@abc.abstractmethod
def mean(self) -> Decimal: # pragma: no cover
pass
@abc.abstractmethod
def update(self, value: Optional[T]) -> None: # pragma: no cover
pass
@abc.abstractmethod
def merge(self, value: "AbstractContainer") -> None: # pragma: no cover
pass
def __repr__(self) -> str:
if not self.has_value():
return "None"
return ", ".join([f"min={self.min_value}", f"max={self.max_value}"])
def has_value(self) -> bool:
return self.min_value is not None and self.max_value is not None
def is_same_value(self) -> bool:
return self.has_value() and self.min_value == self.max_value
def is_zero(self) -> bool:
return self.has_value() and self.min_value == 0 and self.max_value == 0
class ListContainer(AbstractContainer):
__slots__ = ("__value_list",)
@property
def min_value(self) -> Optional[Decimal]:
try:
return min(self.__value_list)
except ValueError:
return None
@property
def max_value(self) -> Optional[Decimal]:
try:
return max(self.__value_list)
except ValueError:
return None
@property
def value_list(self) -> List[Decimal]:
return self.__value_list
def __init__(self, value_list: Optional[List[Decimal]] = None) -> None:
if value_list is None:
self.__value_list: List[Decimal] = []
return
for value in value_list:
self.update(value)
def mean(self) -> Decimal:
try:
return Decimal(sum(self.__value_list) / len(self.__value_list))
except ZeroDivisionError:
return NAN
def update(self, value: Union[int, float, Decimal, None]) -> None:
if value is None:
return
store_value = RealNumber(value).try_convert()
if store_value is None:
return
self.__value_list.append(store_value)
def merge(self, value: "AbstractContainer") -> None:
if not isinstance(value, ListContainer):
return
for v in value.value_list:
self.update(v)
class MinMaxContainer(AbstractContainer):
__slots__ = ("__min_value", "__max_value")
def __init__(self, value_list: Optional[Sequence[Decimal]] = None) -> None:
self.__min_value: Optional[Decimal] = None
self.__max_value: Optional[Decimal] = None
if value_list is None:
return
for value in value_list:
self.update(value)
@property
def min_value(self) -> Optional[Decimal]:
return self.__min_value
@property
def max_value(self) -> Optional[Decimal]:
return self.__max_value
def __eq__(self, other: Any) -> bool:
if not isinstance(other, MinMaxContainer):
return False
return all([self.min_value == other.min_value, self.max_value == other.max_value])
def __ne__(self, other: Any) -> bool:
if not isinstance(other, MinMaxContainer):
return True
return any([self.min_value != other.min_value, self.max_value != other.max_value])
def __contains__(self, x: T) -> bool:
if self.min_value is None:
return False
if self.max_value is None:
return False
return self.min_value <= x <= self.max_value
def diff(self) -> Decimal:
if self.min_value is None:
return NAN
if self.max_value is None:
return NAN
try:
return self.max_value - self.min_value
except TypeError:
return NAN
def mean(self) -> Decimal:
if self.min_value is None:
return NAN
if self.max_value is None:
return NAN
try:
return (self.max_value + self.min_value) * Decimal("0.5")
except TypeError:
return NAN
def update(self, value: Optional[T]) -> None:
if value is None:
return
decimal_value = Decimal(value)
if self.__min_value is None:
self.__min_value = decimal_value
else:
self.__min_value = min(self.__min_value, decimal_value)
if self.__max_value is None:
self.__max_value = decimal_value
else:
self.__max_value = max(self.__max_value, decimal_value)
def merge(self, value: "AbstractContainer") -> None:
if not isinstance(value, MinMaxContainer):
return
self.update(value.min_value)
self.update(value.max_value)
|