From 074145f6580ab75a65657dddb2f065dfecb39f1b Mon Sep 17 00:00:00 2001 From: "Xuefeng Ding (IHEP)" <dingxf@ihep.ac.cn> Date: Thu, 14 Nov 2024 01:30:27 +0800 Subject: [PATCH] add more --- .pre-commit-config.yaml | 6 +++ .vscode/settings.json | 10 +++++ CMakeLists.txt | 2 +- README.md | 54 ++++++++++++++++++++++++-- notebooks/example.ipynb | 36 +++++++++++++++++ noxfile.py | 86 ++++++++++++++++++++++++++++++++++++++++- pyproject.toml | 28 ++++++++++++-- 7 files changed, 213 insertions(+), 9 deletions(-) create mode 100644 .pre-commit-config.yaml create mode 100644 .vscode/settings.json create mode 100644 notebooks/example.ipynb diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..1af59d3 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,6 @@ +repos: + - repo: https://github.com/kynan/nbstripout + rev: main # Use a specific tag or commit hash for stability + hooks: + - id: nbstripout + files: \.ipynb$ diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..8657f3f --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "[python]": { + "editor.codeActionsOnSave": { + "source.organizeImports": "always" + }, + "editor.defaultFormatter": "ms-python.black-formatter" + }, + "isort.args": ["--profile", "black"], + "notebook.lineNumbers": "on" +} diff --git a/CMakeLists.txt b/CMakeLists.txt index 708673a..c3ed9ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project("${SKBUILD_PROJECT_NAME}" LANGUAGES CXX VERSION "${SKBUILD_PROJECT_VERSION}") -set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) +# set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) # include(GNUInstallDirs) add_subdirectory(cpp) diff --git a/README.md b/README.md index e21ef73..e259da8 100644 --- a/README.md +++ b/README.md @@ -8,16 +8,24 @@ use scikit-build-core to setup a python-cmake project that can be installed with ## Conclusion -- cannot make packages like import myproj.simulation. It replaces "." with "_" -- most of the case, check following +- package name has nothing to do with the import path. pacakge named my_package is installed with `pip install .`, uninstalled with `pip uninstall my_package`, but can be used as `import kk.ss as ks`. +- installation of cpp files: ```cmake -install(FILES ${CMAKE_CURRENT_BINARY_DIR}/my_main.py DESTINATION .) +install(TARGET mylib LIBRARY DESTINATION .) ``` and set ``` [tool.scikit-build] wheel.install-dir = "kk/ss" ``` +- installation of python files: +``` +[tool.scikit-build.wheel.packages] +"mm/tt" = "python/myproj/simulation" + +[tool.scikit-build] +wheel.exclude = ["CMakeLists.txt","*.cc","*.h"] +``` ## special variable @@ -38,3 +46,43 @@ wheel.install-dir = "kk/ss" - `${SKBUILD_NULL_DIR}`: Anything installed here will not be placed in the wheel. +## style and lint check +### notebook otuput stripping +- use pre-commit +1. install +```bash +pip install pre-commit nbstripout +``` +2. set +```yaml +repos: + - repo: https://github.com/kynan/nbstripout + rev: master # Pin to a specific version if desired + hooks: + - id: nbstripout + files: \.ipynb$ +``` +3. install +```bash +pre-commit install +``` + +## vscode extensions to install +- "isort", which sort python import +``` +Name: isort +Id: ms-python.isort +Description: Import organization support for Python files using isort. +Version: 2023.10.1 +Publisher: Microsoft +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.isort +``` +- "pylint", for complrehensive code analysis +``` +Name: Pylint +Id: ms-python.pylint +Description: Linting support for Python files using Pylint. +Version: 2024.0.0 +Publisher: Microsoft +VS Marketplace Link: https://marketplace.visualstudio.com/items?itemName=ms-python.pylint +``` diff --git a/notebooks/example.ipynb b/notebooks/example.ipynb new file mode 100644 index 0000000..1f75b37 --- /dev/null +++ b/notebooks/example.ipynb @@ -0,0 +1,36 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import myproj.simulation as ms\n", + "print(ms.add(1,2))\n", + "print(ms.complex(1,2))" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "dingxf", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/noxfile.py b/noxfile.py index 77829dd..5fe4057 100644 --- a/noxfile.py +++ b/noxfile.py @@ -2,8 +2,12 @@ nox configuration file. """ -import nox import sys +from pathlib import Path + +import nox + +DIR = Path(__file__).parent.resolve() @nox.session(venv_backend="none") @@ -31,3 +35,83 @@ def dev(session: nox.Session) -> None: "-C", "build-dir=build", ) + + +@nox.session(reuse_venv=True) +def rr_run(session: nox.Session) -> None: + """ + Run sp-repo-review. + """ + + session.install("sp-repo-review[cli]") + session.run("repo-review", ".") + + +# @nox.session +# def rr_lint(session: nox.Session) -> None: +# """ +# Run the linter. +# """ +# session.install("pre-commit") +# session.run("pre-commit", "run", "--all-files", *session.posargs) + + +@nox.session +def rr_pylint(session: nox.Session) -> None: + """ + Run PyLint. + """ + # This needs to be installed into the package environment, and is slower + # than a pre-commit check + session.install("-e.") + session.install("pylint>=3.2") + session.run("pylint", "myproj.simulation", *session.posargs) + + +# @nox.session +# def rr_tests(session: nox.Session) -> None: +# """ +# Run the unit and regular tests for sp-repo-review. +# """ +# session.install("-e.[test,cli]") +# session.run("pytest", *session.posargs, env={"PYTHONWARNDEFAULTENCODING": "1"}) + + +# @nox.session(reuse_venv=True) +# def rr_build(session: nox.Session) -> None: +# """ +# Build an SDist and wheel for sp-repo-review. +# """ + +# build_p = DIR.joinpath("build") +# if build_p.exists(): +# shutil.rmtree(build_p) + +# session.install("build") +# session.run("python", "-m", "build") + + +@nox.session +def build_extension(session: nox.Session) -> None: + # Install build dependencies + session.install( + "pybind11", "scikit-build", "setuptools", "wheel", "pybind11-stubgen" + ) + + # Build the extension module using scikit-build + session.run("python", "-m", "pip", "install", ".", "--no-build-isolation") + + # Generate the .pyi file using pybind11-stubgen + # Replace 'your_package._hello' with the actual module name + session.run("pybind11-stubgen", "your_package._hello") + + # Move the generated .pyi file to the source directory + # By default, pybind11-stubgen outputs to 'stubs/your_package/_hello/__init__.pyi' + stub_src = ( + Path("stubs") / "myproj" / "simulation" / "_csrc" / "_hello" / "__init__.pyi" + ) + stub_dest = Path("myproj") / "simulation" / "_csrc" / "_hello.pyi" + session.run("mv", stub_src, stub_dest, external=True) + + # Clean up the stubs directory + session.run("rm", "-r", "stubs", external=True) diff --git a/pyproject.toml b/pyproject.toml index 13ee28f..59c184d 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,9 +15,7 @@ maintainers = [ ] requires-python = ">=3.9" -dependencies = [ - "typing_extensions", -] +dependencies = [] classifiers = [ "Development Status :: 4 - Beta", @@ -43,4 +41,26 @@ Changelog = "https://package.readthedocs.io/en/latest/changelog.html" [tool.scikit-build] wheel.install-dir = "myproj/simulation" -wheel.exclude = ["CMakeLists.txt","*.cc","*.h"] \ No newline at end of file +wheel.exclude = ["CMakeLists.txt","*.cc","*.h"] + +[tool.mypy] +strict = true +ignore_missing_imports = true +warn_unused_configs = true +warn_unreachable = true +disable_error_code = [] +mypy_path = ["python"] + +[tool.isort] +multi_line_output = 3 +include_trailing_comma = true +force_grid_wrap = 0 +line_length = 88 +profile = "black" + +[tool.pylint."MESSAGES CONTROL"] +disable = [ + "C0114", # Missing module docstring + "C0115", # Missing class docstring + "C0116" # Missing function or method docstring +] \ No newline at end of file -- GitLab