You can use this project to combine c++ code and python code in one file.
pip install combina_cpp_and_py
Hypothesis that you have a cpp file named "test.cpp" and a python file named "test.py".
import combina_cpp_and_py
with open('test.cpp', 'r') as f:
cpp_code = f.read()
with open('test.py', 'r') as f:
py_code = f.read()
new_code = combina_cpp_and_py.combine(cpp_code, py_code)
with open('result.cpp', 'w') as f:
f.write(new_code)
with open('result.py', 'w') as f:
f.write(py_code)
You will get a new cpp file named "result.cpp" and "result.py" which contains the combined c++ code and python code.
#if false
r"""
#endif
{$cppcode}
#if false
"""
{$pythoncode}
#endif
The code will be combined in the following way:
1. The c++ code will be placed in the {$cppcode} block.
2. The python code will be placed in the {$pythoncode} block.
3. When run c++, the {$pythoncode} block will be ignored due to "#if false" and "#endif".
4. When run python, the {$cppcode} block will be ignored due to "#" and " r"""""" ".
Don't use the code like the result code, or there will be ERROR due to special statement.