From 33685bc493b6e80b559f602323623205e3cf704a Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Wed, 30 Sep 2020 12:32:37 +0200 Subject: [PATCH] allow user do control output style (numerics.yaml looks strange otherwise) --- python/damask/_config.py | 11 +++++++++-- python/damask/_configmaterial.py | 6 ++++-- python/tests/test_Config.py | 7 +++++-- 3 files changed, 18 insertions(+), 6 deletions(-) diff --git a/python/damask/_config.py b/python/damask/_config.py index 7b9d950a7..c9130d7aa 100644 --- a/python/damask/_config.py +++ b/python/damask/_config.py @@ -43,7 +43,7 @@ class Config(dict): fhandle = fname return cls(yaml.safe_load(fhandle)) - def save(self,fname): + def save(self,fname,**kwargs): """ Save to yaml file. @@ -51,13 +51,20 @@ class Config(dict): ---------- fname : file, str, or pathlib.Path Filename or file for writing. + **kwargs : dict + Keyword arguments parsed to yaml.dump. """ try: fhandle = open(fname,'w') except TypeError: fhandle = fname - fhandle.write(yaml.dump(dict(self),width=256,default_flow_style=None,Dumper=NiceDumper)) + + if 'width' not in kwargs: + kwargs['width'] = 256 + if 'default_flow_style' not in kwargs: + kwargs['default_flow_style'] = None + fhandle.write(yaml.dump(dict(self),Dumper=NiceDumper,**kwargs)) @property diff --git a/python/damask/_configmaterial.py b/python/damask/_configmaterial.py index 8b6739763..3eb7180e2 100644 --- a/python/damask/_configmaterial.py +++ b/python/damask/_configmaterial.py @@ -9,7 +9,7 @@ from . import Rotation class ConfigMaterial(Config): """Material configuration.""" - def save(self,fname='material.yaml'): + def save(self,fname='material.yaml',**kwargs): """ Save to yaml file. @@ -17,9 +17,11 @@ class ConfigMaterial(Config): ---------- fname : file, str, or pathlib.Path, optional Filename or file for writing. Defaults to 'material.yaml'. + **kwargs : dict + Keyword arguments parsed to yaml.dump. """ - super().save(fname) + super().save(fname,**kwargs) @property def is_complete(self): diff --git a/python/tests/test_Config.py b/python/tests/test_Config.py index e4d05cffb..e715ad763 100644 --- a/python/tests/test_Config.py +++ b/python/tests/test_Config.py @@ -1,12 +1,15 @@ +import pytest + from damask import Config class TestConfig: - def test_load_save_str(self,tmp_path): + @pytest.mark.parametrize('flow_style',[None,True,False]) + def test_load_save_str(self,tmp_path,flow_style): config = Config() config['A'] = 1 config['B'] = [2,3] - config.save(tmp_path/'config.yaml') + config.save(tmp_path/'config.yaml',default_flow_style=flow_style) assert Config.load(tmp_path/'config.yaml') == config def test_load_save_file(self,tmp_path):