Overloading Utilities
Project description
pyloadover
Repository for developing and maintaining the code for pyloadover
Installation
pip install pyloadover
Basic 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!
from pyloadover.functions import Function, FunctionContext, FunctionIdGenerator
from pyloadover.groups import GroupContext, GroupFunctionValidator
class CustomIdGenerator(FunctionIdGenerator):
def generate_id(self, context: FunctionContext) -> str:
pass
class CustomFunctionValidator(GroupFunctionValidator):
def validate_function(self, group_context: GroupContext, function: Function):
pass
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.1.0.tar.gz
(6.1 kB
view hashes)
Built Distribution
Close
Hashes for pyloadover-3.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bd51802e0cd8f10db5a8de946f9013d4d20e4261af687e2b5ec3c52676cbd61e |
|
MD5 | 1078e2a4e4b35f53e19ca1f889a3e072 |
|
BLAKE2b-256 | c4419c00f50c2a601cfd464d54e649386b27423b72b9698d226f41abf7328e34 |