openepd 6.24.0__py3-none-any.whl → 6.25.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 +1 -1
- openepd/model/specs/singular/__init__.py +2 -4
- openepd/utils/__init__.py +15 -0
- openepd/utils/mapping/__init__.py +15 -0
- openepd/utils/mapping/common.py +192 -0
- {openepd-6.24.0.dist-info → openepd-6.25.0.dist-info}/METADATA +1 -1
- {openepd-6.24.0.dist-info → openepd-6.25.0.dist-info}/RECORD +9 -6
- {openepd-6.24.0.dist-info → openepd-6.25.0.dist-info}/LICENSE +0 -0
- {openepd-6.24.0.dist-info → openepd-6.25.0.dist-info}/WHEEL +0 -0
openepd/__version__.py
CHANGED
@@ -22,8 +22,6 @@ from collections.abc import Sequence
|
|
22
22
|
import types
|
23
23
|
from typing import Any, ClassVar, TypeVar
|
24
24
|
|
25
|
-
from pydantic.v1.fields import FieldInfo
|
26
|
-
|
27
25
|
from openepd.compat.pydantic import pyd
|
28
26
|
from openepd.model.specs.base import BaseOpenEpdHierarchicalSpec
|
29
27
|
from openepd.model.specs.singular.accessories import AccessoriesV1
|
@@ -208,9 +206,9 @@ class Specs(BaseOpenEpdHierarchicalSpec):
|
|
208
206
|
"""
|
209
207
|
klass = self.__class__
|
210
208
|
|
211
|
-
fields: dict[str, FieldInfo] = klass.__fields__ # type: ignore[assignment]
|
209
|
+
fields: dict[str, pyd.fields.FieldInfo] = klass.__fields__ # type: ignore[assignment]
|
212
210
|
|
213
|
-
field: FieldInfo | None
|
211
|
+
field: pyd.fields.FieldInfo | None
|
214
212
|
for i, key in enumerate(keys, start=1):
|
215
213
|
field = fields.get(key)
|
216
214
|
if field is None:
|
@@ -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,5 +1,5 @@
|
|
1
1
|
openepd/__init__.py,sha256=fhxfEyEurLvSfvQci-vb3njzl_lvhcLXiZrecCOaMU8,794
|
2
|
-
openepd/__version__.py,sha256=
|
2
|
+
openepd/__version__.py,sha256=hDgSAJf2ChxmeWlgYCHYlntPXqIh3A42Hhy4RbDRnLc,639
|
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=_eZt_jGVL1a3p9cr-EF39Ve9Vl5sB8zwzTc_slnRL50,7975
|
@@ -98,7 +98,7 @@ openepd/model/specs/range/thermal_moisture_protection.py,sha256=aUpStISG2snc8aZ7
|
|
98
98
|
openepd/model/specs/range/utility_piping.py,sha256=mQTrKbMTES_0Yta9DBnKehsmA2BAN_zYF_uPZCTKLPo,2665
|
99
99
|
openepd/model/specs/range/wood.py,sha256=ZMStFsFOw5d6tCL2P7kksBnlYhwfvWJBZqoAdqSIUDg,6917
|
100
100
|
openepd/model/specs/range/wood_joists.py,sha256=NCBHL0gdKwdKlGCP6up_Royl9BtWUUeuFfS1r2vIZ30,1785
|
101
|
-
openepd/model/specs/singular/__init__.py,sha256=
|
101
|
+
openepd/model/specs/singular/__init__.py,sha256=YaySkDd4qgmHCk58TfMhgsiAnU_IY83xK847NEvOC60,11414
|
102
102
|
openepd/model/specs/singular/accessories.py,sha256=xcuSJrG84oTm_w76_wlv_sItfK36gSyDmUD2TnpZnVE,2013
|
103
103
|
openepd/model/specs/singular/aggregates.py,sha256=oSfArxvLUj63w_U0tZViRyiOpMRRjE72ncy2BimKxco,2934
|
104
104
|
openepd/model/specs/singular/aluminium.py,sha256=7Z3WmuW22o92cPHfR6X_OBSoer0V5llwiNdQmWgKTx8,2408
|
@@ -150,7 +150,10 @@ openepd/model/validation/quantity.py,sha256=mP4gIkeOGZuHRhprsf_BX11Cic75NssYxOTk
|
|
150
150
|
openepd/model/versioning.py,sha256=wBZdOVL3ND9FMIRU9PS3vx9M_7MBiO70xYPQvPez6po,4522
|
151
151
|
openepd/patch_pydantic.py,sha256=bO7U5HqthFol0vfycb0a42UAGL3KOQ8-9MW4yCWOFP0,4150
|
152
152
|
openepd/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
153
|
-
openepd
|
154
|
-
openepd
|
155
|
-
openepd
|
156
|
-
openepd-6.
|
153
|
+
openepd/utils/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
|
154
|
+
openepd/utils/mapping/__init__.py,sha256=9THJcV3LT7JDBOMz1px-QFf_sdJ0LOqJ5dmA9Dvvtd4,620
|
155
|
+
openepd/utils/mapping/common.py,sha256=WphCzwQQlzX11tUk88Ubyq3QPBLvH0tBPSIuH0kmiug,7339
|
156
|
+
openepd-6.25.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
157
|
+
openepd-6.25.0.dist-info/METADATA,sha256=sXZ1WJU-4BzBy9ZSCjbmL_cvd_44recegC2mmALLGlE,9827
|
158
|
+
openepd-6.25.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
159
|
+
openepd-6.25.0.dist-info/RECORD,,
|
File without changes
|
File without changes
|