From 7d0ad0503a04a81f7204ab51ede99aae0ee0c7b7 Mon Sep 17 00:00:00 2001 From: Camille Troillard Date: Wed, 2 Oct 2024 13:52:23 +0200 Subject: [PATCH] feat: Add a convenient way of outputting system and dependency versions (#425) --- src/skore/__init__.py | 5 +- src/skore/utils/__init__.py | 1 + src/skore/utils/_show_versions.py | 84 +++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/skore/utils/__init__.py create mode 100644 src/skore/utils/_show_versions.py diff --git a/src/skore/__init__.py b/src/skore/__init__.py index 3b01c1474..3315055f6 100644 --- a/src/skore/__init__.py +++ b/src/skore/__init__.py @@ -1,4 +1,4 @@ -"""Provide the Store class for managing information in a data science project.""" +"""Configure logging and global settings.""" import logging @@ -6,8 +6,11 @@ from skore.project import Project, load +from .utils._show_versions import show_versions + __all__ = [ "load", + "show_versions", "Project", ] diff --git a/src/skore/utils/__init__.py b/src/skore/utils/__init__.py new file mode 100644 index 000000000..3ee290a4d --- /dev/null +++ b/src/skore/utils/__init__.py @@ -0,0 +1 @@ +"""Various utilities to help with development.""" diff --git a/src/skore/utils/_show_versions.py b/src/skore/utils/_show_versions.py new file mode 100644 index 000000000..c554f4164 --- /dev/null +++ b/src/skore/utils/_show_versions.py @@ -0,0 +1,84 @@ +""" +Utility methods to print system info for debugging. + +adapted from :func:`sklearn.show_versions` +""" + +import platform +import sys + + +def _get_sys_info(): + """System information. + + Returns + ------- + sys_info : dict + system and Python version information + + """ + python = sys.version.replace("\n", " ") + + blob = [ + ("python", python), + ("executable", sys.executable), + ("machine", platform.platform()), + ] + + return dict(blob) + + +def _get_deps_info(): + """Overview of the installed version of main dependencies. + + This function does not import the modules to collect the version numbers + but instead relies on standard Python package metadata. + + Returns + ------- + deps_info: dict + version information on relevant Python libraries + + """ + deps = [ + "pip", + "setuptools", + "diskcache", + "fastapi", + "rich", + "skops", + "uvicorn", + ] + + from importlib.metadata import PackageNotFoundError, version + + deps_info = { + "skore": version("skore"), + } + + for modname in deps: + try: + deps_info[modname] = version(modname) + except PackageNotFoundError: + deps_info[modname] = None + return deps_info + + +def show_versions(): + """Print useful debugging information. + + Examples + -------- + >>> from skore import show_versions + >>> show_versions() + """ + sys_info = _get_sys_info() + deps_info = _get_deps_info() + + print("\nSystem:") # noqa: T201 + for k, stat in sys_info.items(): + print(f"{k:>10}: {stat}") # noqa: T201 + + print("\nPython dependencies:") # noqa: T201 + for k, stat in deps_info.items(): + print(f"{k:>13}: {stat}") # noqa: T201