From c68f3b6b574805a59a4a6bb418035a7ac3220a67 Mon Sep 17 00:00:00 2001 From: Burathar Date: Sat, 25 Sep 2021 12:37:23 +0200 Subject: [PATCH] Simplify save method --- biscd/biscd/models/yaml_serializable.py | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/biscd/biscd/models/yaml_serializable.py b/biscd/biscd/models/yaml_serializable.py index 4494706..34cecbf 100644 --- a/biscd/biscd/models/yaml_serializable.py +++ b/biscd/biscd/models/yaml_serializable.py @@ -40,18 +40,14 @@ class YamlSerializable(RecursiveProperty): if self.name is None: raise TypeError("Name cannot be None") ymlsls = self._get_all_from_file() - found_match = False - for ymlsl in ymlsls: - if self.name == [*ymlsl][0]: - found_match = True - if overwrite: - ymlsl[self.name] = self.config_dict(values_only=True) - else: - raise ValueError( - f"A {type(self).__name__} with name {self.name} already exists!") - break - - if not found_match: + ymlsl = next((ymlsl for ymlsl in ymlsls if [*ymlsl][0] == self.name), None) + if ymlsl: + if overwrite: + ymlsl[self.name] = self.config_dict(values_only=True) + else: + raise ValueError( + f"A {type(self).__name__} with name {self.name} already exists!") + else: ymlsls.append(self.config_dict()) print(ymlsls) self._save_all_to_file(ymlsls)