From 177545acfe6de01cc3116b53c936d486ee1d9a01 Mon Sep 17 00:00:00 2001 From: Yanick Fratantonio Date: Tue, 21 Jan 2025 16:37:43 +0000 Subject: [PATCH] python: implement str() and repr() for Magika object --- .github/workflows/python-build-package.yml | 2 +- .../python-test-published-rc-package.yml | 2 +- python/scripts/run_quick_test_magika_module.py | 2 +- python/src/magika/magika.py | 15 ++++++++++++--- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/.github/workflows/python-build-package.yml b/.github/workflows/python-build-package.yml index 4c2be841..e87d6fac 100644 --- a/.github/workflows/python-build-package.yml +++ b/.github/workflows/python-build-package.yml @@ -73,7 +73,7 @@ jobs: - name: Install wheels run: python3 -m pip install $(python -c "import glob; print(glob.glob('dist/*.whl')[0])") - run: magika --version - - run: "python3 -c 'import magika; m = magika.Magika(); print(f\"Package version: {magika.__version__}\"); print(f\"Model name: {m.get_model_dir_name()}\")'" + - run: "python3 -c 'import magika; m = magika.Magika(); print(m)'" - run: magika -r tests_data/basic - run: python3 ./python/scripts/run_quick_test_magika_cli.py # Windows' onnxruntime/ort returns different results wrt. Linux/MacOS. For diff --git a/.github/workflows/python-test-published-rc-package.yml b/.github/workflows/python-test-published-rc-package.yml index bbff1b31..63cf1afa 100644 --- a/.github/workflows/python-test-published-rc-package.yml +++ b/.github/workflows/python-test-published-rc-package.yml @@ -47,7 +47,7 @@ jobs: uv add --prerelease ".\$wheel" - name: Install magika with pip run: python3 -m pip install --pre magika - - run: python3 -c 'import magika; m = magika.Magika(); print(m); print(magika.__version__)' + - run: python3 -c 'import magika; m = magika.Magika(); print(m)' - run: magika --version # The latest published model does not necessarily support detection for # all types in our tests data; thus, for now we just check that the magika diff --git a/python/scripts/run_quick_test_magika_module.py b/python/scripts/run_quick_test_magika_module.py index 9752c8a1..6d0de23a 100755 --- a/python/scripts/run_quick_test_magika_module.py +++ b/python/scripts/run_quick_test_magika_module.py @@ -31,7 +31,7 @@ def main() -> None: m = Magika(prediction_mode=PredictionMode.HIGH_CONFIDENCE) - print(f"Using model: {m.get_model_dir_name()}") + print(f"Magika instance details: {m}") res = m.identify_bytes(b"text") assert res.dl.label == ContentTypeLabel.UNDEFINED diff --git a/python/src/magika/magika.py b/python/src/magika/magika.py index a1642a7c..ce52a355 100644 --- a/python/src/magika/magika.py +++ b/python/src/magika/magika.py @@ -100,6 +100,18 @@ def __init__( self._onnx_session = self._init_onnx_session() + def __repr__(self) -> str: + return str(self) + + def __str__(self) -> str: + return f'Magika(version="{self.get_version()}", model_name="{self.get_model_name()}")' + + def get_version(self) -> str: + return str(__import__(self.__module__).__version__) + + def get_model_name(self) -> str: + return self._model_dir.name + def identify_path(self, path: Path) -> MagikaResult: if not isinstance(path, Path): raise TypeError("Input path should be of type Path") @@ -172,9 +184,6 @@ def _get_default_model_name() -> str: return DEFAULT_MODEL_NAME - def get_model_dir_name(self) -> str: - return self._model_dir.name - @staticmethod def _load_content_types_kb( content_types_kb_json_path: Path,