Method, device, equipment and medium for SDK to export python interface

By automatically binding the SDK's C++ interface to a Python interface and generating static and dynamic library files, the problem of manual coding and binding of C++ interfaces, which is labor-intensive and error-prone, is solved, and an efficient and error-free SDK interface export process is achieved.

CN115718594BActive Publication Date: 2026-05-15PANDA ELECTRONICS +1
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
PANDA ELECTRONICS
Filing Date
2022-11-22
Publication Date
2026-05-15

AI Technical Summary

Technical Problem

In existing technologies, when using the Python programming language to call SDK interfaces, it is necessary to manually code and bind the C++ interface, which is labor-intensive and prone to errors.

Method used

By generating a list of STL standard template libraries and a list of C++ classes associated with Python types, the SDK's C++ interfaces are automatically bound and compiled into static and dynamic library files, avoiding modifications to the original C++ code. The tuple type is used to handle reference parameters, enabling incremental compilation.

Benefits of technology

It improves the efficiency of exporting Python interfaces to the SDK, reduces development workload, lowers the risk of errors, and facilitates later maintenance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115718594B_ABST
    Figure CN115718594B_ABST
Patent Text Reader

Abstract

The application discloses a method, device and equipment for SDK to export a Python interface and a medium, the method comprising the following steps: creating an interface class of Python according to a C++ interface of SDK and binding the interface class, generating an STL standard template library list and a C++ class list associated with a Python type; performing STL standard template library binding according to the STL standard template library list; performing C++ interface parameter class binding according to the C++ class list; and compiling all generated files and linking the files into a library file for output. The application can improve the efficiency of the SDK to export the Python interface, reduce the development workload, is not prone to errors, and is relatively convenient for adding an interface or maintenance in the later period.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention belongs to the field of software development technology and relates to methods, apparatus, devices and media for exporting Python interfaces using SDKs. Background Technology

[0002] On computers, servers, or other devices, the functions provided by various SDKs can be easily implemented through SDK (Software Development Kit) interface calls. Currently, the user base for the Python programming language is very large, and supporting this language allows users to easily utilize the functions provided by the SDK using its Python interface. Typically, supporting the Python interface of an SDK is based on interfaces from other open languages, such as C++. Manually coding and binding these interfaces one by one is undoubtedly a huge workload, time-consuming, labor-intensive, and prone to errors. Summary of the Invention

[0003] Purpose of the invention: The purpose of this invention is to provide a method, apparatus, device, and medium for exporting Python interfaces from an SDK, which can improve the efficiency of exporting Python interfaces from an SDK and avoid the problems that are easy to make mistakes in manual programming.

[0004] Technical solution: In a first aspect, the present invention provides a method for exporting a Python interface using an SDK, comprising:

[0005] Create Python interface classes based on the SDK's C++ interface and bind them to them, generating a list of STL standard template libraries and a list of C++ classes associated with Python types;

[0006] Bind to the STL Standard Template Library based on the STL Standard Template Library list;

[0007] Bind C++ interface parameter classes based on a list of C++ classes;

[0008] Compile all generated files and link them into a library file for output.

[0009] Furthermore, during interface binding, for interfaces that use references as parameters, the return value is set to a tuple type, and the reference parameter is returned as a member of the tuple.

[0010] Furthermore, during STL standard template library binding, the generated STL standard template library list is traversed, and a separate template file is created for each template type to export the STL standard template library. Creating a file for each class avoids excessive resource consumption during the compilation process.

[0011] Furthermore, during C++ interface parameter class binding, the generated list of C++ classes is traversed, and a separate code file is created for each C++ class, exporting the C++ class. Creating a file for each class avoids excessive resource consumption during the compilation process.

[0012] By creating a separate template file for each STL template type and a separate code file for each C++ class, we can avoid memory shortages caused by excessive resource consumption during the compilation process. At the same time, we can compile the corresponding files separately when certain classes change, achieving incremental compilation and reducing compilation time.

[0013] Furthermore, all generated files are stored in the SDK directory without modifying the original C++ interfaces or classes, thus avoiding the need to modify and maintain the original code.

[0014] Furthermore, all generated files are compiled and linked as library files for output, including: compiling and statically linking all generated files into static library files, then extracting all .o object files from the static library file and all dependent static library files, and finally linking all .o files into .so dynamic link libraries through dynamic linking.

[0015] A second aspect of the present invention provides an apparatus for exporting a Python interface using an SDK, comprising:

[0016] The interface binding module creates Python interface classes based on the SDK's C++ interfaces and binds them to them;

[0017] The STL Standard Template Library binding module is used to generate a list of STL Standard Template Library (STL) types associated with Python types, and to perform STL Standard Template Library (STL) binding based on this list.

