Skip to content

Commit

Permalink
Merge pull request #190 from FAIRmat-NFDI/get-method-template
Browse files Browse the repository at this point in the history
Adds a get method to Dataconverter Template
  • Loading branch information
sherjeelshabih authored Dec 4, 2023
2 parents e7d5206 + d27e731 commit 8620c4e
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions pynxtools/dataconverter/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,26 +125,12 @@ def __contains__(self, k):
k in self.required
])

def get(self, key, return_value=None):
"""Implementing get method for template.
Parameters
----------
key : str
Template key
return_value : Any
return :
The value comes with return_value
"""
val = self.optional.get(key, None)
if val is None:
val = self.recommended.get(key, None)
if val is None:
val = self.required.get(key, None)
if val is None:
val = self.undocumented.get(key, None)
if val is None:
return return_value
return val
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."""
Expand All @@ -162,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."""
Expand Down

0 comments on commit 8620c4e

Please sign in to comment.