From 358cf46dfcb09061e6781415c1c7c1b7a16abd99 Mon Sep 17 00:00:00 2001 From: Ninad Bhat Date: Sat, 20 Mar 2021 19:35:12 +1100 Subject: [PATCH 1/2] Add __str__ to Orbital class --- aiida/tools/data/orbital/orbital.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/aiida/tools/data/orbital/orbital.py b/aiida/tools/data/orbital/orbital.py index d08c603160..e3c0f7a915 100644 --- a/aiida/tools/data/orbital/orbital.py +++ b/aiida/tools/data/orbital/orbital.py @@ -112,6 +112,14 @@ def __init__(self, **kwargs): def __repr__(self): return f'<{self.__class__.__name__}: {str(self)}>' + def __str__(self) -> str: + orb_dict = self.get_orbital_dict() + + position_string = f"{orb_dict['position'][0]:.4f},{orb_dict['position'][1]:.4f},{orb_dict['position'][2]:.4f}" + out_string = f' orbital @ {position_string}' + + return out_string + def _validate_keys(self, input_dict): """ Checks all the input_dict and tries to validate them, to ensure From 5bf01ae3ab4e43f2f71b43596705fba400ddcf09 Mon Sep 17 00:00:00 2001 From: Ninad Bhat Date: Tue, 23 Mar 2021 11:56:06 +1100 Subject: [PATCH 2/2] Adding test --- aiida/tools/data/orbital/orbital.py | 4 +--- tests/tools/data/orbital/test_orbitals.py | 7 +++++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/aiida/tools/data/orbital/orbital.py b/aiida/tools/data/orbital/orbital.py index e3c0f7a915..a1ecabb6e1 100644 --- a/aiida/tools/data/orbital/orbital.py +++ b/aiida/tools/data/orbital/orbital.py @@ -116,9 +116,7 @@ def __str__(self) -> str: orb_dict = self.get_orbital_dict() position_string = f"{orb_dict['position'][0]:.4f},{orb_dict['position'][1]:.4f},{orb_dict['position'][2]:.4f}" - out_string = f' orbital @ {position_string}' - - return out_string + return f'Orbital @ {position_string}' def _validate_keys(self, input_dict): """ diff --git a/tests/tools/data/orbital/test_orbitals.py b/tests/tools/data/orbital/test_orbitals.py index 244484d331..9e59fee8be 100644 --- a/tests/tools/data/orbital/test_orbitals.py +++ b/tests/tools/data/orbital/test_orbitals.py @@ -20,6 +20,13 @@ class TestOrbital(AiidaTestCase): """Test the Orbital base class""" + def test_orbital_str(self): + """"Test the output of __str__ method""" + orbital = Orbital(position=(1, 2, 3)) + expected_output = 'Orbital @ 1.0000,2.0000,3.0000' + + self.assertEqual(str(orbital), expected_output) + def test_required_fields(self): """Verify that required fields are validated.""" # position is required