From f11b37342df5ea08df79a94e7150da61c26f7f63 Mon Sep 17 00:00:00 2001 From: Hugo Buddelmeijer Date: Fri, 14 Jul 2023 13:01:51 +0200 Subject: [PATCH 1/3] Improve printing of UserCommands --- scopesim/commands/user_commands.py | 3 +++ scopesim/tests/tests_commands/test_UserCommands.py | 13 +++++++++++++ 2 files changed, 16 insertions(+) diff --git a/scopesim/commands/user_commands.py b/scopesim/commands/user_commands.py index 793c3bbf..dbd424bd 100644 --- a/scopesim/commands/user_commands.py +++ b/scopesim/commands/user_commands.py @@ -265,6 +265,9 @@ def __contains__(self, item): def __repr__(self): return f"{self.__class__.__name__}(**{self.kwargs!r})" + def __str__(self): + return str(self.cmds) + def check_for_updates(package_name): """ diff --git a/scopesim/tests/tests_commands/test_UserCommands.py b/scopesim/tests/tests_commands/test_UserCommands.py index aeeaafa6..5f90bfe5 100644 --- a/scopesim/tests/tests_commands/test_UserCommands.py +++ b/scopesim/tests/tests_commands/test_UserCommands.py @@ -87,6 +87,14 @@ def test_mode_yamls_read_from_file(self): assert cmd["!OBS.airmass"] == 2 assert cmd.yaml_dicts[-1]["effects"][0]["kwargs"]["meaning_of_life"] == 42 + def test_init_through_repr(self): + """Check whether we can recreate a UserCommand by evaluating its __repr__.""" + cmd1 = UserCommands(use_instrument="test_package") + cmd2 = eval(repr(cmd1)) + # TODO: Create a proper __eq__ so we can assert cmd1 == cmd2 + assert str(cmd1) == str(cmd2) + assert cmd1.cmds == cmd2.cmds + class TestMiscFeatures: def test_updates_with_yaml_dict(self): @@ -101,6 +109,11 @@ def test_update_works_via_setitem(self): cmd["!TEL.gigawatts"] = 1.21 assert cmd["!TEL.gigawatts"] == 1.21 + def test_str(self): + """Test whether __str__ gives a pretty result.""" + cmd = UserCommands(use_instrument="test_package") + assert "├─" in str(cmd) + class TestListLocalPackages: def test_all_packages_listed(self): From 199eda806ff298618ac9ce81523b6c9f8ab21a4b Mon Sep 17 00:00:00 2001 From: oczoske Date: Fri, 14 Jul 2023 13:38:54 +0200 Subject: [PATCH 2/3] add _repr_pretty_ for ipython --- scopesim/commands/user_commands.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scopesim/commands/user_commands.py b/scopesim/commands/user_commands.py index dbd424bd..8ae4ece2 100644 --- a/scopesim/commands/user_commands.py +++ b/scopesim/commands/user_commands.py @@ -268,6 +268,12 @@ def __repr__(self): def __str__(self): return str(self.cmds) + def _repr_pretty_(self, p, cycle): + """For ipython""" + if cycle: + p.text("UserCommands(...)") + else: + p.text(str(self.cmds)) def check_for_updates(package_name): """ From 87591c6206fcdebad5b8dbe05b901f146f7b21b4 Mon Sep 17 00:00:00 2001 From: oczoske Date: Fri, 14 Jul 2023 14:02:59 +0200 Subject: [PATCH 3/3] Update scopesim/commands/user_commands.py Co-authored-by: Hugo Buddelmeijer --- scopesim/commands/user_commands.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scopesim/commands/user_commands.py b/scopesim/commands/user_commands.py index 8ae4ece2..02cbfda0 100644 --- a/scopesim/commands/user_commands.py +++ b/scopesim/commands/user_commands.py @@ -273,7 +273,7 @@ def _repr_pretty_(self, p, cycle): if cycle: p.text("UserCommands(...)") else: - p.text(str(self.cmds)) + p.text(str(self)) def check_for_updates(package_name): """