[0018] The C++ interface parameter class binding module is used to generate a list of C++ classes associated with Python types, and to bind C++ interface parameter classes based on this list of C++ classes.

[0019] The compilation and library file generation module is used to compile all generated files and link them into library files for output.

[0020] A third aspect of the present invention provides a device for exporting a Python interface via an SDK, comprising a processor and a memory;

[0021] The memory is used to store computer programs;

[0022] The processor is configured to execute the computer program to implement the method described in the first aspect above.

[0023] A fourth aspect of the present invention provides a computer-readable storage medium for storing a computer program, which, when executed by a computer, implements the method of the first aspect described above.

[0024] The fifth aspect of the present invention provides a computer program product that, when run on a computer, causes the computer to perform the method described in the first aspect.

[0025] Beneficial effects: Compared with the prior art, the present invention has the following significant advantages: it can improve the efficiency of exporting Python interfaces to SDK, reduce the workload of development, reduce errors, and make it more convenient to add interfaces or maintain them later. Attached Figure Description

[0026] To more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings used in the embodiments of the present invention will be briefly introduced below. Obviously, the drawings described below are only embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0027] Figure 1 This is a diagram of the device module structure for exporting the Python interface from the SDK;

[0028] Figure 2 This is a diagram of the SDK folder directory;

[0029] Figure 3 It is a flowchart of the compilation and library file generation process. Detailed Implementation

[0030] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are not all the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the protection scope of the present invention.

[0031] This application provides a method for exporting an SDK to Python. Based on existing C++ language calling interfaces, the method first binds and exports the existing C++ interface into an interface that can be used by Python. Then, the STL standard template library and C++ interface parameter classes associated with the interface are also bound and exported into classes in Python for easy use by Python users. Finally, the generated code is compiled, template files are extracted from the static compilation library file, and dynamically compiled into a .so format Python library.

[0032] The following section provides a detailed introduction to the methods for exporting the Python interface from this SDK.

[0033] Figure 2 The diagram shows the SDK folder directory, which includes the header file directory 21, the library file directory 22, the STL standard template library binding directory 23, the C++ interface parameter class binding directory 24, and the C++ source code directory 25.

[0034] Methods for exporting Python interfaces from the SDK include:

[0035] Interface binding involves creating Python interface classes based on the SDK's C++ interfaces and binding them to these classes. This process generates a list of STL (Standard Template Library) classes associated with Python types and a list of C++ classes associated with Python types. Specifically, generating the list of STL classes associated with Python types involves binding and exporting types defined in C++, such as vector and map, to Python types. Generating the list of C++ classes associated with Python types involves automatically binding and exporting the C++ classes associated with the interfaces to Python types.

[0036] The default C++ interface and SDK core code are located in the C++ source code directory 25. After locating all interfaces to be exported, the software program creates Python function interface bindings according to the format requirements of third-party Python binding libraries such as Boost.Python and pybind11. In this embodiment, pybind11 is used.

[0037] When binding an interface, for interfaces that use references as parameters, since Python does not support reference parameters, in order to pass the reference to Python for use, the return value is set to a tuple type, and the reference parameter is returned as a member of the tuple. In this way, Python can obtain the result value of the parameter from the tuple.

[0038] The following is an example of an interface that returns a tuple type:

[0039]

[0040] STL Standard Template Library (STL) binding is performed based on the STL STL list. During STL binding, the software program automatically traverses the generated STL STL list, creates a separate template file for each template type, and exports the STL STL. Creating a file for each class avoids excessive resource consumption during the compilation process. The generated files are placed in the STL STL binding directory 23.

[0041] C++ interface parameter class binding is performed based on a list of C++ classes. During binding, the generated list of C++ classes is traversed, and a separate code file is created for each class, exporting the class. Creating a file for each class avoids excessive resource consumption during compilation. The generated files are placed in the C++ interface parameter class binding directory 24.

[0042] All the generated files are stored in the SDK directory without modifying the original C++ interfaces or classes, thus avoiding the need to modify and maintain the original code.

[0043] Compile and generate library files; compile all generated files and link them into a library file for output.

[0044] Figure 3 The diagram shows the compilation and library file generation process. During compilation and library file generation, all generated files are first compiled and statically linked into static library files. Figure 3 The SDK static library 11 shown is then used, along with all dependent static library files. Figure 3 The SDK depends on a static library (12), from which all .o object files are extracted to obtain the SDK's .o object file set (13). Finally, all .o files are linked into a .so dynamic link library using dynamic linking, and the generated dynamic link library file is placed in the library file directory (22). This method of extracting template files from the static library avoids the need for recompilation of the files.

