自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

打通Python和C++之后?你懂的!

開發(fā) 后端
Python作為世界上最好的 膠水 語言(哼,世界上最好的語言當然是PHP==),利用Python的簡潔和C++的高效,基本可以解決99%的問題了吧~

Python作為世界上***的 膠水 語言(哼,世界上***的語言當然是PHP==),利用Python的簡潔和C++的高效,基本可以解決99%的問題了吧~

一般的,Python和C++的交互分為這兩種情況:

  1. 用C++擴展Python:當一個Python項目中出現(xiàn)了性能瓶頸時,將瓶頸部分抽離出來,用C++封裝成一個Python可以調(diào)用的模塊(so庫);
  2. 將Python內(nèi)嵌入C++:當一個C++項目中有部分功能預期將會經(jīng)常變更需求,期望獲得更高的靈活性時,將這部分功能用Python實現(xiàn),在C++中進行調(diào)用。這篇文章將簡單介紹下***部分的一種做法。

打通Python和C++

Boost.Python

Boost作為一個大寶庫,提供了我們所需要的這一功能。并且,在Boost的許多庫中,已經(jīng)默認使用了Boost.Python,所以也算是經(jīng)過了充分的測試。

安裝

Boost的大部分功能都是以頭文件的形式提供的,無需安裝;但是也有少部分功能,需要進行手動編譯。不幸,Boost.Python也是其中之一。

參照 Getting Started on Unix Variants 的第五部分內(nèi)容,即可安裝Boost.Python。安裝完成后,可以在相關(guān)目錄(我的是/usr/local/lib下)看到相關(guān)的so文件。

Hello World

用C++實現(xiàn)一個模塊,在Python中調(diào)用時,可以返回一個特定的字符串。

++

 

  1. #include <boost/python.hpp> 
  2.  
  3. char const* greet() 
  4.     return "hello, boost"
  5.  
  6. BOOST_PYTHON_MODULE(hello_boostpy) 
  7.     using namespace boost::python; 
  8.     def("greet", greet); 

太簡單了,代碼基本說明了一切~

將其編譯成動態(tài)鏈接庫的形式:

  1. g++ -I /usr/include/python2.7/ -fPIC -shared -o hello_boostpy.so hello_boostpy.cc -lboost_python 

這時可以使用ldd看看hello_boostpy.so可不可以找到libboost_python,找不到的話,需要手動將其路徑加入環(huán)境變量LD_LIBRARY_PATH中,或者用ldconfig相關(guān)的命令也可以。

接下來就可以在Python中使用hello_boostpy庫了:

 

  1. # -*- coding: utf-8 -*- 
  2. import sys 
  3. sys.path.append('.'
  4.  
  5.  
  6. def test(): 
  7.     import hello_boostpy 
  8.     return hello_boostpy.greet() 
  9.  
  10.  
  11. if __name__ == "__main__"
  12.     print test() 

Expose Class

接下來,我們在C++實現(xiàn)的模塊中,添加一個類,并且嘗試向C++方向傳入Python的list類型對象。

C++類:

++

 

  1. #include <boost/python.hpp> 
  2. #include <vector> 
  3. #include <string> 
  4. #include <sstream> 
  5. using namespace boost::python; 
  6.  
  7. struct Person 
  8.     void set_name(std::string name) { this->name = name; } 
  9.     std::string print_info(); 
  10.     void set_items(list& prices, list& discounts); 
  11.      
  12.      
  13.     std::string name
  14.     std::vector<double> item_prices; 
  15.     std::vector<double> item_discounts; 
  16. }; 

其中,Python方的list類型,在Boost.Python中有一個對應(yīng)的實現(xiàn)boost::python::list(相應(yīng)的,dict、tuple等類型都有對應(yīng)實現(xiàn))。在set_items中,我們將會用boost::python::extract對數(shù)據(jù)類型做一個轉(zhuǎn)換。

++

 

  1. void Person::set_items(list& prices, list& discounts) 
  2.     for(int i = 0; i < len(prices); ++i) 
  3.     { 
  4.         double price = extract<double>(prices[i]); 
  5.         double discount = extract<double>(discounts[i]); 
  6.         item_prices.push_back(price); 
  7.         item_discounts.push_back(discount); 
  8.     } 

Python模塊定義部分依舊是非常直觀的代碼:

 

  1. BOOST_PYTHON_MODULE(person) 
  2.     class_<Person>("Person"
  3.         .def("set_name", &Person::set_name) 
  4.         .def("print_info", &Person::print_info) 
  5.         .def("set_items", &Person::set_items) 
  6.     ;    

在Python代碼中,就可以像使用一個Python定義的類一樣使用Person類了:

 

  1. # -*- coding: utf-8 -*- 
  2. import sys 
  3. sys.path.append('.'
  4.  
  5.  
  6. def test(): 
  7.     import person 
  8.     p = person.Person() 
  9.     p.set_name('Qie'
  10.     p.set_items([100, 123.456, 888.8], [0.3, 0.1, 0.5]) 
  11.     print p.print_info() 
  12.  
  13.  
  14. if __name__ == "__main__"
  15.     test() 

Py++

上面的模塊封裝過程,看上去還是有些枯燥,有不少地方都是重復的工作。那么可不可以自動的進行呢?Py++提供了這樣的能力,它可以幫你自動生成Boost.Python的相關(guān)代碼,對于接口數(shù)量比較多的模塊來說,可以極大的減少工作量,也減少了出錯的概率。具體使用方法,可以參見 Tutorial

責任編輯:未麗燕 來源: 一根笨茄子
相關(guān)推薦

2009-10-22 09:17:16

C++ CLR

2023-07-17 10:28:00

C/C++編程接口

2021-07-23 16:30:36

PythonC++代碼

2018-05-15 11:14:07

面試官C++編程

2011-04-11 09:43:25

C++C

2019-01-21 09:02:03

C++Python編程語言

2021-03-26 10:35:49

C++Python編程語言

2014-01-24 09:49:01

C++指針

2024-02-26 07:26:27

RustC++開發(fā)

2024-05-15 16:01:04

C++編程開發(fā)

2022-07-01 11:56:54

C語言C++編程語言

2011-05-18 18:05:47

C#C++

2010-01-28 15:22:12

C++嵌套類

2009-09-16 14:56:23

C++

2021-02-26 10:41:59

C++程序員代碼

2011-05-18 17:56:38

C#C++

2009-09-04 17:34:11

C#CC++

2020-06-17 12:22:44

C覆蓋重載

2011-04-06 08:57:07

C++java多態(tài)

2019-07-29 10:39:39

前端性能優(yōu)化緩存
點贊
收藏

51CTO技術(shù)棧公眾號