From fe0cd8e5e2cd8dfb8ce00c3403998598b59e5b40 Mon Sep 17 00:00:00 2001 From: Martin Diehl Date: Thu, 25 Mar 2021 15:30:31 +0100 Subject: [PATCH] transparently handle orientations/rotations --- python/damask/_config.py | 11 +++++++++-- python/tests/test_Config.py | 6 ++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/python/damask/_config.py b/python/damask/_config.py index 85f0c208c..5be1d973c 100644 --- a/python/damask/_config.py +++ b/python/damask/_config.py @@ -6,6 +6,9 @@ import abc import numpy as np import yaml +from . import Rotation +from . import Orientation + class NiceDumper(yaml.SafeDumper): """Make YAML readable for humans.""" @@ -20,8 +23,12 @@ class NiceDumper(yaml.SafeDumper): def represent_data(self, data): """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) + if isinstance(data, dict) and type(data) != dict: + return self.represent_data(dict(data)) + if isinstance(data, (Rotation, Orientation)): + return self.represent_data(data.as_quaternion()) + else: + return super().represent_data(data) def ignore_aliases(self, data): """No references.""" diff --git a/python/tests/test_Config.py b/python/tests/test_Config.py index 9324c28c5..c64573d93 100644 --- a/python/tests/test_Config.py +++ b/python/tests/test_Config.py @@ -2,6 +2,8 @@ import pytest import numpy as np from damask import Config +from damask import Rotation +from damask import Orientation class TestConfig: @@ -51,3 +53,7 @@ class TestConfig: def test_abstract_is_complete(self): assert Config().is_complete is None + + @pytest.mark.parametrize('data',[Rotation.from_random(),Orientation.from_random()]) + def test_rotation_orientation(self,data): + assert str(Config(a=data)) == str(Config(a=data.as_quaternion()))