Skip to main content

Create an overloaded function around a core choosing function.

Reason this release was yanked:

faulty readme

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.4.tar.gz (3.8 kB view details)

Uploaded Source

Built Distribution

overloadable-1.0.4-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for overloadable-1.0.4.tar.gz
Algorithm Hash digest
SHA256 2491a866afbc131dd603cbee99547633d31bfcc74ea6865e36af204a8dea5235
MD5 0d17379180433db20e78c60d0b2e6a1b
BLAKE2b-256 ea752d2e5d7df2c2910481305c6f58293ea672b5d8066f64e169a96f1660988d

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for overloadable-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 9d60ea3851529760df7f7782689c503196dd834c1dd79db36c70816cb1adddeb
MD5 fed15f75c891cfc5e059418c69518bff
BLAKE2b-256 9a32bcf721ce768925a885d25ad3f6e9141c795395772e9d5c055f883100a988

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