Skip to content

Commit f9d0d87

Browse files
committed
Refines validators with enhanced logging
1 parent f97b4b6 commit f9d0d87

File tree

2 files changed

+104
-45
lines changed

2 files changed

+104
-45
lines changed

src/easydiffraction/core/guards.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def wrapper(*args: P.args, **kwargs: P.kwargs) -> Optional[R]:
5454
name = f'{unique_name}.{attr_name}'
5555
message = (
5656
f'Type mismatch for <{name}>. '
57-
f'Provided {new!r} ({new_type}) is not {expected_type}'
57+
f'Provided {new!r} ({new_type}) is not {expected_type}.'
5858
)
5959
else:
6060
message = f'Type mismatch in {func.__qualname__}: {err}'
@@ -111,21 +111,27 @@ def validate(
111111
log.debug(message)
112112
return self.default
113113

114+
keep_current = current is not None
115+
if keep_current:
116+
extra_info = f' Keeping current {current!r}.'
117+
else:
118+
extra_info = f' Using default {self.default!r}.'
119+
114120
if not isinstance(new, (float, int, np.floating, np.integer)):
115121
message = (
116122
f'Type mismatch for <{name}>. '
117-
f'Provided {new!r} ({type(new).__name__}) is not float.'
123+
f'Provided {new!r} ({type(new).__name__}) is not float.{extra_info}'
118124
)
119125
log.error(message, exc_type=TypeError)
120-
return current if current is not None else self.default
126+
return current if keep_current else self.default
121127

122128
if (self.ge is not None and new < self.ge) or (self.le is not None and new > self.le):
123129
message = (
124130
f'Value mismatch for <{name}>. '
125-
f'Provided {new} is outside of [{self.ge}, {self.le}].'
131+
f'Provided {new} is outside of [{self.ge}, {self.le}].{extra_info}'
126132
)
127133
log.error(message, exc_type=ValueError)
128-
return current if current is not None else self.default
134+
return current if keep_current else self.default
129135

130136
log.debug(f'Setting <{name}> to validated {new!r}.')
131137
return new
@@ -171,10 +177,16 @@ def validate(
171177
log.debug(message)
172178
return self.default
173179

180+
keep_current = current is not None
181+
if keep_current:
182+
extra_info = f' Keeping current {current!r}.'
183+
else:
184+
extra_info = f' Using default {self.default!r}.'
185+
174186
if new not in self.allowed_values:
175-
message = f'Value mismatch for <{name}>. Provided {new!r} is unknown.'
187+
message = f'Value mismatch for <{name}>. Provided {new!r} is unknown.{extra_info}'
176188
log.error(message, exc_type=ValueError)
177-
return current if current is not None else self.default
189+
return current if keep_current else self.default
178190

179191
log.debug(f'Setting <{name}> to validated {new!r}.')
180192
return new
@@ -203,21 +215,27 @@ def validate(
203215
log.debug(message)
204216
return self.default
205217

218+
keep_current = current is not None
219+
if keep_current:
220+
extra_info = f' Keeping current {current!r}.'
221+
else:
222+
extra_info = f' Using default {self.default!r}.'
223+
206224
if not isinstance(new, str):
207225
message = (
208226
f'Type mismatch for <{name}>. '
209-
f'Provided {new!r} ({type(new).__name__}) is not string.'
227+
f'Provided {new!r} ({type(new).__name__}) is not string.{extra_info}'
210228
)
211229
log.error(message, exc_type=TypeError)
212-
return current if current is not None else self.default
230+
return current if keep_current else self.default
213231

214232
if not self._regex.match(new):
215233
message = (
216234
f'Value mismatch for <{name}>. '
217-
f"Provided {new!r} does not match pattern '{self.pattern}'."
235+
f"Provided {new!r} does not match pattern '{self.pattern}'.{extra_info}"
218236
)
219237
log.error(message, exc_type=ValueError)
220-
return current if current is not None else self.default
238+
return current if keep_current else self.default
221239

222240
log.debug(f'Setting <{name}> to validated {new!r}.')
223241
return new

tutorials-drafts/Untitled.ipynb

Lines changed: 75 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,76 +2,91 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 55,
5+
"execution_count": 1,
66
"id": "2b4ff90d-5a58-4202-ac2a-874168a2c6a2",
77
"metadata": {},
88
"outputs": [],
99
"source": [
10-
"from easydiffraction.core.categories import CategoryItem, CategoryCollection\n",
11-
"from easydiffraction.core.datablocks import DatablockItem, DatablockCollection\n",
12-
"from easydiffraction.core.guards import RangeValidator, \\\n",
13-
" ListValidator, RegexValidator\n",
14-
"from easydiffraction.core.parameters import DescriptorStr, Parameter\n",
15-
"from easydiffraction.crystallography.cif import CifHandler\n",
16-
"from easydiffraction.utils.logging import log # type: ignore\n",
17-
"from easydiffraction.sample_models.components.cell import Cell # type: ignore\n",
18-
"from easydiffraction.sample_models.components.space_group import SpaceGroup # type: ignore\n",
19-
"from easydiffraction.sample_models.collections.atom_sites import AtomSite, AtomSites # type: ignore\n",
20-
"from easydiffraction.sample_models.sample_model import BaseSampleModel, SampleModel\n",
21-
"from easydiffraction.sample_models.sample_models import SampleModels\n",
22-
"from easydiffraction.analysis.collections.constraints import Constraint\n",
23-
"from easydiffraction.analysis.collections.constraints import Constraints"
10+
"from easydiffraction.sample_models.components.cell import Cell\n",
11+
"from easydiffraction.sample_models.components.space_group import SpaceGroup\n",
12+
"from easydiffraction.sample_models.collections.atom_sites import AtomSite, AtomSites\n",
13+
"from easydiffraction.sample_models.sample_model import SampleModel\n",
14+
"from easydiffraction.sample_models.sample_models import SampleModels"
2415
]
2516
},
2617
{
2718
"cell_type": "code",
28-
"execution_count": 56,
19+
"execution_count": 2,
2920
"id": "1100c5b2-e00c-4513-bd2e-e30742d47e67",
3021
"metadata": {},
3122
"outputs": [],
3223
"source": [
3324
"from easydiffraction.utils.logging import Logger\n",
3425
"\n",
3526
"Logger.configure(\n",
36-
" level=Logger.Level.INFO,\n",
27+
" level=Logger.Level.DEBUG,\n",
3728
" mode=Logger.Mode.COMPACT,\n",
3829
" reaction=Logger.Reaction.WARN,\n",
3930
")"
4031
]
4132
},
4233
{
4334
"cell_type": "code",
44-
"execution_count": 57,
35+
"execution_count": 3,
4536
"id": "a1a30af4-91c9-4015-9c96-a571fd1a711a",
4637
"metadata": {
4738
"scrolled": true
4839
},
49-
"outputs": [],
40+
"outputs": [
41+
{
42+
"name": "stdout",
43+
"output_type": "stream",
44+
"text": [
45+
"\u001b[32mDEBUG \u001b[0m Setting \u001b[1m<\u001b[0m\u001b[1;95mlabel\u001b[0m\u001b[1m>\u001b[0m to validated \u001b[32m'La'\u001b[0m. \n",
46+
"\u001b[32mDEBUG \u001b[0m Setting \u001b[1m<\u001b[0m\u001b[1;95mtype_symbol\u001b[0m\u001b[1m>\u001b[0m to validated \u001b[32m'La'\u001b[0m. \n",
47+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mfract_x\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
48+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mfract_y\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
49+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mfract_z\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
50+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mwyckoff_letter\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[32m'a'\u001b[0m. \n",
51+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95moccupancy\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m1.0\u001b[0m. \n",
52+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mb_iso\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
53+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95madp_type\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[32m'Biso'\u001b[0m. \n"
54+
]
55+
}
56+
],
5057
"source": [
5158
"s1 = AtomSite(label='La', type_symbol='La')"
5259
]
5360
},
5461
{
5562
"cell_type": "code",
56-
"execution_count": 58,
63+
"execution_count": 4,
5764
"id": "5f8217f7-e8cf-4202-8369-ced7438657f2",
5865
"metadata": {},
59-
"outputs": [],
66+
"outputs": [
67+
{
68+
"name": "stdout",
69+
"output_type": "stream",
70+
"text": [
71+
"\u001b[32mDEBUG \u001b[0m Setting \u001b[1m<\u001b[0m\u001b[1;95matom_site.La.fract_x\u001b[0m\u001b[1m>\u001b[0m to validated \u001b[1;36m1.234\u001b[0m. \n"
72+
]
73+
}
74+
],
6075
"source": [
6176
"s1.fract_x.value = 1.234"
6277
]
6378
},
6479
{
6580
"cell_type": "code",
66-
"execution_count": 59,
81+
"execution_count": 5,
6782
"id": "4c9cf2fe-7f72-4b9d-a574-5eb8c40223c4",
6883
"metadata": {},
6984
"outputs": [
7085
{
7186
"name": "stdout",
7287
"output_type": "stream",
7388
"text": [
74-
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95matom_site.La.fract_x\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'xyz'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not float. \n"
89+
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95matom_site.La.fract_x\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'xyz'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not float. Keeping current \u001b[1;36m1.234\u001b[0m. \n"
7590
]
7691
}
7792
],
@@ -81,15 +96,15 @@
8196
},
8297
{
8398
"cell_type": "code",
84-
"execution_count": 60,
99+
"execution_count": 6,
85100
"id": "cba23ca5-a865-428b-b1c8-90e38787e593",
86101
"metadata": {},
87102
"outputs": [
88103
{
89104
"name": "stdout",
90105
"output_type": "stream",
91106
"text": [
92-
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95matom_site.La.fract_x\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'qwe'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not float. \n"
107+
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95matom_site.La.fract_x\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'qwe'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not float. Keeping current \u001b[1;36m1.234\u001b[0m. \n"
93108
]
94109
}
95110
],
@@ -99,15 +114,23 @@
99114
},
100115
{
101116
"cell_type": "code",
102-
"execution_count": 61,
117+
"execution_count": 7,
103118
"id": "3ba30971-177b-40e4-b477-e79a00341f87",
104119
"metadata": {},
105120
"outputs": [
106121
{
107122
"name": "stdout",
108123
"output_type": "stream",
109124
"text": [
110-
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95mfract_x\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'uuuu'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not float. \n"
125+
"\u001b[32mDEBUG \u001b[0m Setting \u001b[1m<\u001b[0m\u001b[1;95mlabel\u001b[0m\u001b[1m>\u001b[0m to validated \u001b[32m'Si'\u001b[0m. \n",
126+
"\u001b[32mDEBUG \u001b[0m Setting \u001b[1m<\u001b[0m\u001b[1;95mtype_symbol\u001b[0m\u001b[1m>\u001b[0m to validated \u001b[32m'Si'\u001b[0m. \n",
127+
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95mfract_x\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'uuuu'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not float. Using default \u001b[1;36m0.0\u001b[0m. \n",
128+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mfract_y\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
129+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mfract_z\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
130+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mwyckoff_letter\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[32m'a'\u001b[0m. \n",
131+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95moccupancy\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m1.0\u001b[0m. \n",
132+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mb_iso\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m0.0\u001b[0m. \n",
133+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95madp_type\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[32m'Biso'\u001b[0m. \n"
111134
]
112135
}
113136
],
@@ -117,7 +140,7 @@
117140
},
118141
{
119142
"cell_type": "code",
120-
"execution_count": 62,
143+
"execution_count": 8,
121144
"id": "992966e7-6bb7-4bc7-bbff-80acfea6fd2c",
122145
"metadata": {},
123146
"outputs": [],
@@ -127,15 +150,15 @@
127150
},
128151
{
129152
"cell_type": "code",
130-
"execution_count": 63,
153+
"execution_count": 9,
131154
"id": "50ef6ebd-097d-4df2-93dc-39243bdba6fd",
132155
"metadata": {},
133156
"outputs": [
134157
{
135158
"name": "stdout",
136159
"output_type": "stream",
137160
"text": [
138-
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95matom_site.Si.fract_x.free\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'abc'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not bool \n"
161+
"\u001b[33mWARNING \u001b[0m Type mismatch for \u001b[1m<\u001b[0m\u001b[1;95matom_site.Si.fract_x.free\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[32m'abc'\u001b[0m \u001b[1m(\u001b[0mstr\u001b[1m)\u001b[0m is not bool. \n"
139162
]
140163
}
141164
],
@@ -145,25 +168,43 @@
145168
},
146169
{
147170
"cell_type": "code",
148-
"execution_count": 64,
171+
"execution_count": 10,
149172
"id": "2c46e9ca-f68d-4b71-b783-6660f357322c",
150173
"metadata": {},
151-
"outputs": [],
174+
"outputs": [
175+
{
176+
"name": "stdout",
177+
"output_type": "stream",
178+
"text": [
179+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mlength_a\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m10.0\u001b[0m. \n",
180+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mlength_b\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m10.0\u001b[0m. \n",
181+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mlength_c\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m10.0\u001b[0m. \n",
182+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mangle_alpha\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m90.0\u001b[0m. \n",
183+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mangle_beta\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m90.0\u001b[0m. \n",
184+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mangle_gamma\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m90.0\u001b[0m. \n"
185+
]
186+
}
187+
],
152188
"source": [
153189
"c = Cell()"
154190
]
155191
},
156192
{
157193
"cell_type": "code",
158-
"execution_count": 65,
194+
"execution_count": 11,
159195
"id": "8e3fee6f-dc71-49a0-bf67-6f85f4ba83cd",
160196
"metadata": {},
161197
"outputs": [
162198
{
163199
"name": "stdout",
164200
"output_type": "stream",
165201
"text": [
166-
"\u001b[33mWARNING \u001b[0m Value mismatch for \u001b[1m<\u001b[0m\u001b[1;95mlength_b\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[1;36m-8.8\u001b[0m is outside of \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m1000\u001b[0m\u001b[1m]\u001b[0m. \n"
202+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mlength_a\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m10.0\u001b[0m. \n",
203+
"\u001b[33mWARNING \u001b[0m Value mismatch for \u001b[1m<\u001b[0m\u001b[1;95mlength_b\u001b[0m\u001b[1m>\u001b[0m. Provided \u001b[1;36m-8.8\u001b[0m is outside of \u001b[1m[\u001b[0m\u001b[1;36m0\u001b[0m, \u001b[1;36m1000\u001b[0m\u001b[1m]\u001b[0m. Using default \u001b[1;36m10.0\u001b[0m. \n",
204+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mlength_c\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m10.0\u001b[0m. \n",
205+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mangle_alpha\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m90.0\u001b[0m. \n",
206+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mangle_beta\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m90.0\u001b[0m. \n",
207+
"\u001b[32mDEBUG \u001b[0m No value provided for \u001b[1m<\u001b[0m\u001b[1;95mangle_gamma\u001b[0m\u001b[1m>\u001b[0m. Using default \u001b[1;36m90.0\u001b[0m. \n"
167208
]
168209
}
169210
],

0 commit comments

Comments
 (0)