日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
打通Python和C++之后?你懂的!

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

在克州等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、做網(wǎng)站 網(wǎng)站設(shè)計制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),全網(wǎng)整合營銷推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,克州網(wǎng)站建設(shè)費用合理。

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

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

Boost.Python

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

安裝

Boost的大部分功能都是以頭文件的形式提供的,無需安裝;但是也有少部分功能,需要進(jìn)行手動編譯。不幸,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  
  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  
  2. #include  
  3. #include  
  4. #include  
  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 item_prices; 
  15.     std::vector 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(prices[i]); 
  5.         double discount = extract(discounts[i]); 
  6.         item_prices.push_back(price); 
  7.         item_discounts.push_back(discount); 
  8.     } 

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

 
 
 
 
  1. BOOST_PYTHON_MODULE(person) 
  2.     class_("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++

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


新聞名稱:打通Python和C++之后?你懂的!
標(biāo)題路徑:http://www.dlmjj.cn/article/cogssdd.html