Skip to content

Commit

Permalink
Merge pull request #252 from AstarVienna/hb/printcmds
Browse files Browse the repository at this point in the history
Improve printing of UserCommands
  • Loading branch information
oczoske committed Jul 14, 2023
2 parents f03211c + 87591c6 commit c6ca4d1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
9 changes: 9 additions & 0 deletions scopesim/commands/user_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,15 @@ def __contains__(self, item):
def __repr__(self):
return f"{self.__class__.__name__}(**{self.kwargs!r})"

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))

def check_for_updates(package_name):
"""
Expand Down
13 changes: 13 additions & 0 deletions scopesim/tests/tests_commands/test_UserCommands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down

0 comments on commit c6ca4d1

Please sign in to comment.