lookup python code with c++
authorDiane Trout <diane@caltech.edu>
Wed, 11 Oct 2006 23:43:33 +0000 (23:43 +0000)
committerDiane Trout <diane@caltech.edu>
Wed, 11 Oct 2006 23:43:33 +0000 (23:43 +0000)
use boost::algorithm::split to split our python namespace
reference.

py/python.cpp

index 198d5f90155bc98550350ff499ed4d880ea95f01..fe2364eb529df5c44b24ae95bfd1381a841a5108 100644 (file)
@@ -1,8 +1,13 @@
 #include "py/python.hpp"
+namespace py = boost::python;
 
 #include <iostream>
+#include <string>
+#include <vector>
 
-namespace py = boost::python;
+#include <boost/algorithm/string/split.hpp>
+#include <boost/algorithm/string/classification.hpp>
+namespace alg = boost::algorithm;
 
 MussaPython::MussaPython()
 {
@@ -64,21 +69,19 @@ void MussaPython::interpreter(FILE *fp)
 
 py::object MussaPython::operator[](std::string name)
 { 
-  std::string code = "reduce(lambda m,n: m.__dict__[n], [__main__] + '";
-  code += name;
-  code += "'.split('.'))";
-  return eval(code);
-  /*
-  py::object py_name(name);
-  py::object name_list = py_name.attr("split")(".");
-  int name_list_len = extract<int>(name_list.attr("__len__")());
+  typedef std::vector< std::string > string_vector;
+  string_vector split_name;
+  alg::split(split_name, name, alg::is_any_of("."));
+  
   py::object lookup = main_namespace["__main__"];
-  for(int i=0; i < name_list_len; ++i) {
-    lookup = lookup.attr("__dict__")(name_list[i]);
-    //std::cout << lookup << std::endl;
+  
+  for (string_vector::const_iterator name_i = split_name.begin();
+       name_i != split_name.end();
+       ++name_i)
+  {
+    lookup = lookup.attr("__dict__")[*name_i];
   }
   return lookup;
-  */
 }
 
 //! return a reference to a single mussa python interpreter
@@ -86,6 +89,4 @@ MussaPython& get_py()
 {
   static MussaPython py;
   return py;
-}
-
-
+}
\ No newline at end of file