Merge branch 'config-operator-overload' into 'development'
defined overloaded + (add) and - (delete) operators to modify Config See merge request damask/DAMASK!352
This commit is contained in:
commit
1aaa658e39
|
@ -1,5 +1,6 @@
|
||||||
import copy
|
import copy
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
from collections.abc import Iterable
|
||||||
import abc
|
import abc
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
@ -44,6 +45,42 @@ class Config(dict):
|
||||||
copy = __copy__
|
copy = __copy__
|
||||||
|
|
||||||
|
|
||||||
|
def __or__(self,other):
|
||||||
|
"""
|
||||||
|
Update configuration with contents of other.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
other : damask.Config or dict
|
||||||
|
Key-value pairs that update self.
|
||||||
|
|
||||||
|
"""
|
||||||
|
duplicate = self.copy()
|
||||||
|
duplicate.update(other)
|
||||||
|
return duplicate
|
||||||
|
|
||||||
|
|
||||||
|
def __ior__(self,other):
|
||||||
|
"""Update configuration with contents of other."""
|
||||||
|
return self.__or__(other)
|
||||||
|
|
||||||
|
|
||||||
|
def delete(self,keys):
|
||||||
|
"""
|
||||||
|
Remove configuration keys.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
keys : iterable or scalar
|
||||||
|
Label of the key(s) to remove.
|
||||||
|
|
||||||
|
"""
|
||||||
|
duplicate = self.copy()
|
||||||
|
for k in keys if isinstance(keys, Iterable) and not isinstance(keys, str) else [keys]:
|
||||||
|
del duplicate[k]
|
||||||
|
return duplicate
|
||||||
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def load(cls,fname):
|
def load(cls,fname):
|
||||||
"""
|
"""
|
||||||
|
@ -99,30 +136,6 @@ class Config(dict):
|
||||||
fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs))
|
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 : str or scalar
|
|
||||||
Label of the key to remove.
|
|
||||||
"""
|
|
||||||
duplicate = self.copy()
|
|
||||||
del duplicate[key]
|
|
||||||
return duplicate
|
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
@abc.abstractmethod
|
@abc.abstractmethod
|
||||||
def is_complete(self):
|
def is_complete(self):
|
||||||
|
|
|
@ -23,8 +23,17 @@ class TestConfig:
|
||||||
assert Config.load(f) == config
|
assert Config.load(f) == config
|
||||||
|
|
||||||
def test_add_remove(self):
|
def test_add_remove(self):
|
||||||
|
dummy = {'hello':'world','foo':'bar'}
|
||||||
config = Config()
|
config = Config()
|
||||||
assert config.add({'hello':'world'}).delete('hello') == config
|
config |= dummy
|
||||||
|
assert config == Config() | dummy
|
||||||
|
config = config.delete(dummy)
|
||||||
|
assert config == Config()
|
||||||
|
assert (config | dummy ).delete( 'hello' ) == config | {'foo':'bar'}
|
||||||
|
assert (config | dummy ).delete([ 'hello', 'foo' ]) == config
|
||||||
|
assert (config | Config(dummy)).delete({ 'hello':1,'foo':2 }) == config
|
||||||
|
assert (config | Config(dummy)).delete(Config({'hello':1 })) == config | {'foo':'bar'}
|
||||||
|
|
||||||
|
|
||||||
def test_repr(self,tmp_path):
|
def test_repr(self,tmp_path):
|
||||||
config = Config()
|
config = Config()
|
||||||
|
|
Loading…
Reference in New Issue