Overloading Utilities
Project description
pyloadover
Repository for developing and maintaining the code for pyloadover
Installation
pip install pyloadover
Usage
from pyloadover import overload
@overload
def function():
return None
@overload
def function(x: int):
return x ** 2
print("[1] Calling function():")
print(function())
print("[2] Calling function(5):")
print(function(5))
print("[3] Calling function(5, 25, 125):")
print(function(5, 25, 125))
[1] Calling function():
None
[2] Calling function(5):
25
[3] Calling function(5, 25, 125):
pyloadover.exceptions.NoMatchFoundError: Provided arguments [(5, 25, 125), {}] do not match any signature in group '__main__.function'
Basic Config
from pyloadover import overload, basic_config, FullyQualifiedNameIdGenerator, UniqueSignaturesValidator
@overload
def function():
return None
basic_config(
propagate=True,
function_id_generator=FullyQualifiedNameIdGenerator(),
group_function_validators=[UniqueSignaturesValidator()]
)
@overload
def function():
return None
pyloadover.exceptions.SignatureExistsError: Function signature () already exists in group '__main__.function'
NOTE: By default, the package is configured as follows:
basic_config(
function_id_generator=FullyQualifiedNameIdGenerator(),
group_validators=[EqualIdsValidator(), UniqueSignaturesValidator()]
)
Groups
import pyloadover
from pyloadover import get_group, pyoverload
function_group = get_group("__main__.function")
@function_group # Method 1
def function(name: str):
return f"Hello {name}!"
@pyoverload("__main__.function") # Method 2
def function(first_name: str, last_name: str):
return f"Hello {first_name}, your last name is {last_name}!"
@pyloadover.__main__.function # Method 3
def function(first_name: str, middle_name: str, last_name: str):
return f"Hello {first_name}, your middle name is {middle_name}, and your last name is {last_name}!"
print('[1] Calling function("Foo"):')
print(function_group.call_matching_function("Foo")) # Method 1
print('[2] Calling function("Foo", "Bar"):')
print(function("Foo", "Bar")) # Method 2
print('[3] Calling function("Foo", "IDK", "Bar"):')
print(function_group.retrieve_single_matching_function("Foo", "IDK", "Bar")("Foo", "IDK", "Bar")) # Method 3
[1] Calling function("Foo"):
Hello Foo!
[2] Calling function("Foo", "Bar"):
Hello Foo, your last name is Bar!
[3] Calling function("Foo", "IDK", "Bar"):
Hello Foo, your middle name is IDK, and your last name is Bar!
Function ID Generators & Group Function Validators
-
Function ID Generators (Inherit from
FunctionIdGenerator
):FullyQualifiedNameIdGenerator
-> Generates an ID which is composed out of a combination of the function's module and the function's qualified nameNameIdGenerator
-> Generates an ID which is composed out of the function's name
-
Group Function Validators (Inherit from
GroupFunctionValidator
):EqualIdsValidator
-> Validates that the ID of the function matches the ID of the group it is registered toUniqueSignaturesValidator
-> Validates that the signature of the function does not already exist in the group it is registered to
NOTE: You could also create your own custom validators / generators!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
pyloadover-3.0.0.tar.gz
(6.0 kB
view hashes)
Built Distribution
Close
Hashes for pyloadover-3.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f0e1f114789edba0efc871b23efc02d603748f7d9d72522093f65c4a5e75e22c |
|
MD5 | 6c8236d1ed8f3b301c6637b580d584f2 |
|
BLAKE2b-256 | a675bd610d4353ad36faf011b9b543333cd727f3926d23eeaffbda5c78df3a5c |