More Related Content
Similar to Boost.python (20)
Boost.python
- 3. 目次 自己紹介 Pythonについて Pythonとは Boost::Pythonの使い方 C++からPythonへ PythonからC++へ Boost::Pythonの有能性 まとめ
- 4. 自己紹介 Twitter ID: fate_fox 本名:安江 直樹 職業:高校3年生 主な言語:C ,C++ , Python etc… 興味がある分野:自然言語処理,人工知能,グリッドコンピューティング,クラウド,並列処理 etc….
- 14. Pythonを知らない人のために Pythonは 覚えやすく使いやすい! 覚えやすいように文法がシンプル! 曖昧さをなくすためにインデントなどにうるさい! 豊富なライブラリやモジュールで機能を拡張できる! Guidovan Rossum氏が開発したインタプリタ言語! ソースが読みやすい! 他言語より予約語(31語)が少ない!「Python2.6より」
- 15. Pythonで有名な物 Google App Enginen Googleが提供するWebアプリケーション開発環境 Xen Pythonが使われた1台で複数台のOSが稼働できる有名な仮想ソフトウェア(OCamlも使われてるらしい さすが関数型帝国!) MAYA CGを作るアプリケーション 機能などの拡張にPythonを使用 Zope/Plone Webアプリケーション鯖 Pythonで書かれてます。 Django Webフレームワーク Zopeと似てる
- 25. // sample1.cpp //関数のラッピング #define BOOST_PYTHON_STATIC_LIB // Boost.Pythonライブラリを静的にリンクする #include <string> #include <boost/python.hpp> std::string hello_world(void) { return "Hellow, world!"; } BOOST_PYTHON_MODULE(sample1) // sample1はpython拡張Moduleの名前になる { // C++のhello_world関数を、" hello_world_python"という名前でPythonに公開する boost::python::def ("hello_world_python", &hello_world); }
- 26. Python 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import sample1 >>> dir (sample1) ['__doc__', '__file__', '__name__', '__package__', 'hello_world_python'] >>> sample1.hello_world_python <Boost.Python.function object at 0x00E43990> >>> sample1.hello_world_python() 'Hellow, world!' >>> print sample1.hello_world_python() Hellow, world! >>>
- 27. // sample2.cpp //クラスのラッピング #define BOOST_PYTHON_STATIC_LIB / / Boost.Pythonライブラリを静的にリンクする #include <boost/python.hpp> structCValue { intgetValue(void) const { return value; } void setValue(int value) { this->value = value; } int value; }; BOOST_PYTHON_MODULE(sample2) // sample2はpython拡張モジュールの名前になる { // C++のCValueクラス(構造体)を、”PValue”という名前で pythonに公開する boost::python::class_<CValue>("PValue") .def(“get”, &CValue::getValue) ; // getValueメンバ関数を、”get”という名前のメソッド として公開する .def("set", &CValue::setValue); // setValueメンバ関数を、”set”という名前のメソッド として公開する }
- 28. // sample3.cpp //C++のアクセサをPythonのプロパティに公開 #define BOOST_PYTHON_STATIC_LIB // Boost.Pythonライブラリを静的にリンクする #include <boost/python.hpp> structCValue { intgetValue(void) const { return value; } void setValue(int value) { this->value = value; } int value; }; BOOST_PYTHON_MODULE(sample3) // sample3は python拡張モジュールの名前になる { // C++のCValueクラス(構造体)を、”PValue”という名前で pythonに公開する boost::python::class_<CValue>("PValue") //“value”プロパティは読み書き可 .add_property("value", &CValue::getValue, &CValue::setValue) //“rvalue”プロパティは読み取り専用 .add_property("rvalue", &CValue::getValue); }
- 31. // sample4.cpp //C++上でPythonインタプリタの実行 #define BOOST_PYTHON_STATIC_LIB // Boost.Pythonライブラリを静的にリンクする #include <iostream> #include <boost/python.hpp> void c_plus_plus(void) { std::cout << "C++" << std::endl; } int main() { // Pythonインタプリタを初期化する Py_Initialize(); // "__main__"モジュールをインポートする boost::python::object module = boost::python::import("__main__"); // "__main__"モジュールの名前空間でコードを実行する boost::python::object ns = module.attr("__dict__"); // "cpp"という名前で Pythonのインタプリタから呼び出す ns["cpp"] = &c_plus_plus; // Pythonのインタプリタを実行する boost::python::exec( "print('Hello, World!')" "cpp() " "value=2**20" , ns); // "__main__"モジュールの名前空間で定義した変数の値を取り出す int v = boost::python::extract<int>(ns["value"]); std::cout << v << std::endl; // Pythonインタプリタの終了処理を行う Py_Finalize(); }