File size: 5,240 Bytes
8620228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import ast

def extract_apis(code):
    tree = ast.parse(code)
    api_list = []
    imported_modules = {}
    scope_stack = []

    class ApiExtractor(ast.NodeVisitor):
        def visit_Import(self, node):
            for alias in node.names:
                module_name = alias.name
                alias_name = alias.asname or alias.name
                if scope_stack:
                    imported_modules[scope_stack[-1]][alias_name] = module_name
                else:
                    imported_modules[alias_name] = module_name
                top_level_module = module_name.split('.')[0]
                if top_level_module not in imported_modules:
                    imported_modules[top_level_module] = module_name
            self.generic_visit(node)

        def visit_ImportFrom(self, node):
            module = node.module
            if module:
                for alias in node.names:
                    full_name = f'{module}.{alias.name}'
                    alias_name = alias.asname or alias.name
                    if scope_stack:
                        imported_modules[scope_stack[-1]][alias_name] = full_name
                    else:
                        imported_modules[alias_name] = full_name
                top_level_module = module.split('.')[0]
                if top_level_module not in imported_modules:
                    imported_modules[top_level_module] = module
            self.generic_visit(node)

        def visit_ClassDef(self, node):
            scope_stack.append(node.name)
            imported_modules[node.name] = {}
            self.generic_visit(node)
            scope_stack.pop()

        def visit_Attribute(self, node):
            if isinstance(node.value, ast.Name):
                id_lookup = node.value.id
                current_scope = scope_stack[-1] if scope_stack else None
                base_module = (imported_modules[current_scope].get(id_lookup)
                               if current_scope and id_lookup in imported_modules[current_scope]
                               else imported_modules.get(id_lookup))
                if base_module:
                    api_call = f"{base_module}.{node.attr}"
                    if api_call not in api_list:
                        api_list.append(api_call)
            self.generic_visit(node)

        def visit_Name(self, node):
            id_lookup = node.id
            current_scope = scope_stack[-1] if scope_stack else None
            base_module = (imported_modules[current_scope].get(id_lookup)
                           if current_scope and id_lookup in imported_modules[current_scope]
                           else imported_modules.get(id_lookup))
            if base_module and base_module not in api_list:
                api_list.append(base_module)
            self.generic_visit(node)

        def visit_Call(self, node):
            function_name = None
            if isinstance(node.func, ast.Name):
                function_name = node.func.id
            elif isinstance(node.func, ast.Attribute):
                attrs = []
                current = node.func
                while isinstance(current, ast.Attribute):
                    attrs.append(current.attr)
                    current = current.value
                if isinstance(current, ast.Name):
                    attrs.append(current.id)
                    attrs.reverse()
                    function_name = '.'.join(attrs)

            if function_name:
                current_scope = scope_stack[-1] if scope_stack else None
                base_module = (imported_modules[current_scope].get(function_name.split('.')[0])
                               if current_scope and function_name.split('.')[0] in imported_modules[current_scope]
                               else imported_modules.get(function_name.split('.')[0]))
                if base_module:
                    api_call = f"{base_module}{'.' + '.'.join(function_name.split('.')[1:]) if len(function_name.split('.')) > 1 else ''}"
                    if api_call not in api_list:
                        api_list.append(api_call)

            # Direct function usage as arguments
            for arg in node.args:
                if isinstance(arg, ast.Name) and arg.id in imported_modules:
                    api_call = imported_modules[arg.id]
                    if api_call not in api_list:
                        api_list.append(api_call)
                    
            self.generic_visit(node)

    ApiExtractor().visit(tree)
    return list(set([api for api in api_list if "." in api]))

if __name__ == "__main__":
    import json
    from datasets import load_dataset
    dataset = load_dataset("bigcode/bigcodebench-hard", split="v0.1.0_hf")
    apis = []
    for item in dataset:
        complete_prompt = item["complete_prompt"]
        canonical_solution = item["canonical_solution"]
        apis.extend(extract_apis(complete_prompt+canonical_solution))
    
    sorted_apis = sorted(apis, lambda x: x.split('.')[0])
    with open("apis.txt", "w") as f:
        for api in sorted_apis:
            f.write(api + "\n")