openepd 7.5.0__py3-none-any.whl → 7.6.0__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.
openepd/__version__.py CHANGED
@@ -13,4 +13,4 @@
13
13
  # See the License for the specific language governing permissions and
14
14
  # limitations under the License.
15
15
  #
16
- VERSION = "7.5.0"
16
+ VERSION = "7.6.0"
@@ -0,0 +1,15 @@
1
+ #
2
+ # Copyright 2025 by C Change Labs Inc. www.c-change-labs.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
@@ -0,0 +1,15 @@
1
+ #
2
+ # Copyright 2025 by C Change Labs Inc. www.c-change-labs.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
@@ -0,0 +1,192 @@
1
+ #
2
+ # Copyright 2025 by C Change Labs Inc. www.c-change-labs.com
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ #
16
+ __all__ = (
17
+ "BaseDataMapper",
18
+ "KeyValueMapper",
19
+ "ReferenceMapper",
20
+ "RegexMapper",
21
+ "SimpleDataMapper",
22
+ )
23
+
24
+ import abc
25
+ from collections.abc import Mapping
26
+ import re
27
+ from typing import Generic, TypeAlias, TypeVar, assert_never, cast
28
+
29
+ T = TypeVar("T")
30
+ K = TypeVar("K")
31
+
32
+
33
+ class BaseDataMapper(Generic[T, K], abc.ABC):
34
+ """
35
+ Base class for all data mappers.
36
+
37
+ Data mappers are objects used to map some input values to output values of different types.
38
+
39
+ Typical use case is mapping between different aliases of impact names with OpenEpd naming conventions.
40
+ """
41
+
42
+ @abc.abstractmethod
43
+ def map(self, input_value: T, default_value: K | None, *, raise_if_missing: bool = False) -> K | None:
44
+ """
45
+ Map the input value to the output value.
46
+
47
+ :param input_value: The input value to map.
48
+ :param default_value: The default value to return if there is no mapping for the input value.
49
+ :param raise_if_missing: Whether to raise an exception if there is no mapping for the input value.
50
+
51
+ :raise ValueError: If there is no mapping for the input value and raise_if_missing is True.
52
+ """
53
+ pass
54
+
55
+
56
+ class SimpleDataMapper(BaseDataMapper[T, T], Generic[T]):
57
+ """A data mapper that does not change the type of the input value."""
58
+
59
+ DATABASE: Mapping[T, T] = {}
60
+
61
+ def map(self, input_value: T, default_value: T | None, *, raise_if_missing: bool = False) -> T | None:
62
+ """
63
+ Map the input value to the output value.
64
+
65
+ :param input_value: The input value to map.
66
+ :param default_value: The default value to return if there is no mapping for the input value.
67
+ :param raise_if_missing: Whether to raise an exception if there is no mapping for the input value.
68
+
69
+ :raise ValueError: If there is no mapping for the input value and raise_if_missing is True.
70
+ """
71
+ if raise_if_missing and input_value not in self.DATABASE:
72
+ msg = f"No mapping for input value: {input_value}"
73
+ raise ValueError(msg)
74
+
75
+ return self.DATABASE.get(input_value, default_value)
76
+
77
+
78
+ class KeyValueMapper(BaseDataMapper[str, T], Generic[T]):
79
+ """
80
+ A data mapper that maps input values to output values using keywords.
81
+
82
+ List of values is expected to be a list string object or a list of objects easily castable to string.
83
+ """
84
+
85
+ KV: Mapping[str, list[T]] = {}
86
+
87
+ def map(self, input_value: str, default_value: T | None, *, raise_if_missing: bool = False) -> T | None:
88
+ """
89
+ Map the input value to the output value using keywords.
90
+
91
+ :param input_value: The input value to map.
92
+ :param default_value: The default value to return if there is no mapping for input value.
93
+ :param raise_if_missing: Whether to raise an exception if there is no mapping for the input value.
94
+
95
+ :raise ValueError: If there is no mapping for the input value and raise_if_missing is True.
96
+ """
97
+ for impact_name, keywords in self.KV.items():
98
+ for keyword in keywords:
99
+ if str(keyword).strip().lower() in input_value.strip().lower():
100
+ return cast(T, impact_name)
101
+
102
+ if raise_if_missing:
103
+ msg = f"No mapping for input value: {input_value}"
104
+ raise ValueError(msg)
105
+
106
+ return default_value
107
+
108
+
109
+ class RegexMapper(BaseDataMapper[str, T], Generic[T]):
110
+ """A data mapper that maps input values to output values using regex."""
111
+
112
+ PATTERNS: dict[str, str] = {}
113
+ _compiled_patterns: dict[str, re.Pattern]
114
+
115
+ def __init__(self) -> None:
116
+ self._compiled_patterns: dict[str, re.Pattern] = {
117
+ key: re.compile(pattern, re.IGNORECASE) for key, pattern in self.PATTERNS.items()
118
+ }
119
+
120
+ def map(self, input_value: str, default_value: T | None, *, raise_if_missing: bool = False) -> T | None:
121
+ """
122
+ Map the input value to the output value using regex.
123
+
124
+ :param input_value: The input value to map.
125
+ :param default_value: The default value to return if there is no mapping for an input value.
126
+
127
+ :param raise_if_missing: Whether to raise an exception if there is no mapping for the input value.
128
+
129
+ :raise ValueError: If there is no mapping for the input value and raise_if_missing is True.
130
+ """
131
+ for impact_name, pattern in self._compiled_patterns.items():
132
+ if pattern.search(input_value.strip().lower()):
133
+ return cast(T, impact_name)
134
+
135
+ if raise_if_missing:
136
+ msg = f"No mapping for input value: {input_value}"
137
+ raise ValueError(msg)
138
+
139
+ return default_value
140
+
141
+
142
+ _TRmRules: TypeAlias = str | re.Pattern | list[str | re.Pattern]
143
+
144
+
145
+ class ReferenceMapper(BaseDataMapper[str, _TRmRules]):
146
+ """
147
+ A mapper that maps input values of any form to the expected value format.
148
+
149
+ Expected values may be a value or a list of values. Values are expected to be a string object, regular expressions,
150
+ or objects easily castable to string.
151
+ """
152
+
153
+ def map(self, input_value: str, default_value: str | None, *, raise_if_missing: bool = False) -> str | None: # type: ignore[override]
154
+ """
155
+ Return specified key as a value if any of the values in the list matches the input value.
156
+
157
+ :param input_value: value to be checked against the list of specified rules
158
+ :param default_value: default value to return if no match is found
159
+ :param raise_if_missing: whether to raise an exception if no match is found
160
+
161
+ :return: mapped value if find any match, else default value
162
+
163
+ :raise ValueError: if no match is found and raise_if_missing is True
164
+ """
165
+ for key, value in self.MAPPING.items():
166
+ if not self._is_applied(input_value, value):
167
+ continue
168
+ return key
169
+
170
+ if raise_if_missing:
171
+ msg = f"No mapping for input value: {input_value}"
172
+ raise ValueError(msg)
173
+
174
+ return default_value
175
+
176
+ def _is_applied(self, input_value: str, rules: _TRmRules) -> bool:
177
+ if isinstance(rules, str | re.Pattern):
178
+ return self._is_applied_to_item(input_value, rules)
179
+ elif isinstance(rules, list):
180
+ return any(self._is_applied_to_item(input_value, rule) for rule in rules)
181
+ else:
182
+ assert_never(rules)
183
+
184
+ def _is_applied_to_item(self, input_value: str, rule: str | re.Pattern) -> bool:
185
+ if isinstance(rule, str):
186
+ return input_value.strip().lower() == rule.strip().lower()
187
+ elif isinstance(rule, re.Pattern):
188
+ return bool(rule.search(input_value.strip().lower()))
189
+ else:
190
+ assert_never(rule)
191
+
192
+ MAPPING: Mapping[str, _TRmRules] = {}
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: openepd
3
- Version: 7.5.0
3
+ Version: 7.6.0
4
4
  Summary: Python library to work with OpenEPD format
