Skip to content

Commit b998f08

Browse files
committed
small refactor of code
1 parent 615ce5b commit b998f08

File tree

1 file changed

+20
-20
lines changed

1 file changed

+20
-20
lines changed

dargs/dargs.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ def _check_strict(self, value: dict, path=None):
305305
if name not in allowed_keys:
306306
raise ArgumentKeyError(path,
307307
f"undefined key `{name}` is "
308-
"not allowed in strict mode")
308+
"not allowed in strict mode")
309309

310310
# above are type checking part
311311
# below are normalizing part
@@ -323,9 +323,9 @@ def normalize(self, argdict: dict, inplace: bool = False,
323323
self.traverse(argdict,
324324
key_hook=Argument._assign_default)
325325
if trim_pattern is not None:
326-
self._trim_unrequired(argdict, trim_pattern, reserved=[self.name])
326+
trim_by_pattern(argdict, trim_pattern, reserved=[self.name])
327327
self.traverse(argdict, sub_hook=lambda a, d, p:
328-
Argument._trim_unrequired(d, trim_pattern, a.flatten_sub(d, p).keys()))
328+
trim_by_pattern(d, trim_pattern, a.flatten_sub(d, p).keys()))
329329
return argdict
330330

331331
def normalize_value(self, value: Any, inplace: bool = False,
@@ -342,7 +342,7 @@ def normalize_value(self, value: Any, inplace: bool = False,
342342
key_hook=Argument._assign_default)
343343
if trim_pattern is not None:
344344
self.traverse_value(value, sub_hook=lambda a, d, p:
345-
Argument._trim_unrequired(d, trim_pattern, a.flatten_sub(d, p).keys()))
345+
trim_by_pattern(d, trim_pattern, a.flatten_sub(d, p).keys()))
346346
return value
347347

348348
def _assign_default(self, argdict: dict, path=None):
@@ -358,21 +358,6 @@ def _convert_alias(self, argdict: dict, path=None):
358358
argdict[self.name] = argdict.pop(alias)
359359
return
360360

361-
@staticmethod
362-
def _trim_unrequired(argdict: dict, pattern: str,
363-
reserved: Optional[List[str]] = None,
364-
use_regex: bool = False):
365-
rep = fnmatch.translate(pattern) if not use_regex else pattern
366-
rem = re.compile(rep)
367-
if reserved:
368-
conflict = list(filter(rem.match, reserved))
369-
if conflict:
370-
raise ValueError(f"pattern `{pattern}` conflicts with the "
371-
f"following reserved names: {', '.join(conflict)}")
372-
unrequired = list(filter(rem.match, argdict.keys()))
373-
for key in unrequired:
374-
argdict.pop(key)
375-
376361
# above are normalizing part
377362
# below are doc generation part
378363

@@ -632,4 +617,19 @@ def update_nodup(this : dict,
632617
raise ValueError(f"duplicate key `{k}` when updating dict"
633618
+("" if err_msg is None else f"in {err_msg}"))
634619
this[k] = v
635-
return this
620+
return this
621+
622+
623+
def trim_by_pattern(argdict: dict, pattern: str,
624+
reserved: Optional[List[str]] = None,
625+
use_regex: bool = False):
626+
rep = fnmatch.translate(pattern) if not use_regex else pattern
627+
rem = re.compile(rep)
628+
if reserved:
629+
conflict = list(filter(rem.match, reserved))
630+
if conflict:
631+
raise ValueError(f"pattern `{pattern}` conflicts with the "
632+
f"following reserved names: {', '.join(conflict)}")
633+
unrequired = list(filter(rem.match, argdict.keys()))
634+
for key in unrequired:
635+
argdict.pop(key)

0 commit comments

Comments
 (0)