[+] SafeNamespace

This commit is contained in:
2023-07-28 20:49:51 -07:00
parent 332a63479e
commit 25aecabd34
+10 -2
View File
@@ -119,8 +119,16 @@ def json_stringify(obj: object, forced: bool = True, **kwargs) -> str:
return json.dumps(obj, **args)
def jsn(s: str) -> SimpleNamespace:
return json.loads(s, object_hook=lambda d: SimpleNamespace(**d))
class SafeNamespace(SimpleNamespace):
def __getattr__(self, attr):
try:
return super().__getattr__(attr)
except AttributeError:
return None
def jsn(s: str) -> SafeNamespace:
return json.loads(s, object_hook=lambda d: SafeNamespace(**d))
def ensure_dir(path: Path | str) -> Path: