From cd3fc13301faadd3c4d68848fcbc442a52dcef9b Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Thu, 12 Sep 2024 19:12:59 +0100 Subject: [PATCH 1/2] Add style parameter to `print_known_signal_types` --- doc/conf.py | 1 + .../tests/utils/test_print_known_signal_types.py | 13 +++++++++++++ hyperspy/utils/__init__.py | 10 +++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/doc/conf.py b/doc/conf.py index 523d33034c..337746bf26 100644 --- a/doc/conf.py +++ b/doc/conf.py @@ -404,6 +404,7 @@ "widget", "strategy", "module", + "prettytable", } # if Version(numpydoc.__version__) >= Version("1.6.0rc0"): diff --git a/hyperspy/tests/utils/test_print_known_signal_types.py b/hyperspy/tests/utils/test_print_known_signal_types.py index d75558d7aa..c7cc276a6b 100644 --- a/hyperspy/tests/utils/test_print_known_signal_types.py +++ b/hyperspy/tests/utils/test_print_known_signal_types.py @@ -1,3 +1,5 @@ +from prettytable import MARKDOWN + from hyperspy.utils import print_known_signal_types @@ -7,3 +9,14 @@ def test_text_output(capsys): assert "signal_type" in captured.out # the output will be str, not html assert "

" not in captured.out + + +def test_style(capsys): + print_known_signal_types(style=MARKDOWN) + captured = capsys.readouterr() + + assert "signal_type" in captured.out + # the output will be markdown, not ascii + assert ":--" in captured.out # markdown + assert "

" not in captured.out # not html + assert "+--" not in captured.out # not ascii diff --git a/hyperspy/utils/__init__.py b/hyperspy/utils/__init__.py index 75dcc942cd..b4ac51f241 100644 --- a/hyperspy/utils/__init__.py +++ b/hyperspy/utils/__init__.py @@ -37,12 +37,17 @@ import importlib -def print_known_signal_types(): +def print_known_signal_types(style=None): r"""Print all known `signal_type`\s This includes `signal_type`\s from all installed packages that extend HyperSpy. + Parameters + ---------- + style : prettytable style or None + If None, the default prettytable style will be used. + Examples -------- >>> hs.print_known_signal_types() # doctest: +SKIP @@ -65,6 +70,8 @@ def print_known_signal_types(): table = PrettyTable() table.field_names = ["signal_type", "aliases", "class name", "package"] + if style is not None: + table.set_style(style) for sclass, sdict in ALL_EXTENSIONS["signals"].items(): # skip lazy signals and non-data-type specific signals if sdict["lazy"] or not sdict["signal_type"]: @@ -77,6 +84,7 @@ def print_known_signal_types(): package = sdict["module"].split(".")[0] table.add_row([sdict["signal_type"], aliases, sclass, package]) table.sortby = "class name" + display(table) From d0ee956d3c2bd9f3cb6c3594ccd65652e2469dd6 Mon Sep 17 00:00:00 2001 From: Eric Prestat Date: Thu, 12 Sep 2024 20:22:47 +0100 Subject: [PATCH 2/2] Add changelog entry --- upcoming_changes/3439.enhancements.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 upcoming_changes/3439.enhancements.rst diff --git a/upcoming_changes/3439.enhancements.rst b/upcoming_changes/3439.enhancements.rst new file mode 100644 index 0000000000..4e30e0946e --- /dev/null +++ b/upcoming_changes/3439.enhancements.rst @@ -0,0 +1 @@ +Add ``style`` parameter to the :func:`~.api.print_known_signal_types` function. \ No newline at end of file