mirror of
https://git.roussel.pro/NUS/CS6217-Project.git
synced 2026-02-08 21:20:18 +01:00
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
import ast
|
|
import inspect
|
|
import z3
|
|
import brahma
|
|
|
|
|
|
def list_function_inputs(func):
|
|
"""
|
|
Lists the input parameters of a given Python function.
|
|
|
|
Args:
|
|
func (function): The Python function to inspect.
|
|
|
|
Returns:
|
|
list: A list of strings representing the input parameter names.
|
|
"""
|
|
if not callable(func):
|
|
raise ValueError("The provided argument is not a function.")
|
|
|
|
# Get the signature of the function
|
|
signature = inspect.signature(func)
|
|
|
|
# Extract the parameter names
|
|
inputs = [param.name for param in signature.parameters.values()
|
|
if param.kind in (param.POSITIONAL_OR_KEYWORD, param.KEYWORD_ONLY)]
|
|
|
|
return inputs
|
|
|
|
def get_oldest_references_from_lambda(func):
|
|
"""
|
|
Extracts the oldest values referenced for each variable from a lambda function.
|
|
|
|
Args:
|
|
func (callable): A lambda function, such as (lambda I1_1, I1_2: (I1_1 & I1_2) - 1).
|
|
|
|
Returns:
|
|
dict: A dictionary where keys are variable names (e.g., "I1") and values are the oldest indices referenced.
|
|
"""
|
|
# Get the source code of the lambda function
|
|
source = inspect.getsource(func).strip()
|
|
|
|
# Parse the source code into an AST
|
|
tree = ast.parse(source)
|
|
|
|
# Extract the lambda function node (should be the first node in the body)
|
|
lambda_node = tree.body[0].value
|
|
|
|
# Dictionary to store the oldest reference for each variable
|
|
oldest_references = {}
|
|
|
|
class ReferenceVisitor(ast.NodeVisitor):
|
|
def visit_Name(self, node):
|
|
# Check if the variable matches the naming pattern (e.g., I1_1, I1_2, etc.)
|
|
if "_" in node.id:
|
|
name, index = node.id.rsplit("_", 1)
|
|
if index.isdigit():
|
|
index = int(index)
|
|
if name not in oldest_references or index > oldest_references[name]:
|
|
oldest_references[name] = index
|
|
else:
|
|
if node.id not in oldest_references:
|
|
oldest_references[node.id] = 0
|
|
|
|
# Visit the AST nodes to extract variable references
|
|
ReferenceVisitor().visit(lambda_node)
|
|
|
|
return oldest_references
|
|
|
|
|