Skip to content

Commit 4e270fd

Browse files
authored
Add files via upload
1 parent 6991327 commit 4e270fd

27 files changed

+1482
-0
lines changed

add_notes.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import re
3+
from api import note
4+
5+
def read_files(directory, file_extension='.py'):
6+
file_paths = []
7+
for root, dirs, files in os.walk(directory):
8+
for file in files:
9+
if file.endswith(file_extension):
10+
file_paths.append(os.path.join(root, file))
11+
return file_paths
12+
13+
def process_file(file_path):
14+
with open(file_path, 'r', encoding='utf-8') as file:
15+
content = file.read()
16+
processed_content = note(content)
17+
match = re.search(r'```(.*?)```', processed_content, re.DOTALL)
18+
if match:
19+
extracted_content = match.group(1).strip().split('\n', 1)[1]
20+
with open(file_path, 'w', encoding='utf-8') as file:
21+
file.write(extracted_content)
22+
23+
def main():
24+
directory = 'code'
25+
file_extension = '.py'
26+
file_paths = read_files(directory, file_extension)
27+
for file_path in file_paths:
28+
process_file(file_path)
29+
30+
main()

add_notes.txt

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
》》Write a Python program that does the following:
2+
3+
At the beginning of your program, specify the format for reading files (default is . py). Read all relevant files from the "code" folder in the current directory using utf-8 encoding and including any subdirectories containing such files.
4+
5+
Import the "api" package; pass each file read into a string to be processed by the api.note() function sequentially. Extract characters between "`````" (including "````) from the returned string, remove the first line of this extracted content and write it back in utf-8 encoding overwriting existing contents in corresponding files.
6+
7+
8+
```python
9+
import os
10+
import re
11+
from api import note
12+
13+
def read_files(directory, file_extension='.py'):
14+
file_paths = []
15+
for root, dirs, files in os.walk(directory):
16+
for file in files:
17+
if file.endswith(file_extension):
18+
file_paths.append(os.path.join(root, file))
19+
return file_paths
20+
21+
def process_file(file_path):
22+
with open(file_path, 'r', encoding='utf-8') as file:
23+
content = file.read()
24+
processed_content = note(content)
25+
match = re.search(r'```(.*?)```', processed_content, re.DOTALL)
26+
if match:
27+
extracted_content = match.group(1).strip().split('\n', 1)[1]
28+
with open(file_path, 'w', encoding='utf-8') as file:
29+
file.write(extracted_content)
30+
31+
def main():
32+
directory = 'code'
33+
file_extension = '.py'
34+
file_paths = read_files(directory, file_extension)
35+
for file_path in file_paths:
36+
process_file(file_path)
37+
38+
if __name__ == "__main__":
39+
main()
40+
```
41+
42+
This program implements the following functions:
43+
44+
1. At the beginning of the program, fill in the file format to be read from a text box (default is `.py`). Use UTF-8 encoding to read all files with the specified format from the "code" folder within the current directory and its subdirectories.
45+
46+
2. Import the 'api' package, pass each string obtained by reading into step 1 as an argument for the function api.note(). Retrieve a substring between two pairs of ```` ``` in the return value of this function call (excluding the first line), remove these matched characters from it using `utf-8` encoding and write them to corresponding files within their respective directories, replacing any existing content.
47+

api.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#本程序包含用于各项任务的提示词,由项目内其它程序调用,使用前需要在send函数填写自己使用的模型的api地址和api key。
2+
# python3
3+
# Please install OpenAI SDK first:`pip3 install openai`
4+
from openai import OpenAI
5+
6+
def send(system,user):
7+
client = OpenAI(api_key=" ", base_url="https://api.deepseek.com")
8+
9+
response = client.chat.completions.create(
10+
model="deepseek-coder",
11+
messages=[
12+
{"role": "system", "content":system},
13+
{"role": "user", "content":user},
14+
],
15+
stream=False
16+
)
17+
18+
return(response.choices[0].message.content)
19+
20+
def note(text):
21+
output=send("给以下代码添加详细的中文注释",text)
22+
return(output)
23+
24+
def describe(text):
25+
output=send("具体描述以下代码的功能",text)
26+
return(output)
27+
28+
def summarize(text):
29+
output=send("以下是一个程序源码中各文件的路径和功能描述,根据这些内容描述整个程序的功能和实现方法",text)
30+
return(output)
31+
32+
def question(text,question):
33+
output=send("以下是一个程序源码中各文件的路径和功能描述,根据这些内容回答后面的问题"+text,question)
34+
return(output)
35+
36+
def code(text):
37+
output=send("根据以下要求,写一个python程序:",text)
38+
return(output)
39+
40+
def code_check(text,code,result):
41+
output=send("以下python程序的功能要求为:"+text+"程序内容为:"+code+"运行结果为:"+result,"检查程序运行结果是否符合预期,如果确定运行结果不符合预期,请对程序进行修改。如果不确定运行结果是否符合预期,则回复不确定运行结果是否符合预期")
42+
return(output)
43+
44+
def code_error(code,result):
45+
output=send(code,"以上程序报错内容为:"+result+"尝试进行修改以修复这个问题")
46+
return(output)
47+
48+
def modify(question,text):
49+
output=send("根据以下要求,修改程序:"+question,"需要修改的程序如下:"+text)
50+
return(output)
51+
52+
def modify_check(text,code,result):
53+
output=send("以下python程序的修改要求为:"+text+"程序内容为:"+code+"运行结果为:"+result,"检查程序运行结果是否符合修改要求,如果确定运行结果不符合修改要求,请对程序进行再次修改。如果不确定运行结果是否符合修改要求,则回复不确定运行结果是否符合修改要求")
54+
return(output)

api_en.py

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#This program contains prompts for each task, which are called by other programs in the project, and you need to fill in the API address and API key of the model you are using in the send function before use.
2+
# python3
3+
# Please install OpenAI SDK first:`pip3 install openai`
4+
from openai import OpenAI
5+
6+
def send(system,user):
7+
client = OpenAI(api_key=" ", base_url="https://api.deepseek.com")
8+
9+
response = client.chat.completions.create(
10+
model="deepseek-coder",
11+
messages=[
12+
{"role": "system", "content":system},
13+
{"role": "user", "content":user},
14+
],
15+
stream=False
16+
)
17+
18+
return(response.choices[0].message.content)
19+
20+
def note(text):
21+
output=send("Add detailed comments to the following code",text)
22+
return(output)
23+
24+
def describe(text):
25+
output=send("Describe the functionality of the following code specifically",text)
26+
return(output)
27+
28+
def summarize(text):
29+
output=send("The following is a description of the path and function of each file in the source code of a program, and describes the function and implementation method of the whole program according to these contents",text)
30+
return(output)
31+
32+
def question(text,question):
33+
output=send("The following is a description of the path and function of each file in the source code of a program, and answer the following questions based on these contents"+text,question)
34+
return(output)
35+
36+
def code(text):
37+
output=send("Write a python program according to the following requirements:",text)
38+
return(output)
39+
40+
def code_check(text,code,result):
41+
output=send("The functional requirements of the following python program are: "+text+" program content is: "+code+" running result is: "+result," check whether the program running result meets expectations, if it is determined that the running result does not meet expectations, please modify the program. If you're not sure if the run results are as expected, reply that you're not sure if the run results are as expected")
42+
return(output)
43+
44+
def code_error(code,result):
45+
output=send(code,"The error message of the above program is: "+result+" tries to modify it to fix this problem")
46+
return(output)
47+
48+
def modify(question,text):
49+
output=send("According to the following requirements, modify the program: "+question," and the program that needs to be modified is as follows:"+text)
50+
return(output)
51+
52+
def modify_check(text,code,result):
53+
output=send("The following python program modification requirements are: "+text+" program content is: "+code+" running result is: "+result," check whether the program running result meets the modification requirements, if it is determined that the running result does not meet the modification requirements, please modify the program again. If you are not sure whether the run result meets the modification requirements, reply that you are not sure whether the run results meet the modification requirements")
54+
return(output)

code-test.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import os
2+
import subprocess
3+
import re
4+
5+
def read_input():
6+
user_input = input("Please enter your content: ")
7+
if not user_input:
8+
try:
9+
with open("question.txt", "r", encoding="utf-8") as file:
10+
user_input = file.read().strip()
11+
except FileNotFoundError:
12+
user_input = ""
13+
return user_input
14+
15+
def extract_code(response):
16+
match = re.search(r"```(.*?)```", response, re.DOTALL)
17+
if match:
18+
code_lines = match.group(1).strip().split('\n')[1:]
19+
return '\n'.join(code_lines)
20+
return None
21+
22+
def run_code(code_path):
23+
try:
24+
result = subprocess.check_output(["python", code_path], universal_newlines=True)
25+
except subprocess.CalledProcessError as e:
26+
result = str(e)
27+
return result
28+
29+
def save_code(code):
30+
with open("code.py", "w", encoding="utf-8") as file:
31+
file.write(code)
32+
33+
def main():
34+
text = read_input()
35+
if not text:
36+
print("The input is empty")
37+
return
38+
39+
import api_en
40+
response = api_en.code(text)
41+
code = extract_code(response)
42+
if not code:
43+
print("The code content was not matched")
44+
return
45+
46+
save_code(code)
47+
result = run_code("code.py")
48+
49+
for i in range(5):
50+
if "Error" in result:
51+
response = api_en.code_error(code, result)
52+
else:
53+
response = api_en.code_check(text, code, result)
54+
55+
new_code = extract_code(response)
56+
if not new_code:
57+
print(response)
58+
return
59+
60+
save_code(new_code)
61+
result = run_code("code.py")
62+
63+
if "Error" in result:
64+
print("The program does not work properly")
65+
else:
66+
print("Not sure if the program is as expected")
67+
68+
main()

code-test.txt

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
》》Write a Python program that does the following:
2+
1. In the command line, enter content. If you leave the input blank, read the contents of a question.txt file in UTF-8 encoding from the current directory and save it to the "text" string. If both your input and the contents of question.txt are empty, output "Input is empty" and end the program.
3+
2. Import the 'api' package; pass the content entered or read into api.code() function
4+
3. Strip off the first line from the returned code in a string by matching characters between two ``` in the api. code()'s return value using UTF-8 encoding, save this part to "code" and write it as code.py (if there is no existing file called `code.py` then create one; if already exists, replace its content)
5+
4. Run 'code.py' from current directory: If successful, capture the output into a string named result', and pass text, code, and result to api.code_check() function in this order (api.code_check(text, code, result)). Otherwise save error message of "code.py" execution failure as `result` then call api.code_error(code, result) passing arguments by same sequence
6+
5. Strip off the first line from ```...``` part found again and replace current content with it on 'code.py' (if does not exist create one; if exists overwrite its content). If no match is found output the returned value of last step.
7+
6. Repeat steps 4 & 5 for five times: After fifth execution, check whether code.py executed without error - print "Cannot be certain that program matches expected behavior." and end otherwise display message as per return from api.code_error() function (if it was called) or output contents in 'code.py' if no errors occurred. If the last operation resulted into an exception then terminate with a prompt saying: "Program cannot execute correctly"
8+
9+
10+
```python
11+
import os
12+
import subprocess
13+
import re
14+
15+
def read_input():
16+
user_input = input("Please enter your content: ")
17+
if not user_input:
18+
try:
19+
with open("question.txt", "r", encoding="utf-8") as file:
20+
user_input = file.read().strip()
21+
except FileNotFoundError:
22+
user_input = ""
23+
return user_input
24+
25+
def extract_code(response):
26+
match = re.search(r"```(.*?)```", response, re.DOTALL)
27+
if match:
28+
code_lines = match.group(1).strip().split('\n')[1:]
29+
return '\n'.join(code_lines)
30+
return None
31+
32+
def run_code(code_path):
33+
try:
34+
result = subprocess.check_output(["python", code_path], universal_newlines=True)
35+
except subprocess.CalledProcessError as e:
36+
result = str(e)
37+
return result
38+
39+
def save_code(code):
40+
with open("code.py", "w", encoding="utf-8") as file:
41+
file.write(code)
42+
43+
def main():
44+
text = read_input()
45+
if not text:
46+
print("The input is empty")
47+
return
48+
49+
import api
50+
response = api.code(text)
51+
code = extract_code(response)
52+
if not code:
53+
print("The code content was not matched")
54+
return
55+
56+
save_code(code)
57+
result = run_code("code.py")
58+
59+
for i in range(5):
60+
if "Error" in result:
61+
response = api.code_error(code, result)
62+
else:
63+
response = api.code_check(text, code, result)
64+
65+
new_code = extract_code(response)
66+
if not new_code:
67+
print(response)
68+
return
69+
70+
save_code(new_code)
71+
result = run_code("code.py")
72+
73+
if "Error" in result:
74+
print("The program does not work properly")
75+
else:
76+
print("Not sure if the program is as expected")
77+
78+
if __name__ == "__main__":
79+
main()
80+
```
81+
82+
This app implements the features you described. Make sure that the 'api' package is installed correctly and that the 'code', 'code_check', and 'code_error' functions are included in the package before running this program.

describe_source_code.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import os
2+
import fnmatch
3+
from api_en import describe
4+
5+
def find_files(directory, pattern):
6+
for root, dirs, files in os.walk(directory):
7+
for basename in files:
8+
if fnmatch.fnmatch(basename, pattern):
9+
filename = os.path.join(root, basename)
10+
yield filename
11+
12+
def main():
13+
file_format = '*.py' # 默认读取.py格式
14+
source_directory = 'code' # 读取当前目录下的code文件夹
15+
16+
for filepath in find_files(source_directory, file_format):
17+
with open(filepath, 'r', encoding='utf-8') as file:
18+
file_content = file.read()
19+
20+
description = describe(file_content)
21+
22+
txt_filepath = os.path.splitext(filepath)[0] + '.txt'
23+
with open(txt_filepath, 'w', encoding='utf-8') as txt_file:
24+
txt_file.write(description)
25+
26+
main()

0 commit comments

Comments
 (0)