ahvn.utils.basic.func_utils module¶
- ahvn.utils.basic.func_utils.code2func(code, func_name=None, env=None)[source]¶
Extract a callable function from a code snippet.
Warning
This function uses exec() to execute the provided code snippet. Executing arbitrary code is a security risk and can lead to remote code execution. Only use this function with trusted code sources. Do not use it to process untrusted user input.
- Parameters:
code (str) – The code snippet containing the function definition.
func_name (Optional[str], optional) – The name of the function to extract. Defaults to None. If None, and only one callable is found, that function will be used.
env (Optional[Dict], optional) – The environment in which to execute the code. Defaults to None.
- Returns:
The extracted callable function.
- Return type:
Callable
- Raises:
ValueError – If no callable is found or multiple callables without specifying func_name.
- ahvn.utils.basic.func_utils.funcwrap(exec_func, sig_func)[source]¶
Create a wrapper function that calls exec_func but has the signature and metadata of sig_func.
- ahvn.utils.basic.func_utils.parse_docstring(func)[source]¶
Parse the docstring of a Python function.
- Parameters:
func (Callable) – The Python function whose docstring is to be parsed.
- Returns:
A dictionary containing the parsed components of the docstring.
- Return type:
Dict
- ahvn.utils.basic.func_utils.synthesize_docstring(description=None, input_schema=None, output_schema=None, style='google')[source]¶
Synthesize a docstring from tool specification attributes.
- Parameters:
description (Optional[str], optional) – Tool description. Defaults to None.
input_schema (Optional[Dict[str, Any]], optional) – Parameters schema (JSON schema object). Defaults to None.
output_schema (Optional[Dict[str, Any]], optional) – Output schema (JSON schema object). Defaults to None.
style (str, optional) – Docstring style (‘google’, ‘numpy’, ‘rest’). Defaults to ‘google’.
- Returns:
The synthesized docstring.
- Return type:
- ahvn.utils.basic.func_utils.synthesize_def(name, input_schema=None, output_schema=None, docstring=None, code='pass')[source]¶
Generate a Python function definition from schema metadata.
- ahvn.utils.basic.func_utils.synthesize_signature(name, input_schema=None, arguments=None)[source]¶
Generate a Python function call signature with provided arguments and default values.
- Parameters:
name (
str) – The function name.input_schema (
Optional[Dict[str,Any]]) – JSON schema for function input_schema (same format as synthesize_def).arguments (
Optional[Dict[str,Any]]) – Dict of argument values to include in the signature. Missing arguments will use their default values from the schema.
- Returns:
The function call signature, e.g., “f(a=1, b=5)”.
- Return type:
Example
>>> synthesize_signature("f", {"type": "object", "properties": {"a": {"type": "int"}, "b": {"type": "int", "default": 5}}}, {"a": 1}) 'f(a=1, b=5)'