datedict 0.1.1__py3-none-any.whl → 0.1.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of datedict might be problematic. Click here for more details.
- datedict/DateDict.py +6 -6
- datedict/YearDict.py +18 -11
- datedict/__init__.py +3 -2
- datedict/common.py +2 -2
- {datedict-0.1.1.dist-info → datedict-0.1.2.dist-info}/METADATA +1 -1
- datedict-0.1.2.dist-info/RECORD +8 -0
- datedict-0.1.1.dist-info/RECORD +0 -8
- {datedict-0.1.1.dist-info → datedict-0.1.2.dist-info}/WHEEL +0 -0
- {datedict-0.1.1.dist-info → datedict-0.1.2.dist-info}/licenses/LICENSE +0 -0
datedict/DateDict.py
CHANGED
|
@@ -3,7 +3,7 @@ from decimal import Decimal
|
|
|
3
3
|
|
|
4
4
|
from datetime import timedelta
|
|
5
5
|
|
|
6
|
-
from .common import
|
|
6
|
+
from .common import ZERO
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
class DateDict:
|
|
@@ -65,7 +65,7 @@ class DateDict:
|
|
|
65
65
|
If a value is None, it is treated as zero.
|
|
66
66
|
"""
|
|
67
67
|
return Decimal(
|
|
68
|
-
sum(value if value is not None else
|
|
68
|
+
sum(value if value is not None else ZERO for value in self.data.values())
|
|
69
69
|
)
|
|
70
70
|
|
|
71
71
|
def __add__(self, other: "Decimal | DateDict") -> "DateDict":
|
|
@@ -79,7 +79,7 @@ class DateDict:
|
|
|
79
79
|
else:
|
|
80
80
|
return DateDict(
|
|
81
81
|
{
|
|
82
|
-
k: ((v + (other.data.get(k) or
|
|
82
|
+
k: ((v + (other.data.get(k) or ZERO) if v is not None else None))
|
|
83
83
|
for k, v in self.data.items()
|
|
84
84
|
}
|
|
85
85
|
)
|
|
@@ -95,7 +95,7 @@ class DateDict:
|
|
|
95
95
|
elif isinstance(other, DateDict):
|
|
96
96
|
return DateDict(
|
|
97
97
|
{
|
|
98
|
-
k: ((v * (other.data.get(k) or
|
|
98
|
+
k: ((v * (other.data.get(k) or ZERO) if v is not None else None))
|
|
99
99
|
for k, v in self.data.items()
|
|
100
100
|
}
|
|
101
101
|
)
|
|
@@ -114,7 +114,7 @@ class DateDict:
|
|
|
114
114
|
elif isinstance(other, DateDict):
|
|
115
115
|
return DateDict(
|
|
116
116
|
{
|
|
117
|
-
k: ((v / (other.data.get(k) or
|
|
117
|
+
k: ((v / (other.data.get(k) or ZERO) if v is not None else None))
|
|
118
118
|
for k, v in self.data.items()
|
|
119
119
|
}
|
|
120
120
|
)
|
|
@@ -133,5 +133,5 @@ class DateDict:
|
|
|
133
133
|
"""
|
|
134
134
|
valid_values = [v for v in self.data.values() if v is not None]
|
|
135
135
|
if not valid_values:
|
|
136
|
-
return
|
|
136
|
+
return ZERO
|
|
137
137
|
return Decimal(sum(valid_values)) / len(valid_values)
|
datedict/YearDict.py
CHANGED
|
@@ -1,20 +1,27 @@
|
|
|
1
1
|
from decimal import Decimal
|
|
2
2
|
|
|
3
|
-
from .common import
|
|
3
|
+
from .common import to_decimal, ZERO
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
class YearDict:
|
|
7
|
-
def __init__(
|
|
7
|
+
def __init__(
|
|
8
|
+
self,
|
|
9
|
+
start_year=2025,
|
|
10
|
+
end_year=2025,
|
|
11
|
+
initial_value: Decimal | int | float = ZERO,
|
|
12
|
+
):
|
|
8
13
|
self.start_year: int = start_year
|
|
9
14
|
self.end_year: int = end_year
|
|
10
|
-
iv: Decimal =
|
|
11
|
-
self.data: dict[int, Decimal] = {
|
|
15
|
+
iv: Decimal = to_decimal(initial_value)
|
|
16
|
+
self.data: dict[int, Decimal] = {
|
|
17
|
+
y: iv for y in range(self.start_year, self.end_year + 1)
|
|
18
|
+
}
|
|
12
19
|
|
|
13
20
|
def __getitem__(self, year: int) -> Decimal:
|
|
14
21
|
return self.data[year]
|
|
15
22
|
|
|
16
23
|
def __setitem__(self, year: int, value):
|
|
17
|
-
self.data[int(year)] =
|
|
24
|
+
self.data[int(year)] = to_decimal(value)
|
|
18
25
|
|
|
19
26
|
def override(self, data: dict[int, Decimal | float | int | str]) -> "YearDict":
|
|
20
27
|
if not data:
|
|
@@ -25,22 +32,22 @@ class YearDict:
|
|
|
25
32
|
raise ValueError("Data must cover all years in the contiguous range.")
|
|
26
33
|
self.start_year, self.end_year = ys[0], ys[-1] # inclusive
|
|
27
34
|
self.data = {
|
|
28
|
-
y:
|
|
35
|
+
y: to_decimal(data[y]) for y in range(self.start_year, self.end_year + 1)
|
|
29
36
|
}
|
|
30
37
|
return self
|
|
31
38
|
|
|
32
|
-
def fit(self, start_year: int, end_year: int, initial_value: Decimal =
|
|
39
|
+
def fit(self, start_year: int, end_year: int, initial_value: Decimal = ZERO):
|
|
33
40
|
self.start_year, self.end_year = int(start_year), int(end_year)
|
|
34
|
-
iv =
|
|
41
|
+
iv = to_decimal(initial_value)
|
|
35
42
|
self.data = {
|
|
36
|
-
y:
|
|
43
|
+
y: to_decimal(self.data[y]) if y in self.data else iv
|
|
37
44
|
for y in range(self.start_year, self.end_year + 1)
|
|
38
45
|
}
|
|
39
46
|
return self
|
|
40
47
|
|
|
41
48
|
def non_negative(self) -> "YearDict":
|
|
42
49
|
out = YearDict(self.start_year, self.end_year)
|
|
43
|
-
out.data = {y: (v if v >=
|
|
50
|
+
out.data = {y: (v if v >= ZERO else ZERO) for y, v in self.data.items()}
|
|
44
51
|
return out
|
|
45
52
|
|
|
46
53
|
def sum(
|
|
@@ -48,7 +55,7 @@ class YearDict:
|
|
|
48
55
|
) -> Decimal:
|
|
49
56
|
sy = self.start_year if start_year is None else int(start_year)
|
|
50
57
|
ey = self.end_year if end_year is None else int(end_year)
|
|
51
|
-
return sum((self.data[y] for y in range(sy, ey + 1) if y in self.data),
|
|
58
|
+
return sum((self.data[y] for y in range(sy, ey + 1) if y in self.data), ZERO)
|
|
52
59
|
|
|
53
60
|
def __mul__(self, other):
|
|
54
61
|
result = YearDict(self.start_year, self.end_year)
|
datedict/__init__.py
CHANGED
datedict/common.py
CHANGED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
datedict/DateDict.py,sha256=JrqKAJIcZL4Q7JMvzKPZQCzXGQ3wZAEkpWABAfHXLr0,4658
|
|
2
|
+
datedict/YearDict.py,sha256=P8t43vUyOtxxABtIKf1epAU7Ohqx2-V9e9t-PyqFEh0,3798
|
|
3
|
+
datedict/__init__.py,sha256=pZv27luenqTNoEm9jdodgCu3B5vnYdDgMYllFwCA2Sc,153
|
|
4
|
+
datedict/common.py,sha256=yW3ohkISis4FP6baN8QJFPYHCkpVyecip7AlFg49_Kw,314
|
|
5
|
+
datedict-0.1.2.dist-info/METADATA,sha256=p8BOciltez0gbtRl-UPsG0ihMtI4cSn5w8PBm1sNlwM,1985
|
|
6
|
+
datedict-0.1.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
+
datedict-0.1.2.dist-info/licenses/LICENSE,sha256=ULDgLM2c4o9DLMR-VEkeyuB-DRWBFX7VunJmlWShWxQ,1072
|
|
8
|
+
datedict-0.1.2.dist-info/RECORD,,
|
datedict-0.1.1.dist-info/RECORD
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
datedict/DateDict.py,sha256=2jXhhR7j9cs6WN9pTtSbUC-xpMtx6hGy-1Lr-sV4Piw,4659
|
|
2
|
-
datedict/YearDict.py,sha256=DVSIG3eZ_v4sp7r9ybzXgfJaF_W7O-CO2z4IavzYlzA,3731
|
|
3
|
-
datedict/__init__.py,sha256=KnG-yvfCIx2MXNQ6gJ4QrZP4jZVYx84FUV9KnpQctu8,120
|
|
4
|
-
datedict/common.py,sha256=GQS1uxInOxhQI6dg2Aqxe8A_OIQ-L7R4WJbZJoDplmE,313
|
|
5
|
-
datedict-0.1.1.dist-info/METADATA,sha256=4T8ERj7yKNTFKj55eLB-QgreJbBppZ2fXylpT8cNOss,1985
|
|
6
|
-
datedict-0.1.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
7
|
-
datedict-0.1.1.dist-info/licenses/LICENSE,sha256=ULDgLM2c4o9DLMR-VEkeyuB-DRWBFX7VunJmlWShWxQ,1072
|
|
8
|
-
datedict-0.1.1.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|