datedict 0.1.5__py3-none-any.whl → 0.1.7__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 CHANGED
@@ -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(self, start_date, end_date, value: Decimable) -> "DateDict":
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
- (date.fromisoformat(start_date) + timedelta(days=i)).strftime(
36
- "%Y-%m-%d"
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", start: str | None = None, end: str | None = None
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
- s = self.start_date if start is None else start
125
- e = self.end_date if end is None else end
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,
datedict/YearDict.py CHANGED
@@ -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)):
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datedict
3
- Version: 0.1.5
3
+ Version: 0.1.7
4
4
  Summary: DateDict and YearDict: date-aware dictionary structures
5
5
  Project-URL: Homepage, https://gitlab.com/grisus/datedict
6
6
  Project-URL: Issues, https://gitlab.com/grisus/datedict/-/issues
@@ -0,0 +1,8 @@
1
+ datedict/DateDict.py,sha256=2iTPwp81PA-ni-FJW-Ok-G7wiOJBxyJST4dwrJoWIpE,8129
2
+ datedict/YearDict.py,sha256=avVXgCp_zgSL5khqCjR6Z5vh-vqFwEdK-5TO9wfhjfc,5493
3
+ datedict/__init__.py,sha256=4QSCqCW_owSqD3k3Oz1WAsB4p_vvzUlINroAxxFCEWQ,201
4
+ datedict/common.py,sha256=aUCsMV6roKGQIQXSFIJIN0cxAXIlK_YyRDWS1EIBu8E,424
5
+ datedict-0.1.7.dist-info/METADATA,sha256=m0njoUnyNurBSPquzGFjYvYpKcmAKhTIWtVOHD5mAe0,1991
6
+ datedict-0.1.7.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ datedict-0.1.7.dist-info/licenses/LICENSE,sha256=GMmiaNfJSsljmGtIp9lCY6V-Dkc9NDZn0jSZuAbFe7Y,1070
8
+ datedict-0.1.7.dist-info/RECORD,,
@@ -1,8 +0,0 @@
1
- datedict/DateDict.py,sha256=E697A7QgjQMwwHqcMsRVTCjWzZLw8sovHZG9caOw5CQ,7701
2
- datedict/YearDict.py,sha256=xwVhMLklo7RjCChPsmSQ63pXaqbeAfIVe_zDnGZOIcQ,5419
3
- datedict/__init__.py,sha256=4QSCqCW_owSqD3k3Oz1WAsB4p_vvzUlINroAxxFCEWQ,201
4
- datedict/common.py,sha256=aUCsMV6roKGQIQXSFIJIN0cxAXIlK_YyRDWS1EIBu8E,424
5
- datedict-0.1.5.dist-info/METADATA,sha256=RVL0i2_rKRaDv7aTLHnvDVnPK0SjWm7VrAZ7s7pQV90,1991
6
- datedict-0.1.5.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- datedict-0.1.5.dist-info/licenses/LICENSE,sha256=GMmiaNfJSsljmGtIp9lCY6V-Dkc9NDZn0jSZuAbFe7Y,1070
8
- datedict-0.1.5.dist-info/RECORD,,