5
5
  License: Apache-2.0
6
6
  Author: C-Change Labs
@@ -1,5 +1,5 @@
1
1
  openepd/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
2
- openepd/__version__.py,sha256=wROB4Az8lHLZ19vzPzFg6vUHTTs6IDmcRyEAEx07WWs,638
2
+ openepd/__version__.py,sha256=SIlr9JF7DMOxJeLOuOMUaby5LJ18vD9hLPBZxuEJy_w,638
3
3
  openepd/api/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
4
4
  openepd/api/average_dataset/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
5
5
  openepd/api/average_dataset/generic_estimate_sync_api.py,sha256=mjTT8eGtfj6Fgp-wcs0cCWA7DJo1KL_iQ75rgKkaY3c,8037
@@ -146,7 +146,10 @@ openepd/model/validation/numbers.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9
146
146
  openepd/model/validation/quantity.py,sha256=M9dz3byTK6Lrys43I0Gq7n2b0aE8WYys-idxi6bKCII,21755
147
147
  openepd/model/versioning.py,sha256=LldrNjPjVBsVTWyJrBe6VoI19B0d47QXbHk_6omXPxc,4561
148
148
  openepd/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
149
- openepd-7.5.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
150
- openepd-7.5.0.dist-info/METADATA,sha256=LWKh9QYyF-8Zw1fsNsaQwZZmUYTTkAEf9JPdKwVy9Rk,9810
151
- openepd-7.5.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
152
- openepd-7.5.0.dist-info/RECORD,,
149
+ openepd/utils/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
150
+ openepd/utils/mapping/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
151
+ openepd/utils/mapping/common.py,sha256=WphCzwQQlzX11tUk88Ubyq3QPBLvH0tBPSIuH0kmiug,7339
152
+ openepd-7.6.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
153
+ openepd-7.6.0.dist-info/METADATA,sha256=P3nO_EP7rA_3qyoV3RCC-_wDURAPDyebRetk0YLUc14,9810
154
+ openepd-7.6.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
155
+ openepd-7.6.0.dist-info/RECORD,,