Skip to content

Commit adc4092

Browse files
committed
adding some explanation in the README.md
1 parent 4eeba40 commit adc4092

File tree

3 files changed

+63
-40
lines changed

3 files changed

+63
-40
lines changed

README.md

+34-10
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,56 @@ are stored in the form of .cpp and .exe files.
2525
```
2626
python3 python2cppconverter.py
2727
```
28+
29+
If the generated C++ code got compiled, test it with
30+
```
31+
./simplePythonScript.exe
32+
```
33+
You hopefully get the same output as when running
34+
```
35+
python3 simplePythonScript.py
36+
```
37+
Check how much faster you are now ...
38+
```
39+
time ./simplePythonScript.exe
40+
time python3 simplePythonScript.py
41+
```
42+
2843
### Example Code Generation:
29-
Python Snippet
44+
Python Snippet:
3045
```python
31-
def print_something():
32-
print("Hello Cpp file")
46+
def add_something(x, y):
47+
print("casually adding some stuff together")
48+
z = x + y
49+
return z
3350

3451

3552
if __name__ == "__main__":
36-
print_something()
53+
print('Okay, lets go')
54+
print(add_something(5, 2))
3755
```
38-
Converted by Codex to:
56+
This is how your CODEX conversion may look like:
3957
```cpp
4058
// C++ Code generated from Python Code:
4159
#include <iostream>
42-
#include <string>
43-
4460
using namespace std;
4561

46-
void print_something() {
47-
cout << "Hello Cpp file" << endl;
62+
int add_something(int x, int y) {
63+
cout << "casually adding some stuff together" << endl;
64+
int z = x + y;
65+
return z;
4866
}
4967

5068
int main() {
51-
print_something();
69+
cout << "Okay, lets go" << endl;
70+
cout << add_something(5, 2) << endl;
5271
return 0;
5372
}
5473
```
5574
75+
76+
Please test your generated code before usage.
77+
## Credits
78+
79+
This project is based on the OpenAI Codex project.
5680
Inspired by https://github.com/tom-doerr

python2cppconverter.py

+23-27
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
import random
88

99

10-
1110
MAX_SUPPORTED_INPUT_LENGTH = 4096
1211
USE_STREAM_FEATURE = True
1312
SET_TEMPERATURE_NOISE = False
14-
MAX_TOKENS_DEFAULT = 64
13+
MAX_TOKENS_DEFAULT = 128
1514

1615
STREAM = True
1716
API_KEYS_LOCATION = "./config"
@@ -51,19 +50,15 @@ def initialize_openai_api():
5150

5251

5352
def create_input_prompt(length=3000):
54-
input_prompt = ''
55-
files_sorted_by_mod_date = sorted(os.listdir('.'), key=os.path.getmtime)
56-
# Reverse sorted files.
57-
files_sorted_by_mod_date = files_sorted_by_mod_date[::-1]
58-
for filename in files_sorted_by_mod_date:
59-
if filename == PYTHON_FILE_TO_CONVERT:
60-
with open(filename) as f:
61-
input_prompt += '\n===================\n# ' + filename + ':\n'
62-
input_prompt += f.read() + '\n'
53+
inputPrompt = ''
54+
filename = PYTHON_FILE_TO_CONVERT
55+
with open(filename) as f:
56+
inputPrompt += '\n===================\n# ' + filename + ':\n'
57+
inputPrompt += f.read() + '\n'
6358

64-
input_prompt = input_prompt[:length]
65-
input_prompt += '\n\n===================\n# ' + 'C++ Code:' + '\n'
66-
return input_prompt
59+
inputPrompt = inputPrompt[:length]
60+
inputPrompt += '\n\n===================\n# ' + 'C++ Code:' + '\n'
61+
return inputPrompt
6762

6863

6964
def generate_completion(input_prompt, num_tokens):
@@ -77,14 +72,14 @@ def generate_completion(input_prompt, num_tokens):
7772

7873

7974
def get_generated_response(response):
80-
generated_file = "// C++ Code generated from Python Code: \n"
75+
generatedFile = "// C++ Code generated from Python Code: \n"
8176
while True:
82-
next_response = next(response)
83-
completion = next_response['choices'][0]['text']
84-
generated_file = generated_file + completion
85-
if next_response['choices'][0]['finish_reason'] is not None:
77+
nextResponse = next(response)
78+
completion = nextResponse['choices'][0]['text']
79+
generatedFile = generatedFile + completion
80+
if nextResponse['choices'][0]['finish_reason'] is not None:
8681
break
87-
return generated_file
82+
return generatedFile
8883

8984

9085
def write_cpp_file(textResponse):
@@ -96,21 +91,21 @@ def write_cpp_file(textResponse):
9691
f.close()
9792

9893

99-
def test_cpp_compilation(cpp_file):
94+
def test_cpp_compilation(cppFile):
10095
"""
10196
Checks if the generated file is compilable using g++
10297
"""
103-
exe_file = cpp_file.split(".")[0] + ".exe"
104-
if os.system("g++ " + cpp_file + " -o " + exe_file + " &> /dev/null") == 0:
98+
exeFile = cppFile.split(".")[0] + ".exe"
99+
if os.system("g++ " + cppFile + " -o " + exeFile + " &> /dev/null") == 0:
105100
return True
106101
else:
107102
return False
108103

109104

110-
def iterate_for_compilable_solution(prompt, max_iterations):
105+
def iterate_for_compilable_solution(prompt, maxIterations):
111106
print('Iterating for a compilable C++ solution ...')
112107
print()
113-
for it in range(max_iterations):
108+
for it in range(maxIterations):
114109
response = generate_completion(prompt, num_tokens=MAX_TOKENS_DEFAULT)
115110
textResponse = get_generated_response(response)
116111
write_cpp_file(textResponse)
@@ -122,14 +117,15 @@ def iterate_for_compilable_solution(prompt, max_iterations):
122117
print("C++ File: {}".format(fileName + ".cpp"))
123118
print("Compiled Executable: {}".format(fileName + ".exe"))
124119
break
125-
if it == max_iterations - 1:
120+
if it == maxIterations - 1:
126121
print('Unfortunately CODEX did not find a compilable solution. Still you can find the generated code '
127122
'in the file: {}'.format(fileName + ".cpp"))
128123

129124

130125
if __name__ == "__main__":
131126
initialize_openai_api()
132127
prompt = create_input_prompt()
133-
iterate_for_compilable_solution(prompt=prompt, max_iterations=5)
128+
#print(prompt)
129+
iterate_for_compilable_solution(prompt=prompt, maxIterations=5)
134130

135131

simplePythonScript.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
def print_something():
2-
print("Hello Cpp file")
1+
def add_something(x, y):
2+
print("casually adding some stuff together")
3+
z = x + y
4+
return z
35

46

57
if __name__ == "__main__":
6-
print_something()
8+
print('Okay, lets go')
9+
print(add_something(5, 2))

0 commit comments

Comments
 (0)