Skip to main content

Module for conditional decorators in Python

Project description

pyConDec

pyConDec is a lightweight Python library that provides conditional decorators — decorators that apply an acceleration or transformation only when the required dependency is available, and fall back to a no-op otherwise.

The primary use case is Numba JIT compilation: code decorated with cond_jit will be compiled with numba.jit when Numba is installed, and will run as plain Python when it is not. This lets you write performance-optimised code that remains portable and installable without making Numba a hard dependency.


Installation

From PyPI (once published)

pip install pyConDec

From source

git clone https://github.com/eskoruppa/pyConDec.git
cd pyConDec
pip install .

To also install the optional Numba dependency:

pip install numba

Usage

cond_jit — conditional numba.jit

cond_jit wraps numba.jit. When Numba is installed the function is JIT-compiled; when it is not, the original Python function is returned unchanged.

from pycondec import cond_jit

@cond_jit(nopython=True, cache=True)
def dot_product(a, b):
    result = 0.0
    for i in range(len(a)):
        result += a[i] * b[i]
    return result

print(dot_product([1.0, 2.0, 3.0], [4.0, 5.0, 6.0]))  # 32.0

If Numba is not installed, dot_product behaves as a regular Python function — no import errors, no code changes needed.

cond_jitclass — conditional numba.experimental.jitclass

import numpy as np
from pycondec import cond_jitclass

spec = [('value', float)]

@cond_jitclass(spec)
class Counter:
    def __init__(self, value):
        self.value = value

    def increment(self):
        self.value += 1.0

Again, if Numba is absent the class is returned as a plain Python class.

cond_dec — conditional arbitrary decorator

cond_dec is the general-purpose variant. It applies any decorator conditionally based on a boolean flag. Two calling styles are supported:

Style 1 — pre-configured decorator (decorator already holds its own arguments):

from functools import lru_cache
from pycondec import cond_dec

USE_CACHE = True

@cond_dec(lru_cache(maxsize=128), USE_CACHE)
def expensive(n):
    return sum(range(n))

Style 2 — decorator factory with arguments (pass the factory and its arguments separately):

from functools import lru_cache
from pycondec import cond_dec

USE_CACHE = True

@cond_dec(lru_cache, USE_CACHE, maxsize=128)
def expensive(n):
    return sum(range(n))

Both styles are equivalent. The second style mirrors the cond_jit experience and is convenient when you want to keep the decorator factory and its arguments readable inline.

When condition is False the original, undecorated function is returned — no branching needed at the call site and no hard dependency on the library providing the decorator.


License

GNU General Public License v2.0 — see LICENSE for details.

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

pycondec-0.0.1.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

pycondec-0.0.1-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file pycondec-0.0.1.tar.gz.

File metadata

  • Download URL: pycondec-0.0.1.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0rc1

File hashes

Hashes for pycondec-0.0.1.tar.gz
Algorithm Hash digest
SHA256 df194ec4fe5a00f1cbd4409963f1fee01c7c1fa74b85c1b1db464274919fc205
MD5 e78704605cc91f994ea96142d8066626
BLAKE2b-256 09a329e8e6edf9ab238d90929b14bfdb99594d1bda06b75ed5b7e1083077e9a8

See more details on using hashes here.

File details

Details for the file pycondec-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pycondec-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.0rc1

File hashes

Hashes for pycondec-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 509e72fc884a165be7d1dd532fc77f09151b72e15d7668f0db76d9b24d91ec03
MD5 c2d19b761c7651c116898e19ad2a366d
BLAKE2b-256 e599dd22dbe80fe610180bef8bb5be4153ecf847875b843b92652bf430fb6d3c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page