Embedded python in boost. Python Library

Source: Internet
Author: User

I don't need to talk much about it. I just need to look at an example to understand it.

// Testpython. cpp: defines the entry point of the console application. # Include <iostream> using namespace STD; # include <python. h> # include <boost/python. HPP> using namespace boost: Python; // use boost: Python namespace int _ tmain (INT argc, _ tchar * argv []) {py_initialize (); // initialize the python environment if (! Py_isinitialized () {cout <"------ Python initialization failed" <Endl; return 0;} object mainmodule; // main module object mainnamespace; // main namespace try {mainmodule = import ("_ main _"); // import _ main _ module mainnamespace = mainmodule. ATTR ("_ dict _"); // execute exec ("Import OS", mainnamespace, mainnamespace) directly; Exec ("Print OS. getcwd () ", mainnamespace, mainnamespace); // indirectly execute object mod = mainmodule. ATTR ("OS"); object Foo = Mod. ATTR ("Getcwd"); string dir = extract <string> (FOO (); cout <"cur dir:" <dir <Endl; mod = Mod. ATTR ("path"); Foo = Mod. ATTR ("isfile"); bool r = extract <bool> (FOO ("testpython. CPP "); cout <" file exsit: "<r <Endl;} catch (...) {If (pyerr_occurred () pyerr_print () ;}// py_finalize (); // do not call py_finalize because the boost library is not complete yet and global variables are not released yet. System ("pause"); Return 0 ;}

Brief description:

Import: import module.

Object: equivalent to pyobject, which is an encapsulation of the pyobject type. It can be understood as a omnipotent Python type.

Object: ATTR (char const *): obtains the attributes of a module.

Foo (): equivalent to a function call. The returned value is of the object type.

Extract: converts the object type to the corresponding C ++ type.


Embedded in Python, this is basically the case.

The following is a more comprehensive example:

// Testpython. cpp: defines the entry point of the console application. # Include <iostream> using namespace STD; # include <python. h> # include <boost/python. HPP> using namespace boost: Python; int _ tmain (INT argc, _ tchar * argv []) {py_initialize (); If (! Py_isinitialized () {cout <"------ Python initialization failed" <Endl; return 0 ;}try {object mainmodule = import ("_ main __"); // main module object mainnamespace = mainmodule. ATTR ("_ dict _"); // main namespace // E1. execute the method directly. Obtain the current path. // Run in an anonymous space, that is, the result is retained in mainnamespace. // you can obtain the result in the form of mainnamespace ["XX. Exec ("Import OS", mainnamespace, mainnamespace); // import the OS module exec ("Path = OS. getcwd () ", mainnamespace, mainnamespace); // obtain the current path exec (" Print 'cur path is: ', path ", mainnamespace, mainnamespace ); // print the current path // E2. obtain the PATH variable from the namespace Object Path = mainnamespace ["path"]; // obtain the variable path. String pathname = extract <string> (PATH); // convert to C ++ string type. Cout <"cur path is:" <pathname <Endl; // Note: Only the [] operator can be used to obtain elements of the dictionary type, table column, and list. // Other types, which throw an exception. // E3. indirect execution method. Obtain the current path. // The OS module can be obtained in the following three methods. // Object OS = import ("OS"); // directly import // object OS = mainmodule. ATTR ("OS"); // obtain the attribute from the main module: OS module object OS = mainnamespace ["OS"]; // retrieve the element from the dictionary: OS module object getcwd = OS. ATTR ("getcwd"); // get the getcwd method object result = getcwd (); // execute the getcwd method string dir = extract <string> (result ); // The Conversion Result is C ++-type cout <"cur path is:" <dir <Endl; // E4. determine whether the object isfile = OS exists. ATTR ("path "). ATTR ("isfile"); bool r = extract <bool> (isfi Le ("testpython. cpp"); cout <"file testpython. cpp exsit? "<R <Endl; // E5. operation list. Object sys = import ("sys"); // import the object paths = sys. ATTR ("path"); // retrieves the path attribute string path0 = extract <string> (paths [0]); // obtain the first cout element of path <"sys path0:" <path0 <Endl; // e6. use contains to determine whether the sequence contains any element. Bool haskey = bool (mainnamespace. contains ("OS"); // determines whether a is in cout <"mainnamespace haskey 'OS':" 


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.