From 71ba71e2271d3488b6df933695333060be0225b4 Mon Sep 17 00:00:00 2001 From: "Xuefeng Ding (IHEP)" <dingxf@ihep.ac.cn> Date: Sun, 17 Nov 2024 08:08:21 +0800 Subject: [PATCH] update files and add tests --- .vscode/settings.json | 5 +++- notebooks/example.ipynb | 40 ++++++++++++++++++++++++++++++ python/myproj/simulation/core.py | 7 ++++++ python/tests/test_simulation.py | 42 ++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 python/tests/test_simulation.py diff --git a/.vscode/settings.json b/.vscode/settings.json index afcd6bb..5c83d81 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -31,5 +31,8 @@ ], "clangd.path": "clangd-17", "jupyter.debugJustMyCode": false, - "debugpy.debugJustMyCode": false + "debugpy.debugJustMyCode": false, + "pylint.args": ["--prefer-stubs=true"], + "python.testing.pytestEnabled": true, + "python.testing.unittestEnabled": false } diff --git a/notebooks/example.ipynb b/notebooks/example.ipynb index 1c9db39..4952e58 100644 --- a/notebooks/example.ipynb +++ b/notebooks/example.ipynb @@ -35,6 +35,46 @@ "\n", "print(util.is_namespace(\"myproj\"))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "from io import StringIO\n", + "\n", + "import pylint.lint\n", + "from pylint.reporters import JSONReporter\n", + "\n", + "# Adjust PYTHONPATH\n", + "# sys.path.append('/datafs/users/dingxf/neutrino-physics-tutorial/scikit-build-cpp-python')\n", + "\n", + "# Define the Pylint options\n", + "pylint_options = [\n", + " \"--reports=n\",\n", + " \"--output-format=json\",\n", + " \"--extension-pkg-allow-list=simulation._csrc._hello\",\n", + " \"--prefer-stubs=true\",\n", + " \"--clear-cache-post-run=y\",\n", + " \"/datafs/users/dingxf/neutrino-physics-tutorial/scikit-build-cpp-python/python/myproj/simulation/core.py\", # The file you want to lint\n", + "]\n", + "\n", + "# Create a StringIO buffer to capture Pylint output\n", + "pylint_output = StringIO()\n", + "reporter = JSONReporter(output=pylint_output)\n", + "\n", + "# Run Pylint\n", + "pylint.lint.Run(pylint_options, reporter=reporter, exit=True)\n", + "\n", + "# Get the output\n", + "output = pylint_output.getvalue()\n", + "\n", + "# Parse and display the results\n", + "results = json.loads(output)\n", + "print(json.dumps(results, indent=2))" + ] } ], "metadata": { diff --git a/python/myproj/simulation/core.py b/python/myproj/simulation/core.py index fa362a0..5f49694 100644 --- a/python/myproj/simulation/core.py +++ b/python/myproj/simulation/core.py @@ -1,2 +1,9 @@ +from ._csrc._hello import add, complexf + + def div(a: float, b: float) -> float: return a / b + + +def complex2(a: float, b: float) -> float: + return complexf(a, b) + add(a, b) diff --git a/python/tests/test_simulation.py b/python/tests/test_simulation.py new file mode 100644 index 0000000..52919f5 --- /dev/null +++ b/python/tests/test_simulation.py @@ -0,0 +1,42 @@ +############################################################################# +# Author: Xuefeng Ding <dingxf@ihep.ac.cn> @ IHEP-CAS +# +# Project: cpp-python-small +# Date: 2024 November 17th +# Version: v1.0 +# Description: +# Boilerplate for c++-python project. +# Can be installed with `pip install .` +# +# Maintainer: +# Xuefeng Ding <dingxf@ihep.ac.cn> +# +# All rights reserved. 2024 copyrighted. +############################################################################# +import pytest +from myproj.simulation import add, complexf + + +@pytest.mark.parametrize( + "a, b, expected", + [ + (1, 2, 3), + (10, 15, 25), + (-5, 5, 0), + (0, 0, 0), + ], +) +def test_add_parametrized(a: float, b: float, expected: float) -> None: + assert add(a, b) == expected + + +@pytest.mark.parametrize( + "a, b, expected", + [ + (0, 1, 3), + (10, -5, 7), + (-10, -10, -18), + ], +) +def test_myclass_parametrized(a: float, b: float, expected: float) -> None: + assert complexf(a, b) == expected -- GitLab