From 8d8f0eeba6674a877f179b2bdeeac76ea20ffa9c Mon Sep 17 00:00:00 2001 From: Sherjeel Shabih Date: Sat, 2 Dec 2023 12:17:42 +0100 Subject: [PATCH 1/2] Adds a get method to Dataconverter Template --- pynxtools/dataconverter/template.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pynxtools/dataconverter/template.py b/pynxtools/dataconverter/template.py index 28762ea17..a3fa1bedb 100644 --- a/pynxtools/dataconverter/template.py +++ b/pynxtools/dataconverter/template.py @@ -125,6 +125,13 @@ def __contains__(self, k): k in self.required ]) + def get(self, key: str, default=None): + """Proxies the get function to our internal __getitem__""" + try: + return self[key] + except KeyError: + return default + def __getitem__(self, k): """Handles how values are accessed from the Template object.""" # Try setting item in all else throw error. Does not append to default. From 295f72db96f0510935829a3b3813dfb240ce091c Mon Sep 17 00:00:00 2001 From: Sherjeel Shabih Date: Sat, 2 Dec 2023 15:25:40 +0100 Subject: [PATCH 2/2] Adds KeyError for not accepted keys in Template --- pynxtools/dataconverter/template.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pynxtools/dataconverter/template.py b/pynxtools/dataconverter/template.py index a3fa1bedb..fa6907d36 100644 --- a/pynxtools/dataconverter/template.py +++ b/pynxtools/dataconverter/template.py @@ -148,7 +148,10 @@ def __getitem__(self, k): return self.required[k] except KeyError: return self.undocumented[k] - return self.get_optionality(k) + if k in ("required", "optional", "recommended", "undocumented"): + return self.get_optionality(k) + raise KeyError("Only paths starting with '/' or one of [optional_parents, " + "lone_groups, required, optional, recommended, undocumented] can be used.") def clear(self): """Clears all data stored in the Template object."""