Skip to main content

witty

License PyPI Python Version CI

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

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

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

Metadata

Release files for witty 0.3.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for witty 0.3.3
File Size Uploaded
witty-0.3.3.tar.gz 13.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for witty 0.3.3
File Interpreter ABI Platform
witty-0.3.3-py3-none-any.whl Python 3 none any Details

Total release size: 22.6 kB

Release files / witty-0.3.3.tar.gz

Download URL witty-0.3.3.tar.gz
Size 13.3 kB
Tags Source
SHA-256 checksum
How to use checksums
f5bf3b201c3d59f6c583286255073f36b5e2574c15060508e250997a92b09208
BLAKE2b-256 checksum
How to use checksums
d6fce85e5ce181988d6d789aa0888e45b0818309ca817b00df46f3b125e0bc93
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release files / witty-0.3.3-py3-none-any.whl

Download URL witty-0.3.3-py3-none-any.whl
Size 9.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9ceb8f7d3ed298b6d2e6daa7ac7b77ebd526fcf24e9c162bfcc3b6c503dc6341
BLAKE2b-256 checksum
How to use checksums
e985dc0e211a49c8efdd47fc8284a4a7c461ea09547f847caa8babd380c97c18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 30, 2026.

Transparency log

Release history Release notifications | RSS feed

0.3.4

2 release files

This release

0.3.3 This release

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.2.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page