Skip to main content

Runtime dispatch with decorator-based overload registration.

Project description

Tree Interval Logo

WizeDispatcher

A lightweight, version-robust Python runtime dispatch library with powerful overload registration and type-based selection

Try it on Replit

✨ Features

  • 🎯 Overload Registration via Decorators
    Register multiple implementations for the same function, method, or property setter, with keyword or positional type constraints.

  • 📝 Type Hint Overrides
    Overloads can specify types in the decorator to override or partially use type hints from the function signature.

  • Partial Type Specification
    Missing type constraints in an overload are automatically filled from the fallback/default implementation.

  • 📊 Weighted Specificity Scoring
    Runtime match scoring system:

    • 3 → Exact type match
    • 2 → Instance match
    • 1Any annotation match
    • 0 → Wildcard match (unspecified param)
    • -1 → No match
  • 🛠 Full Typing Support
    Union, Optional, Literal, generic containers (list[int], tuple[int, ...]), and callable detection.

  • 📦 Method & Property Support
    Works with instance methods, @classmethod, @staticmethod, and property setters.

  • 🚀 Fast Cached Dispatch
    Caches previous matches to speed up repeated calls.

  • 🧩 Varargs & Kwargs Handling
    Fully supports *args and **kwargs in overloads, resolving them according to parameter order.

  • 🐍 Version Robust
    Works consistently across Python 3.8+ with no dependencies.

🚀 Quick Start

from wizedispatcher import dispatch

# Fallback
def greet(name: object) -> str:
    return f"Hello, {name}!"

# Keyword constraint
@dispatch.greet(name=str)
def _(name: str) -> str:
    return f"Hello, {name}, nice to meet you."

# Positional constraint
@dispatch.greet(str, int)
def _(name, age) -> str:
    return f"{name} is {age} years old"

print(greet("Alice"))   # Hello, Alice, nice to meet you.
print(greet("Bob", 30)) # Bob is 30 years old

📊 Weight-Based Evaluation

WizeDispatcherevaluates overloads with a specificity weight system:

Weight Meaning
3 Exact type match
2 Instance match
1 Any annotation match
0 Wildcard (unspecified param)
-1 No match (discarded)

Example:
If two overloads match, the one with the higher total weight is chosen.

🧩 Partial Type Specification

# Default function defines all parameters
def process(a: int, b: str, c: float) -> str:
    return "default"

# Overload defines only 'a', inherits 'b' and 'c' types from default
@dispatch.process(a=str)
def _(a: str, b, c) -> str:
    return f"a is str, b is {type(b)}, c is {type(c)}"

🛠 Methods & Properties

class Converter:
    @property
    def value(self) -> int:
        return self._value

    @value.setter
    def value(self, val: object) -> None:
        self._value = val  # fallback setter

    @dispatch.value(value=int)
    def _(self, value: int) -> None:
        self._value = value * 10

    @dispatch.value(value=str)
    def _(self, value: str) -> None:
        self._value = int(value)

c = Converter()
c.value = 3
print(c.value)  # 30
c.value = "7"
print(c.value)  # 7

📦 Installation

pip install wizedispatcher

📚 Documentation

  • Wiki: Complete documentation in /wizedispatcher_wiki
  • Examples: Ready-to-run demos in /demo

📝 License

This project is licensed under the MIT License - see the LICENSE file.

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

wizedispatcher-0.1.3.tar.gz (36.6 kB view details)

Uploaded Source

Built Distribution

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

wizedispatcher-0.1.3-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file wizedispatcher-0.1.3.tar.gz.

File metadata

  • Download URL: wizedispatcher-0.1.3.tar.gz
  • Upload date:
  • Size: 36.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for wizedispatcher-0.1.3.tar.gz
Algorithm Hash digest
SHA256 3ecdc95bac7a837decb412dc1ecf86d1c8d485621e363ddfd857d2040204cffd
MD5 faeb95575be8d695f1bcf42c21f3e01c
BLAKE2b-256 f75bdfedd24e988f3c984c6f8ed7a5a8bda3556576c66588127a0948caa0a55f

See more details on using hashes here.

File details

Details for the file wizedispatcher-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: wizedispatcher-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for wizedispatcher-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9ac24d6de52017b94b34030d612e8da441ee860b85a2f7600507e90b33ccbcb6
MD5 2c754a90d76df117cf743d36e3fb3452
BLAKE2b-256 ed0964954953fd98bb1183f8dc2b4c1329aec0a6b244f8c029cbef17e67e85b5

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