隨筆 - 67  文章 - 79  trackbacks - 0
          <2009年1月>
          28293031123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(1)

          隨筆檔案

          文章檔案

          相冊

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          這幾天單位的同事好多請假不來,上班如同值班一般,但是事情一樣沒少,甚是繁忙。
          父親去外地開會去了,母親在醫院陪伴住院的外祖母,晚飯都是自己簡單對付一下,隨便弄些糊口了事。

          今年的工作乏善可陳,除非上了幾個項目,真不知道總結怎么來寫。最大的成就或許是對python的學習和應用,本來想寫一些應用的,但是各個都是半成品。Java自己沒怎么去看過,或許是因為Java這一年也沒什么大的發展吧。C++倒是看了不少書,泛型也有點入門了,湊合著能寫點忽悠人的代碼了。Linux shell和gnu tools 那塊今年有學了不少東西。通過移植幾個win32的庫,把autotools了解的差不多了。這個月加了內存,升到4G后 裝了個64位的Vista玩,感覺就是windows的64位版本遠不如linux的。

          想想今年幫過不少人做過畢業設計,有本科的,也有研究生。有最鐵的兄弟,也有
          ________________
          上面寫于12.26 哪幾天身體不好 沒寫完 其實也沒多大意思 都已經到明年了 算了吧

          找地方貼下 我修改的python_def_function的代碼和示例
          /******************************************************************************
           * How to wrap boost.function objects with boost.python and use them both from
           * C++ and Python.
           * 
           *   AUTHOR: Alcides Viamontes Esquivel
           *
           *   LICENSE: You're free to use this file for whatever you want. You're 
           *            specially welcome to improve it in any way and give the 
           *            changes back.
           *
           ******************************************************************************
           *
           * Usage example:
           *
           *     For the boost.python C++ module:
           *     
           *     
           *     #include <boost/python.hpp>
           *     
           *     #include "py_boost_function.hpp"
           *     
           *
           *     void module_greeter_f(std::string const& origin)
           *     {
           *           cout << "Hello world, by " << origin << endl;
           *     }
           *
           *
           *     boost::function< void( std::string const& ) > module_greeter( 
           *          module_greeter_f
           *     ) ;
           *     
           *
           *     BOOST_PYTHON_MODULE( foo ) {
           *
           *          using namespace boost.python;
           *          
           *
           *          def_function< void(string const&) >(
           *              "greeter_function_t", 
           *              "A greeting function" 
           *          );
           *
           *          
           *          scope().attr("module_greeter") = module_greeter;
           *     }
           *
           * From python code:
           *
           *     - Invoke:
           *
           *          >>> import foo
           *          >>> foo.module_greeter("world")
           *
           *     - Create instances from python:
           *             
           *          >>> def my_greetings(hi):
           *          >>>  print hi, ", world"
           *          >>> 
           *          >>> grfunc = foo.greeter_function_t.from_callable( my_greetings )
           *
           
          */



          #ifndef PY_BOOST_FUNCTION_INCLUDE_GUARD
          #define PY_BOOST_FUNCTION_INCLUDE_GUARD 

          #include 
          <boost/python.hpp>

          #include 
          <boost/function.hpp>
          #include 
          <boost/function_types/function_type.hpp>
          #include 
          <boost/function_types/result_type.hpp>
          #include 
          <boost/function_types/is_function.hpp>

          #include 
          <boost/type_traits/function_traits.hpp>

          #include 
          <boost/preprocessor/repetition/enum_binary_params.hpp>
          #include 
          <boost/preprocessor/repetition/enum_params.hpp>
          #include 
          <boost/preprocessor/repetition/repeat.hpp>
          #include 
          <boost/preprocessor/repetition/repeat_from_to.hpp>


          namespace detail{

              template 
          <typename RT, int arity>
              
          struct pyobject_invoker;

              template 
          <typename RT> 
              
          struct pyobject_invoker<RT, 0 > 
              { 
                  boost::python::
          object callable; 
                  
                  RT 
          operator( )( )
                  {
                      
          return boost::python::extract<RT>( callable( ) );
                  }
              };

          #define MAKE_PYOBJECT_INVOKER(z, n, data) \
              template 
          <typename RT> \
              
          struct pyobject_invoker<RT, n >{ \
                  boost::python::
          object callable; \
                  template 
          < BOOST_PP_ENUM_PARAMS(n, typename Arg) > \
                  RT 
          operator( )(BOOST_PP_ENUM_BINARY_PARAMS(n, Arg, arg ) )\
                  {\
                      boost::python::
          object o = callable( BOOST_PP_ENUM_PARAMS(n, arg) );\
                      
          return boost::python::extract<RT>( o );\
                  }\
              };\
              template 
          <> \
              
          struct pyobject_invoker<void, n >{ \
                  boost::python::
          object callable; \
                  template 
          < BOOST_PP_ENUM_PARAMS(n, typename Arg) > \
                  
          void operator( )(BOOST_PP_ENUM_BINARY_PARAMS(n, Arg, arg ) )\
                  {\
                      boost::python::
          object o = callable( BOOST_PP_ENUM_PARAMS(n, arg) );\
                  }\
              };
              
          BOOST_PP_REPEAT_FROM_TO( 
          16, MAKE_PYOBJECT_INVOKER, nothing );

          #undef MAKE_PYOBJECT_INVOKER
              
              template 
          <typename FT>
              boost::function
          < FT > function_frompyobj( boost::python::object o )
              {
                  
          const int arity = boost::function_traits< FT >::arity;
                  typedef 
                      typename  boost::function_types::result_type
          < FT >::type
                      result_type;
                  pyobject_invoker
          <result_type, arity > inv;
                  inv.callable 
          = o;
                  
          return inv;
              }

          // namespace detail

          template 
          <typename FT>
          void def_function(const char* func_name, const char* func_doc)
          {
              BOOST_STATIC_ASSERT( boost::function_types::is_function
          < FT >::value ) ;
              
          namespace bp = boost::python;
              typedef boost::function
          <FT> function_t;
              bp::class_
          < function_t >
              (func_name, func_doc, bp::no_init)
                  .def(
          "__call__"&function_t::operator() )
                  .def(
          "from_callable"&detail::function_frompyobj<FT> )
                  .staticmethod(
          "from_callable")
              ;
          // def_function0




          #endif // PY_BOOST_FUNCTION


          // python_test.cpp : 定義控制臺應用程序的入口點。
          //

          #include 
          "stdafx.h"
          #include 
          <boost/function.hpp>
          #include 
          <boost/bind.hpp>
          #include 
          <string>

          void error(){
              PyErr_Print();
              boost::python::
          object sys(
                boost::python::handle
          <>(PyImport_ImportModule("sys"))
              );
              boost::python::
          object err = sys.attr("stderr");
              std::
          string err_text = boost::python::extract<std::string>(err.attr("getvalue")());
              fprintf(stderr,
          "Python Error:\n%s\n",err_text.c_str());

          }
          void py_init(boost::python::object& main_module,boost::python::object& main_namespace){
              Py_Initialize();
              main_module 
          = boost::python::import("__main__");
              main_namespace 
          = main_module.attr("__dict__");
              PyRun_SimpleString(
          "import cStringIO");
              PyRun_SimpleString(
          "import sys");
              PyRun_SimpleString(
          "sys.stderr = cStringIO.StringIO()");

          }
          class Sample{
          private:
              
          int _value;
          public:
              Sample(
          int value):_value(value){}
              Sample(
          const Sample& rhs):_value(rhs._value){}
              
          void setValue(int value){_value=value;}
              
          int  getValue(){return _value;}

          };
          void print_hello(const char* str){
              printf(
          "%s\n",str);
          }


          int _tmain(int argc, _TCHAR* argv[])
          {

              Sample sample(
          10);
              boost::function
          <void(int)> setter = boost::bind(&Sample::setValue,&sample,_1);
              boost::function
          <int(void)> getter = boost::bind(&Sample::getValue,&sample);
              
              boost::python::
          object main_module,main_namespace;
              py_init(main_module,main_namespace);

              def_function
          <int(void)>("getter_t""A functor sample");
              def_function
          <void(int)>("setter_t""A functor sample 2");
              
          using namespace boost::python;
              
              
              main_namespace[
          "hello"]=make_function(print_hello);
              main_namespace[
          "get"]=getter;
              main_namespace[
          "set"]=setter;
             
              
          try
              {
                  
          object ignored = exec("set(20);print get()", main_namespace,main_namespace);
                  printf(
          "%d\n",sample.getValue());
              }
              
          catch(error_already_set const &)
              {
                  error();
              }
              
          return 0;
          }



          posted on 2009-01-24 09:58 zarra 閱讀(326) 評論(1)  編輯  收藏

          FeedBack:
          # re: 寫給明年的我[未登錄] 2009-01-24 20:21 apple
          你都快成畢設專業戶了~~
          樹大招風!!  回復  更多評論
            

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 来宾市| 怀化市| 平湖市| 宝清县| 离岛区| 民乐县| 定陶县| 图木舒克市| 皮山县| 周宁县| 高清| 乐平市| 南安市| 镇宁| 奉贤区| 安徽省| 云霄县| 许昌县| 翁源县| 双柏县| 昆明市| 丰都县| 台山市| 信丰县| 抚顺县| 马公市| 文山县| 巴南区| 襄汾县| 荥经县| 宝山区| 台南市| 遂平县| 保亭| 濮阳市| 玉门市| 法库县| 古丈县| 龙陵县| 琼中| 察哈|