Skip to content
Snippets Groups Projects
Commit 3e7939da authored by lukas leufen's avatar lukas leufen
Browse files

added get_default method

parent 2970085f
No related branches found
No related tags found
2 merge requests!37include new development,!30Lukas issue037 feat run without training
Pipeline #29018 passed
...@@ -144,6 +144,22 @@ class DataStoreByVariable(AbstractDataStore): ...@@ -144,6 +144,22 @@ class DataStoreByVariable(AbstractDataStore):
""" """
return self._stride_through_scopes(name, scope)[2] return self._stride_through_scopes(name, scope)[2]
def get_default(self, name: str, scope: str, default: Any) -> Any:
"""
Same functionality like the standard get method. But this method adds a default argument that is returned if no
data was stored in the data store. Use this function with care, because it will not report any errors and just
return the given default value. Currently, there is no statement that reports, if the returned value comes from
the data store or the default value.
:param name: Name to look for
:param scope: scope to search the name for
:param default: default value that is return, if no data was found for given name and scope
:return: the stored object or the default value
"""
try:
return self._stride_through_scopes(name, scope)[2]
except (NameNotFoundInDataStore, NameNotFoundInScope):
return default
def _stride_through_scopes(self, name, scope, depth=0): def _stride_through_scopes(self, name, scope, depth=0):
if depth <= scope.count("."): if depth <= scope.count("."):
local_scope = scope.rsplit(".", maxsplit=depth)[0] local_scope = scope.rsplit(".", maxsplit=depth)[0]
...@@ -267,6 +283,22 @@ class DataStoreByScope(AbstractDataStore): ...@@ -267,6 +283,22 @@ class DataStoreByScope(AbstractDataStore):
""" """
return self._stride_through_scopes(name, scope)[2] return self._stride_through_scopes(name, scope)[2]
def get_default(self, name: str, scope: str, default: Any) -> Any:
"""
Same functionality like the standard get method. But this method adds a default argument that is returned if no
data was stored in the data store. Use this function with care, because it will not report any errors and just
return the given default value. Currently, there is no statement that reports, if the returned value comes from
the data store or the default value.
:param name: Name to look for
:param scope: scope to search the name for
:param default: default value that is return, if no data was found for given name and scope
:return: the stored object or the default value
"""
try:
return self._stride_through_scopes(name, scope)[2]
except (NameNotFoundInDataStore, NameNotFoundInScope):
return default
def _stride_through_scopes(self, name, scope, depth=0): def _stride_through_scopes(self, name, scope, depth=0):
if depth <= scope.count("."): if depth <= scope.count("."):
local_scope = scope.rsplit(".", maxsplit=depth)[0] local_scope = scope.rsplit(".", maxsplit=depth)[0]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment