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

[!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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

witty-0.3.2.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

witty-0.3.2-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file witty-0.3.2.tar.gz.

File metadata

  • Download URL: witty-0.3.2.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for witty-0.3.2.tar.gz
Algorithm Hash digest
SHA256 e0a3b1a84fc4429d8b8ab6031c9d2c171b30ef2e64d80dbe89a02b12e3442fa3
MD5 95e29ec761e09e9c6ac705f3e8f1cc0f
BLAKE2b-256 ffa3211a17423c88445308156f50186696bbab6442dce14e41dd3a97bd88a33f

See more details on using hashes here.

Provenance

The following attestation bundles were made for witty-0.3.2.tar.gz:

Publisher: ci.yaml on funkelab/witty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file witty-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: witty-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.14

File hashes

Hashes for witty-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1de2e8d4353134d26413f221c584d68b978830d6788434266ff03e05ae33dab4
MD5 e243f0b3d5563fd3d1c2e76a466c3283
BLAKE2b-256 dc063d97145eedc8e8905d2482ff34b2086808fafe7528fcaa9d46bdfb95d6ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for witty-0.3.2-py3-none-any.whl:

Publisher: ci.yaml on funkelab/witty

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 files

0.3.1

2 files

0.3.0

2 files

0.2.1

2 files

0.2.0

2 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