diff --git a/src/datastore.py b/src/datastore.py index 92623baf7c01f8199f653b7220db77a931986708..69e486796cc4320d88b3c3f5fd4a970d4ee814e9 100644 --- a/src/datastore.py +++ b/src/datastore.py @@ -144,6 +144,22 @@ class DataStoreByVariable(AbstractDataStore): """ 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): if depth <= scope.count("."): local_scope = scope.rsplit(".", maxsplit=depth)[0] @@ -267,6 +283,22 @@ class DataStoreByScope(AbstractDataStore): """ 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): if depth <= scope.count("."): local_scope = scope.rsplit(".", maxsplit=depth)[0]