Spaces:
Runtime error
Runtime error
Merge pull request #12 from mayureshagashe2105/main
Browse files- techdocs/utils/functools.py +3 -1
- techdocs/utils/parse.py +4 -2
- testing/DBQueries.py +94 -0
- testing/test.py +87 -0
techdocs/utils/functools.py
CHANGED
@@ -43,6 +43,8 @@ def request_inference(config, code_block, max_retries=1):
|
|
43 |
return request_inference(config, code_block, max_retries=max_retries-1)
|
44 |
|
45 |
|
46 |
-
|
|
|
|
|
47 |
|
48 |
|
|
|
43 |
return request_inference(config, code_block, max_retries=max_retries-1)
|
44 |
|
45 |
|
46 |
+
def update_file(file_path, docstr_code):
|
47 |
+
with open(file_path, "w",errors='ignore') as file:
|
48 |
+
file.write(docstr_code)
|
49 |
|
50 |
|
techdocs/utils/parse.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import ast
|
2 |
import os
|
3 |
import requests
|
|
|
4 |
|
5 |
from utils.functools import *
|
6 |
|
@@ -36,5 +37,6 @@ def extract_functions_from_directory(config):
|
|
36 |
parsed = ast.parse(content)
|
37 |
extract_outermost_function(parsed, config)
|
38 |
docstr_code = ast.unparse(parsed)
|
39 |
-
|
40 |
-
|
|
|
|
1 |
import ast
|
2 |
import os
|
3 |
import requests
|
4 |
+
import threading
|
5 |
|
6 |
from utils.functools import *
|
7 |
|
|
|
37 |
parsed = ast.parse(content)
|
38 |
extract_outermost_function(parsed, config)
|
39 |
docstr_code = ast.unparse(parsed)
|
40 |
+
|
41 |
+
write_thread = threading.Thread(target=update_file, args=(file_path, docstr_code))
|
42 |
+
write_thread.start()
|
testing/DBQueries.py
ADDED
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pydantic
|
2 |
+
from bson.objectid import ObjectId
|
3 |
+
pydantic.json.ENCODERS_BY_TYPE[ObjectId] = str
|
4 |
+
from typing import Union, Tuple, List
|
5 |
+
from backend.utils import DBConnection
|
6 |
+
from backend.core.Exceptions import *
|
7 |
+
|
8 |
+
class DBQueries:
|
9 |
+
|
10 |
+
@classmethod
|
11 |
+
def insert_to_database(cls, table_name: str, data: Union[Tuple, List[Tuple]], cols: List[str]=None):
|
12 |
+
"""
|
13 |
+
This method is used to insert data into a specified table in the database.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
table_name (str): The name of the table where the data will be inserted.
|
17 |
+
data (Union[Tuple, List[Tuple]]): The data to be inserted into the table. It can be either a tuple or a list of tuples.
|
18 |
+
cols (List[str], optional): A list of column names in the table. If not provided, the method will assume that all columns are required. Defaults to None.
|
19 |
+
|
20 |
+
Raises:
|
21 |
+
TypeError: If the data is not a tuple or a list of tuples.
|
22 |
+
"""
|
23 |
+
con = DBConnection.get_client()
|
24 |
+
cursor = con.cursor()
|
25 |
+
QUERY = f"INSERT INTO {{table_name}} ({','.join(cols)}) VALUES ".format(table_name=table_name)
|
26 |
+
print(data)
|
27 |
+
if isinstance(data, list):
|
28 |
+
QUERY += '(' + ','.join(['%s' for _ in range(len(data[0]))]) + ')'
|
29 |
+
cursor.executemany(QUERY, data)
|
30 |
+
else:
|
31 |
+
QUERY += '(' + ','.join(['%s' for _ in range(len(data))]) + ')'
|
32 |
+
cursor.execute(QUERY, data)
|
33 |
+
con.commit()
|
34 |
+
|
35 |
+
@classmethod
|
36 |
+
def fetch_data_from_database(cls, table_name: str, cols_to_fetch: Union[str, List[str]], where_clause: str=None):
|
37 |
+
"""
|
38 |
+
This method is a class method that fetches data from a specified table in the database based on the specified
|
39 |
+
column names and an optional WHERE clause.
|
40 |
+
|
41 |
+
Args:
|
42 |
+
- table_name (str): The name of the table from which to fetch data.
|
43 |
+
- cols_to_fetch (Union[str, List[str]]): The column(s) to fetch from the table. If a single string, it should
|
44 |
+
be a comma-separated list of column names.
|
45 |
+
- where_clause (str, optional): An optional WHERE clause to filter the fetched data. Defaults to None.
|
46 |
+
|
47 |
+
Returns:
|
48 |
+
- List[tuple]: A list of tuples, where each tuple represents a row of data fetched from the database.
|
49 |
+
|
50 |
+
Raises:
|
51 |
+
- None
|
52 |
+
"""
|
53 |
+
con = DBConnection.get_client()
|
54 |
+
cursor = con.cursor()
|
55 |
+
if isinstance(cols_to_fetch, str):
|
56 |
+
cols_to_fetch = [cols_to_fetch]
|
57 |
+
cols_to_fetch = ', '.join(cols_to_fetch)
|
58 |
+
QUERY = 'SELECT {cols} FROM {table_name}'.format(cols=cols_to_fetch, table_name=table_name)
|
59 |
+
if where_clause:
|
60 |
+
QUERY = QUERY + ' WHERE ' + where_clause
|
61 |
+
cursor.execute(QUERY)
|
62 |
+
return cursor.fetchall()
|
63 |
+
|
64 |
+
@classmethod
|
65 |
+
def update_data_in_database(cls, table_name: str, cols_to_update: Union[str, List[str]], where_clause: str=None, new_values: Union[str, List[str]]=None):
|
66 |
+
"""
|
67 |
+
This function is used to update data in a specific table in the database.
|
68 |
+
|
69 |
+
Args:
|
70 |
+
table_name (str): The name of the table in which the data needs to be updated.
|
71 |
+
cols_to_update (Union[str, List[str]]): The column(s) that need to be updated. If a single string, it should end with '=%s'. If a list, elements should be separated by '=%s, '.join(cols_to_update).
|
72 |
+
where_clause (str, optional): The WHERE clause to specify the condition for updating the data. Defaults to None.
|
73 |
+
new_values (Union[str, List[str]], optional): The new values that need to be updated in the specified columns. If a single string, it should be a list of new values. Defaults to None.
|
74 |
+
|
75 |
+
Returns:
|
76 |
+
bool: Returns True if the data is successfully updated in the database.
|
77 |
+
|
78 |
+
Raises:
|
79 |
+
None
|
80 |
+
"""
|
81 |
+
con = DBConnection.get_client()
|
82 |
+
cursor = con.cursor()
|
83 |
+
if isinstance(cols_to_update, str):
|
84 |
+
cols_to_update = cols_to_update + '=%s'
|
85 |
+
else:
|
86 |
+
cols_to_update = '=%s, '.join(cols_to_update)
|
87 |
+
if isinstance(new_values, str):
|
88 |
+
new_values = [new_values]
|
89 |
+
QUERY = 'UPDATE {table_name} SET {cols}'.format(table_name=table_name, cols=cols_to_update)
|
90 |
+
if where_clause:
|
91 |
+
QUERY = QUERY + ' WHERE ' + where_clause
|
92 |
+
cursor.execute(QUERY, new_values)
|
93 |
+
con.commit()
|
94 |
+
return True
|
testing/test.py
CHANGED
@@ -1,18 +1,105 @@
|
|
1 |
def add(a, b):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
return a + b
|
3 |
|
4 |
def multiply(a, b):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
return a * b
|
6 |
|
7 |
def subtract(a, b):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
return a - b
|
9 |
|
10 |
def divide(a, b):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
if b == 0:
|
12 |
raise ValueError('Cannot divide by zero')
|
13 |
return a / b
|
14 |
|
15 |
def func(*args, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
return func(*args, **kwargs)
|
18 |
return wrapper
|
|
|
1 |
def add(a, b):
|
2 |
+
"""
|
3 |
+
This function adds two numbers.
|
4 |
+
|
5 |
+
Arguments:
|
6 |
+
a (int): The first number to be added.
|
7 |
+
b (int): The second number to be added.
|
8 |
+
|
9 |
+
Returns:
|
10 |
+
int: Returns the sum of the two numbers.
|
11 |
+
|
12 |
+
Raises:
|
13 |
+
TypeError: If the input is not an integer.
|
14 |
+
"""
|
15 |
+
'\n This function adds two numbers.\n\n Arguments:\n a (int): The first number to be added.\n b (int): The second number to be added.\n\n Returns:\n int: Returns the sum of the two numbers.\n\n Raises:\n TypeError: If the input is not an integer.\n '
|
16 |
return a + b
|
17 |
|
18 |
def multiply(a, b):
|
19 |
+
"""
|
20 |
+
This function multiplies two given numbers.
|
21 |
+
|
22 |
+
Arguments:
|
23 |
+
a (int or float): The first number to be multiplied.
|
24 |
+
b (int or float): The second number to be multiplied.
|
25 |
+
|
26 |
+
Returns:
|
27 |
+
int or float: The product of the two numbers.
|
28 |
+
|
29 |
+
Raises:
|
30 |
+
TypeError: If the input types are not numbers (int or float).
|
31 |
+
|
32 |
+
"""
|
33 |
+
'\n This function multiplies two given numbers.\n\n Arguments:\n a (int or float): The first number to be multiplied.\n b (int or float): The second number to be multiplied.\n\n Returns:\n int or float: The product of the two numbers.\n\n Raises:\n TypeError: If the input types are not numbers (int or float).\n\n '
|
34 |
return a * b
|
35 |
|
36 |
def subtract(a, b):
|
37 |
+
"""
|
38 |
+
This function subtracts the second number from the first number.
|
39 |
+
|
40 |
+
Args:
|
41 |
+
a (int): The first number to be subtracted.
|
42 |
+
b (int): The second number to be subtracted from the first number.
|
43 |
+
|
44 |
+
Returns:
|
45 |
+
int: Returns the result of the subtraction.
|
46 |
+
|
47 |
+
Raises:
|
48 |
+
TypeError: If the input arguments are not integers.
|
49 |
+
"""
|
50 |
+
'\n This function subtracts the second number from the first number.\n\n Args:\n a (int): The first number to be subtracted.\n b (int): The second number to be subtracted from the first number.\n\n Returns:\n int: Returns the result of the subtraction.\n\n Raises:\n TypeError: If the input arguments are not integers.\n '
|
51 |
return a - b
|
52 |
|
53 |
def divide(a, b):
|
54 |
+
"""
|
55 |
+
This function divides the first argument by the second argument.
|
56 |
+
|
57 |
+
Arguments:
|
58 |
+
a (float): First argument to be divided.
|
59 |
+
b (float): Second argument by which the first argument is to be divided.
|
60 |
+
|
61 |
+
Returns:
|
62 |
+
float: Returns the result of the division of the first argument by the second argument.
|
63 |
+
|
64 |
+
Raises:
|
65 |
+
ValueError: If the second argument is zero, it raises a ValueError with the message 'Cannot divide by zero'.
|
66 |
+
"""
|
67 |
+
"\n This function divides the first argument by the second argument.\n\n Arguments:\n a -- First argument to be divided. It should be a float.\n b -- Second argument by which the first argument is to be divided. It should be a float.\n\n Returns:\n float -- Returns the result of the division of the first argument by the second argument.\n\n Raises:\n ValueError -- If the second argument is zero, it raises a ValueError with the message 'Cannot divide by zero'.\n "
|
68 |
if b == 0:
|
69 |
raise ValueError('Cannot divide by zero')
|
70 |
return a / b
|
71 |
|
72 |
def func(*args, **kwargs):
|
73 |
+
"""
|
74 |
+
This function is a decorator that wraps another function and returns a new function that acts as a wrapper.
|
75 |
+
|
76 |
+
Arguments:
|
77 |
+
func (function): The function to be wrapped.
|
78 |
+
* args (tuple): Positional arguments to be passed to the wrapped function.
|
79 |
+
* kwargs (dict): Keyword arguments to be passed to the wrapped function.
|
80 |
+
|
81 |
+
Returns:
|
82 |
+
A new function that acts as a wrapper for the original function.
|
83 |
+
|
84 |
+
Raises:
|
85 |
+
None
|
86 |
+
"""
|
87 |
+
'\nThis function is a decorator that wraps another function and returns a new function that acts as a wrapper.\n\nArgs:\n func (function): The function to be wrapped.\n\nKwargs:\n Any additional keyword arguments are passed to the wrapped function.\n\nReturns:\n A new function that acts as a wrapper for the original function.\n\nRaises:\n None\n'
|
88 |
+
|
89 |
def wrapper(*args, **kwargs):
|
90 |
+
"""
|
91 |
+
This function performs a specific operation on a list of integers.
|
92 |
+
|
93 |
+
Arguments:
|
94 |
+
n (int): The size of the list.
|
95 |
+
k (int): The value to be searched in the list.
|
96 |
+
|
97 |
+
Returns:
|
98 |
+
A list of integers representing the result of the operation.
|
99 |
+
|
100 |
+
Raises:
|
101 |
+
ValueError: If the input arguments are not integers.
|
102 |
+
"""
|
103 |
+
"\n This function is a wrapper that calls another function (specified by 'func') with the given arguments.\n\n Arguments:\n * args (tuple): Positional arguments to be passed to 'func'.\n * kwargs (dict): Keyword arguments to be passed to 'func'.\n\n Returns:\n Whatever 'func' returns.\n\n Raises:\n Whatever exceptions 'func' raises.\n "
|
104 |
return func(*args, **kwargs)
|
105 |
return wrapper
|