/hk-python-oct-webinar

Demo of fusion between Python and Rust using maturin and PyO3

Primary LanguageRustMIT LicenseMIT

(Insert GitHub Actions/codecov status badges here) Code style: black

README

Prerequisites

Install Rust by following the instructions on their website.

This demo was tested with Python 3.10 and the following rustup/Rust versions:

$ rustup --version
rustup 1.26.0 (5af9b9484 2023-04-05)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.73.0 (cc66ad468 2023-10-03)`

Create a new Python 3.10 (install it beforehand) virtual environment using venv and switch to it.

python3.10 -u -m venv ~/venv-hkpythonoctwebinar ;
source ~/venv-hkpythonoctwebinar/bin/activate && pip install --upgrade pip ;

Install cargo-binstall.

Make sure Cargo is usable and use cargo-binstall to install cargo-tarpaulin (code coverage).

cargo binstall -y cargo-tarpaulin

Clone the repository and cd into it.

maturin-related (Rust)

Development command:

cargo clippy --all-targets -- -D warnings && python -u -m pip install --upgrade pip ; pip install --upgrade -r requirements.txt && cargo tarpaulin --all-targets --count --exclude-files=target/* --engine=llvm --fail-under=80 --ignored --no-dead-code --out=stdout --skip-clean --target-dir=target/tarpaulin-target/ && maturin develop

Switch maturin develop (debug Rust code) to maturin develop --release for optimized Rust code.

Running

Run the module using:

(venv-hkpythonoctwebinar) $ python -u -m hkpythonoctwebinar

Ensure any file is present in the root folder of the git repository with the hardcoded filename pydocs-v3pt12-210-copies-over-23M-lines.txt, it will then be truncated to 1 million lines after running the following command:

(venv-hkpythonoctwebinar) $ python -u -m hkpythonoctwebinar.truncate

Run tools on your package

(All commands here must be run within the venv, in the main repository directory - not any subfolders)

For comprehensive tests and all linters:

python -u -m pytest --black --cov --mypy --pylint --ruff

Documentation generation via Sphinx

  • Change into docs/ folder: cd docs/
  • Run generation command - you must first be in the docs/ directory: ./gen-sphinx-html.sh
  • Your generated HTML files are now in docs/build/html/ directory
  • Open index.html to start