Skip to main content

Create an overloaded function around a core choosing function.

Project description

Overview

Create an overloaded function around a core choosing function.

Installation

To install overloadable, you can use pip. Open your terminal and run:

pip install overloadable

Implementation

import functools
import typing

__all__ = ["overloadable"]

class Holder:...

def overloadable(old, /):
    holder = Holder()
    @functools.wraps(old)
    def new(*args, **kwargs):
        key = old(*args, **kwargs)
        value = holder._data.lookup[key]
        ans = value(*args, **kwargs)
        return ans
    holder._data = new
    new.lookup = dict()
    new.overload = functools.partial(tool, data=new)
    return new

def tool(key=None, **kwargs):
    return functools.partial(decorator, key=key, **kwargs)

def decorator(old, /, *, data, key):
    typing.overload(old)
    data.lookup[key] = old
    return data

Example

from overloadable import overloadable

class Bar:
    def __init__(self, addon) -> None:
        self.addon = addon

    @overloadable
    def foo(self, x):
        if type(x) is int:
            return "int"

    @foo.overload("int")
    def foo(self, x):
        return x * x + self.addon

    @foo.overload() # key=None
    def foo(self, x):
        return str(x)[::-1]

bar = Bar(42)
print(bar.foo(1)) # prints 43
print(bar.foo(3.14)) # prints 41.3
print(bar.foo("baz")) # prints zab

License

This project is licensed under the MIT License.

Credits

Thank you for using overloadable!

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

overloadable-1.0.3.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

overloadable-1.0.3-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file overloadable-1.0.3.tar.gz.

File metadata

  • Download URL: overloadable-1.0.3.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.4

File hashes

Hashes for overloadable-1.0.3.tar.gz
Algorithm Hash digest
SHA256 4f8c03e2bfc09a19b899e8a6ee5b96b8045d5dc172851fb029f9627d8adb8cbf
MD5 303dc439c39ca65a6dbd5f3daa77bc58
BLAKE2b-256 308780083e6293630a885a70beb59002197788d1d51000a3c16a55c95887f34a

See more details on using hashes here.

File details

Details for the file overloadable-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for overloadable-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 d429b97f2d4081ca5b0977b22912945606f6293d0edc8a24cfd6833a10d9fb1f
MD5 3ae9906a3c8815219b7eab93bca7fbda
BLAKE2b-256 6fee7d482c8537f2c514b5bce5108d7fc03ac7510438a56ed018e4acc75e2971

See more details on using hashes here.

Supported by

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