1. How to realize

This project is a GitHub open source project and is very easy to use. Just download it, install the dependencies, and run the corresponding Python script.

python3 python2cppconverter.py

Take the following Python code as an example

def add_something(x, y):
    print("casually adding some stuff together")
    z = x + y
    return z


if __name__ == "__main__":
    print('Okay, lets go')
    print(add_something(5, 2))

The conversion to C++ code is as follows.

// C++ Code generated from Python Code: 
#include <iostream>
using namespace std;

int add_something(int x, int y) {
    cout << "casually adding some stuff together" << endl;
    int z = x + y;
    return z;
}

int main() {
    cout << "Okay, lets go" << endl;
    cout << add_something(5, 2) << endl;
    return 0;
}

python2cppconverter.py actually calls OpenAI's API to do the code conversion. The core code is as follows.

openai.Completion.create(engine='code-davinci-002', 
  prompt=input_prompt, 
  temperature=temperature,
  max_tokens=num_tokens, 
  stream=STREAM, 
  stop='===================\n',
  top_p=1.0, 
  frequency_penalty=0.0, 
  presence_penalty=0.0)

is essentially a function call, where the engage parameter is a code transformation model.

Before introducing code-davinci-002, let's understand the GPT-3 model.

GPT-3 is a model released by OpenAI in May 2020. This model contains 175 billion parameters, which is two orders of magnitude more than GPT-2 and a dramatic improvement over GPT-2.

GPT-3 achieves robust performance on many NLP datasets, including translation, question-and-answer, and completion tasks, as well as some tasks that require on-the-fly reasoning or domain adaptation, such as deciphering words or performing arithmetic operations.

The code-davinci-002 model is a descendant of GPT-3, and its training data contains natural language and billions of lines of public code from GitHub. As a result, it understands and generates code and is fluent in more than a dozen programming languages, with the most expertise in Python.

Therefore, the code-davinci-002 model not only enables interconversion between programming languages, but also interconversion with natural languages, finding bugs in the code, writing documentation based on the code, and so on.

For example, the following example generates a textual description of the code function

python2cppconverter.py

Code to Natural Language

Take a look at the following example to generate docstring for Python code

python2cppconverter.py

docstring

For other examples, you can see the official website of OpenAI.

Related articles

Getting Started with Python Unit Testing Framework Pytest

Unit testing is the most important tool for code correctness verification, and the most important part of system testing. It is also the only test method that requires code to be written for testing.Getting Started with Python Unit Testing Framework Pytes

Installation and use of PyMuPDF

Before introducing PyMuPDF, let's learn about MuPDF, as you can see from the naming form, PyMuPDF is the Python interface form of MuPDF.

pandasql example

pandasql is a small but powerful package with only 358 lines of code. pandasql's idea is to let Python run SQL

25 Python text processing examples

Python processing text is a very common function, and this article has compiled a variety of text extraction and NLP-related examples

7 efficiency tools for python

In order to improve efficiency, we often use some Python efficiency tools in our usual work. As a relatively old programming language, Python can automate various kinds of daily work.

Python Visualization Tool for Climate Researchers

cf-plot is a data visualization tool for climate researchers that can plot common contour, vector and line plots such as projection maps, multinomial graphs, Hovmuller plots, and trajectory plots.

requests-cache tutorial

requests-cache, an extension of the requests library, allows us to easily cache requests and get the corresponding crawl results directly.