datedict 0.1.1__tar.gz → 0.1.2__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.

@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: datedict
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: DateDict and YearDict: date-aware dictionary structures
5
5
  Project-URL: Homepage, https://github.com/you/datedict
6
6
  Project-URL: Issues, https://github.com/you/datedict/issues
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "datedict"
7
- version = "0.1.1"
7
+ version = "0.1.2"
8
8
  description = "DateDict and YearDict: date-aware dictionary structures"
9
9
  readme = "README.md"
10
10
  requires-python = ">=3.10"
@@ -3,7 +3,7 @@ from decimal import Decimal
3
3
 
4
4
  from datetime import timedelta
5
5
 
6
- from .common import _to_decimal, _Z
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 _Z for value in self.data.values())
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 _Z) if v is not None else None))
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 _Z) if v is not None else None))
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 _Z) if v is not None else None))
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 _Z
136
+ return ZERO
137
137
  return Decimal(sum(valid_values)) / len(valid_values)
@@ -1,20 +1,27 @@
1
1
  from decimal import Decimal
2
2
 
3
- from .common import _to_decimal, _Z
3
+ from .common import to_decimal, ZERO
4
4
 
5
5
 
6
6
  class YearDict:
7
- def __init__(self, start_year=2025, end_year=2025, initial_value: Decimal | int | float = _Z):
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 = _to_decimal(initial_value)
11
- self.data: dict[int, Decimal] = {y: iv for y in range(self.start_year, self.end_year + 1)}
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)] = _to_decimal(value)
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: _to_decimal(data[y]) for y in range(self.start_year, self.end_year + 1)
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 = _Z):
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 = _to_decimal(initial_value)
41
+ iv = to_decimal(initial_value)
35
42
  self.data = {
36
- y: _to_decimal(self.data[y]) if y in self.data else iv
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 >= _Z else _Z) for y, v in self.data.items()}
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), _Z)
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)
@@ -0,0 +1,6 @@
1
+ from .DateDict import DateDict
2
+ from .YearDict import YearDict
3
+ from .common import ZERO
4
+
5
+ __all__ = ["DateDict", "YearDict", "ZERO"]
6
+ __version__ = "0.1.2"
@@ -1,10 +1,10 @@
1
1
  from decimal import Decimal
2
2
 
3
3
 
4
- _Z = Decimal("0")
4
+ ZERO = Decimal("0")
5
5
 
6
6
 
7
- def _to_decimal(x) -> Decimal:
7
+ def to_decimal(x) -> Decimal:
8
8
  if isinstance(x, Decimal):
9
9
  return x
10
10
  if isinstance(x, (int, str)):
@@ -1,5 +0,0 @@
1
- from .DateDict import DateDict
2
- from .YearDict import YearDict
3
-
4
- __all__ = ["DateDict", "YearDict"]
5
- __version__ = "0.1.1"
File without changes
File without changes
File without changes
File without changes