Function, Program, Converter#
This tutorial will talk about three core classes in the base package:
Function,Program, andTextFunctionProgramConverter.
Function class#
Intuitively, an instance of a Function class maintains a function object which is going to be optimized. Its attributes encompasses function name, arguments list, return type, docstring, and function body.
Please note that a function may not be executable, since the import statements are not included in the function. A Function class only focuses on individual function object.
Program class#
The program class maintains a Python program that is executable. This means a program may incorporate the import statement, globals variables, some class definition, and multiple functions.
TextFunctionProgramConverter class#
The converter can convert a function/program in str to a Function/Program class, or convert between Function instance and Program instance.
Tutorials#
Below are examples on how to use these classes.
[1]:
from llm4ad.base import TextFunctionProgramConverter
This is a program in str type.
[2]:
example_program_str = '''\
import numpy as np
from typing import List
def example_function(arr: List | np.ndarray):
"""This is an example function."""
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
'''
The str program can be converted to a Program instance.
[3]:
example_program = TextFunctionProgramConverter.text_to_program(example_program_str)
print(type(example_program))
print('---------------------------------')
print(str(example_program))
print('---------------------------------')
print(example_program.functions[0])
<class 'llm4ad.base.code.Program'>
---------------------------------
import numpy as np
from typing import List
def example_function(arr: List | np.ndarray):
"""This is an example function."""
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
---------------------------------
def example_function(arr: List | np.ndarray):
"""This is an example function."""
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
The function in the str program can be extracted into a Function instance. Please note that the function is not executable, as it lacks package import statements.
[4]:
example_function = TextFunctionProgramConverter.text_to_function(example_program_str)
print(type(example_function))
print()
print(str(example_function))
print()
print(f'[function name] \n{example_function.name}\n')
print(f'[function docstring] \n{example_function.docstring}\n')
print(f'[function body] \n{example_function.body}\n')
<class 'llm4ad.base.code.Function'>
def example_function(arr: List | np.ndarray):
"""This is an example function."""
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
[function name]
example_function
[function docstring]
This is an example function.
[function body]
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
You can convert a function (in str or Function instance) to a Program instance just by providing a template program. As shown below, the name of the function is modified to that in the template program, the package import statements are added, but the function body are preserved.
[5]:
func = '''
def ha_ha_ha_ha(arr: List | np.ndarray):
"""This is an example function."""
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
'''
program = TextFunctionProgramConverter.function_to_program(func, example_program)
print(str(program))
import numpy as np
from typing import List
def example_function(arr: List | np.ndarray):
"""This is an example function."""
max = np.max(arr)
min = np.min(arr)
result = max + min
return result
[ ]: