From 5f1399acc37529e5b2bfa3d12be666823259f23b Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Sun, 3 Jan 2021 12:09:21 +0100 Subject: [PATCH] consistent behavior with other classes python dictionary operates in-place, so wrappers for out-of-place behavior let it use like the other DAMASK classes --- python/damask/_config.py | 24 ++++++++++++++++++++++++ python/tests/test_Config.py | 4 ++++ 2 files changed, 28 insertions(+) diff --git a/python/damask/_config.py b/python/damask/_config.py index 91be5ebf1..c7e937656 100644 --- a/python/damask/_config.py +++ b/python/damask/_config.py @@ -99,6 +99,30 @@ class Config(dict): fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs)) + def add(self,d): + """ + Add dictionary. + + d : dict + Dictionary to append. + """ + duplicate = self.copy() + duplicate.update(d) + return duplicate + + + def delete(self,key): + """ + Delete item. + + key : dict + Label of the key to remove. + """ + duplicate = self.copy() + del duplicate[key] + return duplicate + + @property @abc.abstractmethod def is_complete(self): diff --git a/python/tests/test_Config.py b/python/tests/test_Config.py index 67c419b3e..0319fb6de 100644 --- a/python/tests/test_Config.py +++ b/python/tests/test_Config.py @@ -22,6 +22,10 @@ class TestConfig: with open(tmp_path/'config.yaml') as f: assert Config.load(f) == config + def test_add_remove(self): + config = Config() + assert config.add({'hello':'world'}).delete('hello') == config + def test_repr(self,tmp_path): config = Config() config['A'] = 1