datedict 0.1.5__tar.gz → 0.1.7__tar.gz
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-0.1.5 → datedict-0.1.7}/PKG-INFO +1 -1
- {datedict-0.1.5 → datedict-0.1.7}/pyproject.toml +1 -1
- {datedict-0.1.5 → datedict-0.1.7}/src/datedict/DateDict.py +34 -17
- {datedict-0.1.5 → datedict-0.1.7}/src/datedict/YearDict.py +3 -0
- {datedict-0.1.5 → datedict-0.1.7}/.gitignore +0 -0
- {datedict-0.1.5 → datedict-0.1.7}/.gitlab-ci.yml +0 -0
- {datedict-0.1.5 → datedict-0.1.7}/LICENSE +0 -0
- {datedict-0.1.5 → datedict-0.1.7}/README.md +0 -0
- {datedict-0.1.5 → datedict-0.1.7}/pytest.ini +0 -0
- {datedict-0.1.5 → datedict-0.1.7}/src/datedict/__init__.py +0 -0
- {datedict-0.1.5 → datedict-0.1.7}/src/datedict/common.py +0 -0
|
@@ -8,6 +8,9 @@ from .common import NAN, ONE, Decimable, to_decimal, ZERO
|
|
|
8
8
|
class DateDict:
|
|
9
9
|
def __init__(self, data: Mapping[str, Decimable] = dict(), strict: bool = True):
|
|
10
10
|
dates = sorted(data.keys())
|
|
11
|
+
if len(dates) == 0:
|
|
12
|
+
self.data = {}
|
|
13
|
+
return
|
|
11
14
|
if strict:
|
|
12
15
|
# enforce contiguous coverage
|
|
13
16
|
if dates != [
|
|
@@ -24,21 +27,24 @@ class DateDict:
|
|
|
24
27
|
self.start_date, self.end_date = dates[0], dates[-1]
|
|
25
28
|
self.data: dict[str, Decimal] = {k: to_decimal(v) for k, v in data.items()}
|
|
26
29
|
|
|
27
|
-
def fill(
|
|
30
|
+
def fill(
|
|
31
|
+
self, start_date: str | date, end_date: str | date, value: Decimable
|
|
32
|
+
) -> "DateDict":
|
|
28
33
|
"""
|
|
29
34
|
Create a new graph with a specified range and value.
|
|
30
35
|
The range is defined by start_date and end_date.
|
|
31
36
|
"""
|
|
37
|
+
start = (
|
|
38
|
+
start_date
|
|
39
|
+
if isinstance(start_date, date)
|
|
40
|
+
else date.fromisoformat(start_date)
|
|
41
|
+
)
|
|
42
|
+
end = end_date if isinstance(end_date, date) else date.fromisoformat(end_date)
|
|
32
43
|
v = to_decimal(value)
|
|
33
44
|
return DateDict(
|
|
34
45
|
{
|
|
35
|
-
(
|
|
36
|
-
|
|
37
|
-
): v
|
|
38
|
-
for i in range(
|
|
39
|
-
(date.fromisoformat(end_date) - date.fromisoformat(start_date)).days
|
|
40
|
-
+ 1
|
|
41
|
-
)
|
|
46
|
+
(start + timedelta(days=i)).strftime("%Y-%m-%d"): v
|
|
47
|
+
for i in range((end - start).days + 1)
|
|
42
48
|
}
|
|
43
49
|
)
|
|
44
50
|
|
|
@@ -115,25 +121,36 @@ class DateDict:
|
|
|
115
121
|
)
|
|
116
122
|
|
|
117
123
|
def sum(
|
|
118
|
-
self: "DateDict",
|
|
124
|
+
self: "DateDict",
|
|
125
|
+
start_date: str | date | None = None,
|
|
126
|
+
end_date: str | date | None = None,
|
|
119
127
|
) -> Decimal:
|
|
120
128
|
"""
|
|
121
129
|
Return the sum of values in the specified range.
|
|
122
130
|
If a value is NaN, it is treated as zero.
|
|
123
131
|
"""
|
|
124
|
-
|
|
125
|
-
|
|
132
|
+
start = (
|
|
133
|
+
date.fromisoformat(self.start_date)
|
|
134
|
+
if start_date is None
|
|
135
|
+
else (
|
|
136
|
+
start_date
|
|
137
|
+
if isinstance(start_date, date)
|
|
138
|
+
else date.fromisoformat(start_date)
|
|
139
|
+
)
|
|
140
|
+
)
|
|
141
|
+
end = (
|
|
142
|
+
date.fromisoformat(self.end_date)
|
|
143
|
+
if end_date is None
|
|
144
|
+
else (
|
|
145
|
+
end_date if isinstance(end_date, date) else date.fromisoformat(end_date)
|
|
146
|
+
)
|
|
147
|
+
)
|
|
126
148
|
return sum(
|
|
127
149
|
[
|
|
128
150
|
(self.get(k, ZERO))
|
|
129
151
|
for k in map(
|
|
130
152
|
lambda x: x.strftime("%Y-%m-%d"),
|
|
131
|
-
[
|
|
132
|
-
date.fromisoformat(s) + timedelta(days=i)
|
|
133
|
-
for i in range(
|
|
134
|
-
(date.fromisoformat(e) - date.fromisoformat(s)).days + 1
|
|
135
|
-
)
|
|
136
|
-
],
|
|
153
|
+
[start + timedelta(days=i) for i in range((end - start).days + 1)],
|
|
137
154
|
)
|
|
138
155
|
],
|
|
139
156
|
ZERO,
|
|
@@ -7,6 +7,9 @@ from .common import NAN, ONE, Decimable, to_decimal, ZERO
|
|
|
7
7
|
class YearDict:
|
|
8
8
|
def __init__(self, data: Mapping[int, Decimable] = dict(), strict: bool = True):
|
|
9
9
|
years = sorted(data.keys())
|
|
10
|
+
if len(years) == 0:
|
|
11
|
+
self.data = {}
|
|
12
|
+
return
|
|
10
13
|
if strict:
|
|
11
14
|
# enforce contiguous coverage
|
|
12
15
|
if years != list(range(years[0], years[-1] + 1)):
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|