From 5834d9501231f877ada4c065b5e43ff1aa68cae7 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 26 Oct 2020 21:38:24 +0100 Subject: [PATCH 1/3] support for more types - allow to directly use Config and its sublasses (cast to dict) - convert numpy arrays --- python/damask/_config.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/python/damask/_config.py b/python/damask/_config.py index c9130d7aa..40b9e918f 100644 --- a/python/damask/_config.py +++ b/python/damask/_config.py @@ -1,6 +1,7 @@ from io import StringIO import abc +import numpy as np import yaml class NiceDumper(yaml.SafeDumper): @@ -15,6 +16,11 @@ class NiceDumper(yaml.SafeDumper): def increase_indent(self, flow=False, indentless=False): return super().increase_indent(flow, False) + def represent_data(self, data): + """Cast Config objects and its qsubclasses to dict.""" + return self.represent_data(dict(data)) if isinstance(data, dict) and type(data) != dict else \ + super().represent_data(data) + class Config(dict): """YAML-based configuration.""" @@ -64,7 +70,14 @@ class Config(dict): kwargs['width'] = 256 if 'default_flow_style' not in kwargs: kwargs['default_flow_style'] = None - fhandle.write(yaml.dump(dict(self),Dumper=NiceDumper,**kwargs)) + + def array_representer(dumper, data): + """Convert numpy array to list of native types.""" + return dumper.represent_list([d.item() for d in data]) + + NiceDumper.add_representer(np.ndarray, array_representer) + + fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs)) @property From 4159ae5f3d6ffad63fb71b98c79d385953681523 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Mon, 26 Oct 2020 21:43:21 +0100 Subject: [PATCH 2/3] keep order of keys --- python/damask/_config.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/damask/_config.py b/python/damask/_config.py index 40b9e918f..f2ffbbbf3 100644 --- a/python/damask/_config.py +++ b/python/damask/_config.py @@ -17,7 +17,7 @@ class NiceDumper(yaml.SafeDumper): return super().increase_indent(flow, False) def represent_data(self, data): - """Cast Config objects and its qsubclasses to dict.""" + """Cast Config objects and its subclasses to dict.""" return self.represent_data(dict(data)) if isinstance(data, dict) and type(data) != dict else \ super().represent_data(data) @@ -70,6 +70,8 @@ class Config(dict): kwargs['width'] = 256 if 'default_flow_style' not in kwargs: kwargs['default_flow_style'] = None + if 'sort_keys' not in kwargs: + kwargs['sort_keys'] = False def array_representer(dumper, data): """Convert numpy array to list of native types.""" From 4dd387d7f51c5b25535126f2c8733a3a91f25bec Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Tue, 27 Oct 2020 06:34:05 +0100 Subject: [PATCH 3/3] compatibility with old pyyaml + test for numpy --- python/damask/_config.py | 6 +++++- python/tests/test_Config.py | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/python/damask/_config.py b/python/damask/_config.py index f2ffbbbf3..24245f4bd 100644 --- a/python/damask/_config.py +++ b/python/damask/_config.py @@ -79,7 +79,11 @@ class Config(dict): NiceDumper.add_representer(np.ndarray, array_representer) - fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs)) + try: + fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs)) + except TypeError: # compatibility with old pyyaml + del kwargs['sort_keys'] + fhandle.write(yaml.dump(self,Dumper=NiceDumper,**kwargs)) @property diff --git a/python/tests/test_Config.py b/python/tests/test_Config.py index e715ad763..67c419b3e 100644 --- a/python/tests/test_Config.py +++ b/python/tests/test_Config.py @@ -1,4 +1,5 @@ import pytest +import numpy as np from damask import Config @@ -29,6 +30,8 @@ class TestConfig: f.write(config.__repr__()) assert Config.load(tmp_path/'config.yaml') == config + def test_numpy(self,tmp_path): + assert Config({'A':np.ones(3,'i')}).__repr__() == Config({'A':[1,1,1]}).__repr__() def test_abstract_is_valid(self): assert Config().is_valid is None