more logical behavior

explicit keywords are more important than existing one
This commit is contained in:
Martin Diehl 2023-02-01 14:14:54 +01:00
parent 7334772f74
commit 8895e7a36f
2 changed files with 35 additions and 7 deletions

View File

@ -2,6 +2,7 @@ import copy
from io import StringIO
from collections.abc import Iterable
import abc
import platform
from typing import Optional, Union, Dict, Any, Type, TypeVar
import numpy as np
@ -69,13 +70,30 @@ class Config(dict):
**kwargs: arbitray keyword-value pairs, optional
Top level entries of the configuration.
Notes
-----
Values given as keyword-value pairs take precedence
over entries with the same keyword in 'config'.
"""
if int(platform.python_version_tuple()[1]) >= 9:
if isinstance(config,str):
kwargs.update(yaml.load(config, Loader=SafeLoader))
kwargs = yaml.load(config, Loader=SafeLoader) | kwargs
elif isinstance(config,dict):
kwargs.update(config)
kwargs = config | kwargs # type: ignore
super().__init__(**kwargs)
else:
if isinstance(config,str):
c = yaml.load(config, Loader=SafeLoader)
elif isinstance(config,dict):
c = config.copy()
else:
c = {}
c.update(kwargs)
super().__init__(**c)
def __repr__(self) -> str:
"""

View File

@ -7,6 +7,17 @@ from damask import Orientation
class TestConfig:
def test_init_keyword(self):
assert Config(p=4)['p'] == 4
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
def test_init_config(self,config):
assert Config(config)['p'] == 1
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
def test_init_both(self,config):
assert Config(config,p=2)['p'] == 2
@pytest.mark.parametrize('flow_style',[None,True,False])
def test_load_save_str(self,tmp_path,flow_style):
config = Config()
@ -36,7 +47,6 @@ class TestConfig:
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):
config = Config()
config['A'] = 1