Free Open-source Python library description

Source: Internet
Author: User

The Python library is free and open-source. It can be transplanted to multiple operating systems. If it does not depend on the functions of the specified operating system, the Python program can run operations on different operating platforms, this is also one of the advantages that many programmers prefer.

The Boost C ++ Library provides libraries for threads, flexible pointers, operation rules, Python, and more. Many libraries in Boost have been submitted as future C ++ standard attachments. The Boost Python C ++ library is a good template library that allows you to encapsulate any C ++ class or function for Python library users. With this library, it is easier to create a system composed of a C ++ code model and a Python code combination.

First, download and install Boost. Although most of Boost content is included in the header file, the Python library must be compiled. Boost supports most popular compilers on the market, including Visual C ++ 6 and 7. Boost uses a JAM build system. Do not forget to download the JAM library ). For each compiler, you need to set build instructions step by step to build instruction ).

If you follow these instructions, the creation process will be fine. You also need to download and install Python. If you are using a Windows operating system, I strongly recommend that you use Python of ActiveState. After creating Boost, you also need to add the Boost directory to your include path and add the static library and dynamic library to your project.

Compile code in Python. extensions are implemented as DLL or shared libraries, so you need to create a DLL project. In our example project, the Python extension we created will generate a message box, and the text content of the message box can be changed at will.

This example is obviously of little practical value, but it will help you understand the main points of this article. In VC, first create a simple DLL project, and then add a class named CMsgBox to the project. Add a new constructor to this class. The constructor parameter is a standard string. Then, add a Show method to this class. This method has no parameters and its return value is void. The code you get is as follows:

 
 
  1. # Include<String> 
  2.  
  3. Typedefstd: string;
  4.  
  5. Class CmsgBox
  6.  
  7. {
  8.  
  9. String m_Text;
  10.  
  11. Public:
  12.  
  13. CMsgBox (void );
  14.  
  15. Virtual ~ CMsgBox (void );
  16.  
  17. CMsgBox (const string & text): m_Text (text ){}
  18.  
  19. Void Show (){
  20.  
  21. MessageBox (NULL, m_Text.c_str (), "Python", MB_ OK );
  22.  
  23. }
  24.  
  25. };
  26.  
  27. To allow Python to access this class, we need to add the following code:
  28.  
  29. # Include<Boost/Python/class_builder.hpp> 
  30.  
  31. NamespacePython=Boost: Python;
  32.  
  33. // Here is the code in the above example
  34.  
  35. ...
  36.  
  37. BOOST_PYTHON_MODULE_INIT (PythonDemo)
  38.  
  39. {
  40.  
  41. Python: module_builder mod ("PythonDemo ");
  42.  
  43. Python: class_builder<CMsgBox>Msgbox (mod, "CMsgBox ");
  44.  
  45. Msgbox. def (python: constructor<String>());
  46.  
  47. Msgbox. def (CMsgBox: Show, "Show ");
  48.  
  49. }

This is an executable extension module. The focus here is the BOOST_PYTHON_MODULE_INIT macro. This macro processes module initialization and provides Python with access to classes, constructors, and methods. Note: The DLL name and module name must be the same. After the DLL is created, copy it to The DLL Directory under your Python installation directory. The following describes how to use this extension in Python.

The above Code seems not neat, but you can get other additional gains. You can subclass in the Python Library) This CmsgBox class. If you are not familiar with Python, you generally cannot subclass extension classes in Python .)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.