more systematic naming
YAML is the base class for ConfigMaterial and LoadcaseGrid
This commit is contained in:
parent
ad60193502
commit
7bfb081106
2
PRIVATE
2
PRIVATE
|
@ -1 +1 @@
|
|||
Subproject commit bc2c8e3b8f405fdda0d69a2900d9b94c7cc0936f
|
||||
Subproject commit 1f36292500ca01e7164ac6195eba0dc3e952bafb
|
|
@ -23,7 +23,7 @@ from ._orientation import Orientation # noqa
|
|||
from ._table import Table # noqa
|
||||
from ._colormap import Colormap # noqa
|
||||
from ._vtk import VTK # noqa
|
||||
from ._config import Config # noqa
|
||||
from ._yaml import YAML # noqa
|
||||
from ._configmaterial import ConfigMaterial # noqa
|
||||
from ._loadcasegrid import LoadcaseGrid # noqa
|
||||
from ._geomgrid import GeomGrid # noqa
|
||||
|
|
|
@ -4,7 +4,7 @@ import numpy as np
|
|||
import h5py
|
||||
|
||||
from ._typehints import FileHandle, FloatSequence, StrSequence
|
||||
from . import Config
|
||||
from . import YAML
|
||||
from . import Rotation
|
||||
from . import Orientation
|
||||
from . import util
|
||||
|
@ -12,7 +12,7 @@ from . import tensor
|
|||
from . import Table
|
||||
|
||||
|
||||
class ConfigMaterial(Config):
|
||||
class ConfigMaterial(YAML):
|
||||
"""
|
||||
Material configuration.
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@ from numpy import ma
|
|||
import yaml
|
||||
|
||||
from ._typehints import FileHandle
|
||||
from ._config import NiceDumper
|
||||
from ._yaml import NiceDumper
|
||||
from . import util
|
||||
from . import Config
|
||||
from . import YAML
|
||||
|
||||
|
||||
class MaskedMatrixDumper(NiceDumper):
|
||||
|
@ -16,7 +16,7 @@ class MaskedMatrixDumper(NiceDumper):
|
|||
return super().represent_data(data.astype(object).filled('x') if isinstance(data, ma.core.MaskedArray) else data) # type: ignore[attr-defined]
|
||||
|
||||
|
||||
class LoadcaseGrid(Config):
|
||||
class LoadcaseGrid(YAML):
|
||||
"""Load case for grid solver."""
|
||||
|
||||
def __init__(self,
|
||||
|
|
|
@ -18,7 +18,7 @@ from ._typehints import FileHandle
|
|||
from . import Rotation
|
||||
from . import util
|
||||
|
||||
MyType = TypeVar('MyType', bound='Config')
|
||||
MyType = TypeVar('MyType', bound='YAML')
|
||||
|
||||
class NiceDumper(SafeDumper):
|
||||
"""Make YAML readable for humans."""
|
||||
|
@ -37,7 +37,7 @@ class NiceDumper(SafeDumper):
|
|||
|
||||
def represent_data(self,
|
||||
data: Any):
|
||||
"""Cast Config objects and its subclasses to dict."""
|
||||
"""Cast YAML objects and its subclasses to dict."""
|
||||
if isinstance(data, dict) and type(data) != dict:
|
||||
return self.represent_data(dict(data))
|
||||
if isinstance(data, np.ndarray):
|
||||
|
@ -54,7 +54,7 @@ class NiceDumper(SafeDumper):
|
|||
"""Do not use references to existing objects."""
|
||||
return True
|
||||
|
||||
class Config(dict):
|
||||
class YAML(dict):
|
||||
"""YAML-based configuration."""
|
||||
|
||||
def __init__(self,
|
||||
|
@ -66,7 +66,7 @@ class Config(dict):
|
|||
Parameters
|
||||
----------
|
||||
config : dict or str, optional
|
||||
Configuration. String needs to be valid YAML.
|
||||
YAML. String needs to be valid YAML.
|
||||
**kwargs: arbitray keyword-value pairs, optional
|
||||
Top level entries of the configuration.
|
||||
|
||||
|
@ -129,12 +129,12 @@ class Config(dict):
|
|||
|
||||
Parameters
|
||||
----------
|
||||
other : damask.Config or dict
|
||||
other : damask.YAML or dict
|
||||
Key-value pairs that update self.
|
||||
|
||||
Returns
|
||||
-------
|
||||
updated : damask.Config
|
||||
updated : damask.YAML
|
||||
Updated configuration.
|
||||
|
||||
Note
|
||||
|
@ -170,7 +170,7 @@ class Config(dict):
|
|||
|
||||
Returns
|
||||
-------
|
||||
updated : damask.Config
|
||||
updated : damask.YAML
|
||||
Updated configuration.
|
||||
|
||||
"""
|
||||
|
@ -193,8 +193,8 @@ class Config(dict):
|
|||
|
||||
Returns
|
||||
-------
|
||||
loaded : damask.Config
|
||||
Configuration from file.
|
||||
loaded : damask.YAML
|
||||
YAML from file.
|
||||
|
||||
"""
|
||||
return cls(yaml.load(util.open_text(fname), Loader=SafeLoader))
|
File diff suppressed because it is too large
Load Diff
|
@ -1,75 +1,75 @@
|
|||
import pytest
|
||||
import numpy as np
|
||||
|
||||
from damask import Config
|
||||
from damask import YAML
|
||||
from damask import Rotation
|
||||
from damask import Orientation
|
||||
|
||||
class TestConfig:
|
||||
class TestYAML:
|
||||
|
||||
def test_init_keyword(self):
|
||||
assert Config(p=4)['p'] == 4
|
||||
assert YAML(p=4)['p'] == 4
|
||||
|
||||
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
|
||||
def test_init_config(self,config):
|
||||
assert Config(config)['p'] == 1
|
||||
assert YAML(config)['p'] == 1
|
||||
|
||||
@pytest.mark.parametrize('config',[{'p':1},'{p: 1}'])
|
||||
def test_init_both(self,config):
|
||||
assert Config(config,p=2)['p'] == 2
|
||||
assert YAML(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()
|
||||
config = YAML()
|
||||
config['A'] = 1
|
||||
config['B'] = [2,3]
|
||||
config.save(tmp_path/'config.yaml',default_flow_style=flow_style)
|
||||
assert Config.load(tmp_path/'config.yaml') == config
|
||||
assert YAML.load(tmp_path/'config.yaml') == config
|
||||
|
||||
def test_load_save_file(self,tmp_path):
|
||||
config = Config()
|
||||
config = YAML()
|
||||
config['A'] = 1
|
||||
config['B'] = [2,3]
|
||||
with open(tmp_path/'config.yaml','w') as f:
|
||||
config.save(f)
|
||||
with open(tmp_path/'config.yaml') as f:
|
||||
assert Config.load(f) == config
|
||||
assert YAML.load(f) == config
|
||||
|
||||
def test_add_remove(self):
|
||||
dummy = {'hello':'world','foo':'bar'}
|
||||
config = Config()
|
||||
config = YAML()
|
||||
config |= dummy
|
||||
assert config == Config() | dummy
|
||||
assert config == YAML() | dummy
|
||||
config = config.delete(dummy)
|
||||
assert config == Config()
|
||||
assert config == YAML()
|
||||
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'}
|
||||
assert (config | YAML(dummy)).delete({ 'hello':1,'foo':2 }) == config
|
||||
assert (config | YAML(dummy)).delete(YAML({'hello':1 })) == config | {'foo':'bar'}
|
||||
|
||||
def test_repr(self,tmp_path):
|
||||
config = Config()
|
||||
config = YAML()
|
||||
config['A'] = 1
|
||||
config['B'] = [2,3]
|
||||
with open(tmp_path/'config.yaml','w') as f:
|
||||
f.write(config.__repr__())
|
||||
assert Config.load(tmp_path/'config.yaml') == config
|
||||
assert YAML.load(tmp_path/'config.yaml') == config
|
||||
|
||||
def test_numpy(self,tmp_path):
|
||||
assert Config({'A':np.ones(3,'i'), 'B':np.ones(1)[0]}).__repr__() == \
|
||||
Config({'A':[1,1,1], 'B':1.0}).__repr__()
|
||||
assert YAML({'A':np.ones(3,'i'), 'B':np.ones(1)[0]}).__repr__() == \
|
||||
YAML({'A':[1,1,1], 'B':1.0}).__repr__()
|
||||
|
||||
def test_abstract_is_valid(self):
|
||||
with pytest.raises(NotImplementedError):
|
||||
Config().is_valid
|
||||
YAML().is_valid
|
||||
|
||||
def test_abstract_is_complete(self):
|
||||
with pytest.raises(NotImplementedError):
|
||||
Config().is_complete
|
||||
YAML().is_complete
|
||||
|
||||
@pytest.mark.parametrize('data',[Rotation.from_random(),Orientation.from_random(lattice='cI')])
|
||||
def test_rotation_orientation(self,data):
|
||||
assert str(Config(a=data)) == str(Config(a=data.as_quaternion()))
|
||||
assert str(YAML(a=data)) == str(YAML(a=data.as_quaternion()))
|
||||
|
||||
def test_initialize(self):
|
||||
yml = """
|
||||
|
@ -77,4 +77,4 @@ class TestConfig:
|
|||
- 1
|
||||
- 2
|
||||
"""
|
||||
assert Config(yml) == Config('{"a":[1,2]}') == Config(a=[1,2]) == Config(dict(a=[1,2]))
|
||||
assert YAML(yml) == YAML('{"a":[1,2]}') == YAML(a=[1,2]) == YAML(dict(a=[1,2]))
|
Loading…
Reference in New Issue