Git Product home page Git Product logo

pyo3-pycon2024's Introduction

Gaussian Mixtures

Repository for the tutorial session "Performant, scientific computation in Python and Rust" at PyCon/PyData Berlin 2024.

To the participants of the tutorial session:

All stages of the tutorial can be reproduced by checking out different branches of this repository and the slides are written in HTML and will be hosted on GitHub pages shortly before the session so that you can see them in our browser.

Note: If you want to reproduce the tutorial on your device, please follow the installation steps described below prior to the session to avoid longer delays during at the beginning. Thank you very much!

I use uv for dependency handling but you can use pip-tools or plain pip / virtual environments instead.

Uses a modern tech stack based with a lot of (opinionated) choices:

The repository is structured as follows:

  • python sources for the pure python project

  • rust sources for the optimized code in rust

  • bindings sources for wrapping the rust code for python.

    This is separated from the rust crate as this is required for inline testing

  • pyproject.toml, requirements.txt The definition of the python project and the locked virtual environment

  • Cargo.toml, Cargo.lock: The definition of the workspace and locked dependencies

Installation

Python

I recommend installing with PyEnv.

curl https://pyenv.run | bash
pyenv install -l # choose a recent version
pyenv install 3.12.0

Alternatively, you can use the system's version of course. On macOS using brew is recommended, while on Debian-based Linux distributions, run

sudo apt install python3.12

I have little experience with Windows systems, but you should probably use WSL2 there.

Virtual environment

Dependency handling with uv

First, install uv (or follow instructions on the project page)

curl -LsSf https://astral.sh/uv/install.sh | sh

Create environment and install the package

pyenv shell 3.12 # optional to select version when using pyenv
uv venv
# The next line needs only be called if dependencies are altered
uv pip compile pyproject.toml --all-extras -o requirements.txt
uv pip sync requirements.txt
. ./venv/bin/activate # Optional to activate the environment

Alternatively, you should be able to achieve the same with plain pip

python3.12 -m venv .venv
. ./venv/bin/activate # Optional to activate the environment
pip install -r requirements.txt

You should select the newly created virtual environment in your development environment.

Tasks management with poethepoet

In pyproject.toml there are multiple tasks defined that are available once you have the virtual environment build and activated:

poe # will list all commands
poe lint # runs ruff to fix all auto-fixable issues
poe typing # runs the mypy type checker
poe fmt # runs ruff to format the source files
poe test # runs test and coverage
poe all # runs all of the above
poe install-kernel # makes the environment available in jupyter
poe uninstall-kernel # removes the associated jupyter kernel
poe verify # Checks whether cuda works as expected
poe docs # Builds the docs
poe lab # starts a jupyter service

Build documentation

poe docs
firefox docs/build/html/index.html # or similar

Installing pre-commit GIT hooks

Activate the virtual environment and run

pre-commit install

Then, all modified sources will be checked for formatting and all tests will be on every commit unless if called with the -n (no-verify) switch. The virtual environment needs to be activated for this. If you run into problems with your IDE, please install pre-commit globally for your user.

Build the Python bindings to the Rust code

After calling the following command, changes in Python are effective immediately from now on (restarting the interpreter might be required). Changes in Rust require a rebuild with the same command.

uv pip install -e . -v -U # for an editable install
# or
poe maturin               # for building a wheel

Rust

First install Rust. This is usually done with the rustup tool that manages Rust installations and distribution of tools. On a Unix-like OS, run

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup update

You can check your installation by building the project. The command used for this is cargo a build system (comparable to poetry or rye in the Python world):

cargo build

You should also install the following add-ons to the cargo build system:

cargo install cargo-criterion
cargo install cargo-show-asm

pyo3-pycon2024's People

Contributors

stefanulbrich avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.