Spaces:
Runtime error
Runtime error
Commit
·
d5eeec7
1
Parent(s):
1752bad
Modify AST nodes inplace and write to file
Browse files- TechdocsAPI/app.py +1 -0
- TechdocsAPI/{backend/requirements.txt → requirements.txt} +0 -0
- techdocs/utils/functools.py +5 -0
- techdocs/utils/parse.py +11 -10
- testing/test.py +21 -0
TechdocsAPI/app.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from backend import app
|
TechdocsAPI/{backend/requirements.txt → requirements.txt}
RENAMED
File without changes
|
techdocs/utils/functools.py
CHANGED
@@ -41,3 +41,8 @@ def request_inference(config, code_block, max_retries=1):
|
|
41 |
config.update({"access_token":get_access_token(data)})
|
42 |
|
43 |
return request_inference(config,config["access_token"], code_block, max_retries=max_retries-1)
|
|
|
|
|
|
|
|
|
|
|
|
41 |
config.update({"access_token":get_access_token(data)})
|
42 |
|
43 |
return request_inference(config,config["access_token"], code_block, max_retries=max_retries-1)
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
|
48 |
+
|
techdocs/utils/parse.py
CHANGED
@@ -7,20 +7,18 @@ from utils.functools import *
|
|
7 |
|
8 |
|
9 |
# Function to extract and print the outermost nested function with line number
|
10 |
-
def extract_outermost_function(node,
|
11 |
-
|
12 |
if isinstance(node, ast.FunctionDef):
|
13 |
function_def = ast.unparse(node)
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
|
19 |
|
20 |
for child in ast.iter_child_nodes(node):
|
21 |
-
|
22 |
-
line_number = child.lineno
|
23 |
-
extract_outermost_function(child, line_number)
|
24 |
|
25 |
# Function to traverse directories recursively and extract functions from Python files
|
26 |
def extract_functions_from_directory(config):
|
@@ -32,4 +30,7 @@ def extract_functions_from_directory(config):
|
|
32 |
with open(file_path, "r",errors='ignore') as file:
|
33 |
content = file.read()
|
34 |
parsed = ast.parse(content)
|
35 |
-
extract_outermost_function(parsed, config)
|
|
|
|
|
|
|
|
7 |
|
8 |
|
9 |
# Function to extract and print the outermost nested function with line number
|
10 |
+
def extract_outermost_function(node, config):
|
11 |
+
|
12 |
if isinstance(node, ast.FunctionDef):
|
13 |
function_def = ast.unparse(node)
|
14 |
+
response = request_inference(config=config, code_block=function_def)
|
15 |
+
docstr = response['docstr']
|
16 |
+
docstring = ast.Expr(value=ast.Str(s=docstr))
|
17 |
+
node.body.insert(0, docstring)
|
18 |
|
19 |
|
20 |
for child in ast.iter_child_nodes(node):
|
21 |
+
extract_outermost_function(child, config)
|
|
|
|
|
22 |
|
23 |
# Function to traverse directories recursively and extract functions from Python files
|
24 |
def extract_functions_from_directory(config):
|
|
|
30 |
with open(file_path, "r",errors='ignore') as file:
|
31 |
content = file.read()
|
32 |
parsed = ast.parse(content)
|
33 |
+
extract_outermost_function(parsed, config)
|
34 |
+
docstr_code = ast.unparse(parsed)
|
35 |
+
with open(file_path, "w",errors='ignore') as file:
|
36 |
+
file.write(docstr_code)
|
testing/test.py
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
def add(a, b):
|
2 |
+
return a + b
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
def multiply(a, b):
|
7 |
+
return a * b
|
8 |
+
|
9 |
+
def subtract(a, b):
|
10 |
+
return a - b
|
11 |
+
|
12 |
+
def divide(a, b):
|
13 |
+
if b == 0:
|
14 |
+
raise ValueError("Cannot divide by zero")
|
15 |
+
return a / b
|
16 |
+
|
17 |
+
|
18 |
+
def func(*args, **kwargs):
|
19 |
+
def wrapper(*args, **kwargs):
|
20 |
+
return func(*args, **kwargs)
|
21 |
+
return wrapper
|