[0045] This application also provides an apparatus for exporting a Python interface using an SDK, such as... Figure 1 As shown, the device includes:

[0046] Interface binding module 1 creates Python interface classes based on the SDK's C++ interfaces and binds them to them;

[0047] STL Standard Template Library Binding Module 2 is used to generate a list of STL Standard Template Libraries associated with Python types, and to perform STL Standard Template Library binding based on this list.

[0048] C++ interface parameter class binding module 3 is used to generate a list of C++ classes associated with Python types, and to bind C++ interface parameter classes based on this list of C++ classes;

[0049] Module 4, which compiles and generates library files, is used to compile all generated files and link them as library files for output.

[0050] This application also provides a device for exporting a Python interface using an SDK, including a processor and a memory;

[0051] The memory is used to store computer programs;

[0052] The processor is configured to execute the computer program to implement the method described in the above embodiments.

[0053] This application also provides a computer-readable storage medium for storing a computer program, which, when executed by a computer, implements the methods described in the above embodiments. The computer storage medium can be, for example, a USB flash drive, a portable hard drive, a ROM memory, a RAM memory, a magnetic disk, or an optical disk, or any other medium suitable for storing computer instructions.

[0054] This application also provides a computer program product that, when run on a computer, causes the computer to execute the methods described in the above embodiments.

[0055] The method provided in this application can automatically perform interface binding, automatically generate related class interfaces, and generate library files, which can improve the efficiency of exporting Python interfaces using the SDK and is less prone to errors. The apparatus, device, computer-readable storage medium, and computer program product provided in this application are all used to execute the method provided above, and therefore have the same beneficial effects as the method.

[0056] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the technical scope disclosed in the present invention should be included within the scope of protection of the present invention. Therefore, the scope of protection of the present invention should be determined by the scope of the claims.

Claims

1. A method for exporting Python interfaces using an SDK, characterized in that, include: Create Python interface classes based on the SDK's C++ interface and bind them, generating a list of STL standard template libraries and a list of C++ classes associated with Python types; generating the list of STL standard template libraries associated with Python types involves binding and exporting types defined in C++ as Python types; generating the list of C++ classes associated with Python types involves automatically binding and exporting the C++ classes associated with the interface as Python types. Bind to the STL Standard Template Library based on the STL Standard Template Library list; Bind C++ interface parameter classes based on a list of C++ classes; The process involves compiling all generated files and linking them into a library file, including: compiling and statically linking all generated files into a static library file; extracting all .o object files from this static library file and all dependent static library files; and finally linking all .o files into a .so dynamic link library.

2. The method according to claim 1, characterized in that, When binding an interface, for interfaces that use references as parameters, set the return value to a tuple type and return the reference parameter as a member of the tuple.

3. The method according to claim 1, characterized in that, When binding to the STL Standard Template Library, the generated list of STL Standard Template Library is traversed, a separate template file is created for each template type, and the STL Standard Template Library is exported.

4. The method according to claim 1, characterized in that, When binding C++ interface parameter classes, iterate through the generated list of C++ classes, create a code file for each C++ class, and export the C++ class.

5. The method according to claim 1, characterized in that, All generated files are stored in the SDK directory, without modifying the original C++ interfaces or classes.

6. A device for exporting a Python interface using an SDK, characterized in that, include: The interface binding module creates Python interface classes based on the SDK's C++ interfaces and binds them to them; The STL Standard Template Library binding module is used to generate a list of STL Standard Template Library (STL) types associated with Python types, and to perform STL Standard Template Library (STL) binding based on this list. Generate a list of STL standard template libraries with associated Python types by binding and exporting types defined in C++ as Python types; The C++ interface parameter class binding module is used to generate a list of C++ classes associated with Python types, and to bind C++ interface parameter classes based on this list of C++ classes. Generating a list of C++ classes associated with Python types involves automatically binding and exporting the C++ classes associated with the interface as Python types. The compilation and library file generation module is used to compile all generated files and link them into library files for output. This includes: compiling all generated files and statically linking them into static library files; then extracting all .o object files from the static library file and all dependent static library files; and finally linking all .o files into .so dynamic link libraries through dynamic linking.

7. A device whose SDK exports a Python interface, characterized in that, Including processor and memory; The memory is used to store computer programs; The processor is configured to execute the computer program to implement the method as described in any one of claims 1 to 5.

8. A computer-readable storage medium, characterized in that, Used to store a computer program, which, when executed by a computer, implements the method according to any one of claims 1 to 5.

9. A computer program product, characterized in that, When the computer program product is run on a computer, it causes the computer to perform the method of any one of claims 1 to 5.