These functions provide a unified way to retrieve or format a function’s parameters.

Functions

build_function_key()

core_lib.helpers.func_utils.build_function_key() [source]

Will format a new string by merging a unique message with the function parameters and custom parameters.

def build_function_key(key: str, func, *args, **kwargs) -> str:
    ....

Arguments

  • key (str): Base string for formatting the parameter, when not set the func.qualname is used.
  • func: Function from which we wish to extract the parameters.
  • *args, **kwargs: The function’s args/kwargs for building the result string.

Returns

(str): Returns a formatted key.

Example

from core_lib.helpers.func_utils import build_function_key


def function_to_format(param_1, param_2, param_3="hello"):
    pass


formatted_parameters = build_function_key('key_{param_1}_{param_2}_{param_3}', function_to_format, 1, 2, "hello world")
print(formatted_parameters)  # key_1_2_hello world

formatted_parameters = build_function_key('key_{param_1}_{param_2}_{param_3}', function_to_format, 1)
print(formatted_parameters)  # key_1_!Eparam_2E!_hello

Note: Will return None if the parameter’s value is missing.

get_func_parameters_as_dict()

core_lib.helpers.func_utils.get_func_parameters_as_dict() [source]

Extracts a function’s parameters to dict, where key of the dictionary will be the parameter’s name and value will be the value of the parameter.

def get_func_parameters_as_dict(func, *args, **kwargs) -> dict:
    ....

Arguments

  • func: Function from which we wish to extract a parameter.
  • *args, **kwargs: The function’s args/kwargs for building the result string.

Returns

(dict): Returns a dictionary with the parameter’s name and value as key-value pair.

Example

from core_lib.helpers.func_utils import get_func_parameters_as_dict

# A function that will take in 2 parameters one is type integer and other is string
def function_to_extract(param_1: int, param_2: str, param_3 = "hello"):
    pass

extracted_dict = get_func_parameters_as_dict(function_to_extract) 
print(extracted_dict)# {'param_1':None,'param_2':None, 'param_3':'hello'}

extracted_dict = get_func_parameters_as_dict(function_to_extract, 1, "hello", "world") 
print(extracted_dict)# {'param_1':'1', 'param_2':'hello', 'param_3':'world'}

Note: Will return the value as None if the parameter’s value is missing.

get_func_parameter_index_by_name()

core_lib.helpers.func_utils.get_func_parameter_index_by_name() [source]

Takes in a single parameter and function name and will return the parameter’s index

def get_func_parameter_index_by_name(func, parameter_name: str) -> int:
    ....

Arguments

  • func: Function to which the parameter belongs.
  • parameter_name (str): The parameter’s name from the function.

Returns

(int): Returns the index of the parameter.

Example

from core_lib.helpers.func_utils import get_func_parameter_index_by_name

def function_to_get_param_index(param_1, param_2):
    pass

parameter_index = get_func_parameter_index_by_name(function_to_get_param_index, "param_1") 
print(function_to_get_param_index) # 0

parameter_index = get_func_parameter_index_by_name(function_to_get_param_index, "param_2")
print(function_to_get_param_index) # 1

Note: Will raise an exception if the parameter passed is not valid

Keyable Class

core_lib.helpers.func_utils.Keyable [source]

The Keyable class allows users to create a custom representation of their data, which is generated by build function key. The user should implement the abstract function key() to return the custom string of formatted data.

Example

from core_lib.helpers.func_utils import Keyable, build_function_key

class User(Keyable):

    def __init__(self, u_id, name, details):
        self.id = u_id
        self.name = name
        self.details = details

    def key(self) -> str:
        return f'User(id:{self.id}, name:{self.name})'

def function_to_format(custom_key):
    pass

        
formatted_parameters = build_function_key('{custom_key}', function_to_format, User(4, 'Rosa Doe'))
print(formatted_parameters)  # User(id:4, name:Rosa Doe)