Well-in-Time Compiler for Cython Modules
Project description
witty
A "well-in-time" compiler using cython or
nanobind to compile pyx or
C/C++ modules (respectively) at runtime.
Installation
You can install witty via pip:
# for Cython support
pip install witty[cython]
# for Nanobind support
pip install witty[nanobind]
Cython
[!NOTE] Cython is currently included by default with witty, but this will change in the future. Please install with
pip install witty[cython]if you wish to compile Cython modules.
from witty import compile_cython
fancy_module_pyx = """
def add(int a, int b):
return a + b
"""
# equivalent to "import fancy_module"
fancy_module = compile_cython(fancy_module_pyx)
result = fancy_module.add(3, 2)
print("fancy_module.add(3, 2) =", result)
Nanobind
[!NOTE] Cython is currently included by default with witty, but this will change in the future. Please install with
pip install witty[nanobind]if you wish to compile nanobind modules.
from witty import compile_nanobind
fancy_module_cpp = """
#include <nanobind/nanobind.h>
int add(int a, int b) {
return a + b;
}
NB_MODULE(fancy_module, m) {
m.def("add", &add);
}
"""
# equivalent to "import fancy_module"
fancy_module = compile_nanobind(fancy_module_cpp)
result = fancy_module.add(3, 2)
print("fancy_module.add(3, 2) =", result)
This module will no longer be needed if/when https://github.com/cython/cython/pull/555 gets merged into Cython.
Why?
Compilation at runtime is very handy to modify C/C++/PYX sources based on type information or configurations that are not known during build time. This allows combining the optimizations of C++ template libraries with Python's runtime flexibility, like so:
import witty
source_pxy_template = """
cdef extern from '<vector>' namespace 'std':
cdef cppclass vector[T]:
vector()
void push_back(T& item)
size_t size()
def to_vector(values):
vec = vector[{type}]()
for x in values:
vec.push_back(x)
return vec
"""
fancy_module = witty.compile_cython(
source_pxy_template.format(type="float"), language="c++"
)
# create a C++ vector of floats from a list
vec_float = fancy_module.to_vector([0.1, 0.2, 0.3, 1e10])
print(vec_float)
How?
witty invokes cython or nanobind to compile the module given as a
PYX/C/C++ source string (just like it would compile it during build time). The
compiled module ends up in a cache directory, with a hash build from the
content of the source string. Repeated calls to compile_[cython,nanobind]
will only invoke the compiler if the exact source string has not been compiled
before (or if force_rebuild==True). Compilation is protected by a file lock,
i.e., concurrent calls to compile_[cython,nanobind] are safe.
For developers
To push a new release, make sure you've pulled main and are definitely on the commit you want to release, then tag a commit and push to github:
git tag -a vX.Y.Z -m vX.Y.Z
git push upstream --follow-tags
The deploy is handled by workflows/ci.yaml
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file witty-0.3.1.tar.gz.
File metadata
- Download URL: witty-0.3.1.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ee71ba3321c77b64fd9d3a33c2f9ed2424cd7798ee22a4519687c2a88e0f108
|
|
| MD5 |
417d44b1694c9bd6bf9083900ee28df7
|
|
| BLAKE2b-256 |
79558235436e9664dc824c07e9d50b4ff68088a7c3a53d4c971558616cb0fe63
|
Provenance
The following attestation bundles were made for witty-0.3.1.tar.gz:
Publisher:
ci.yaml on funkelab/witty
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
witty-0.3.1.tar.gz -
Subject digest:
8ee71ba3321c77b64fd9d3a33c2f9ed2424cd7798ee22a4519687c2a88e0f108 - Sigstore transparency entry: 312439150
- Sigstore integration time:
-
Permalink:
funkelab/witty@3afed602bfa8b44c98223be62373e00729507ce8 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/funkelab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@3afed602bfa8b44c98223be62373e00729507ce8 -
Trigger Event:
push
-
Statement type:
File details
Details for the file witty-0.3.1-py3-none-any.whl.
File metadata
- Download URL: witty-0.3.1-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e9bb379e06888a6857472f39e865e66ebb9b1523eb4725772f0b71f338e7dea6
|
|
| MD5 |
dd9f065354797227d17c4b329bae91ea
|
|
| BLAKE2b-256 |
e05e82387eb68d7c9de328ae6ad0dbd12e3a6b71425f831f87e05738de509971
|
Provenance
The following attestation bundles were made for witty-0.3.1-py3-none-any.whl:
Publisher:
ci.yaml on funkelab/witty
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
witty-0.3.1-py3-none-any.whl -
Subject digest:
e9bb379e06888a6857472f39e865e66ebb9b1523eb4725772f0b71f338e7dea6 - Sigstore transparency entry: 312439159
- Sigstore integration time:
-
Permalink:
funkelab/witty@3afed602bfa8b44c98223be62373e00729507ce8 -
Branch / Tag:
refs/tags/v0.3.1 - Owner: https://github.com/funkelab
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yaml@3afed602bfa8b44c98223be62373e00729507ce8 -
Trigger Event:
push
-
Statement type: