OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
PythonExpr.h
Go to the documentation of this file.
1 #ifndef __PYTHON_EXPR_H__
2 #define __PYTHON_EXPR_H__
3 
4 #include "boost/tuple/tuple.hpp"
5 #include "boost/variant/get.hpp"
6 #include "boost/variant/variant.hpp"
7 
8 #include <boost/python.hpp>
9 #include <boost/python/suite/indexing/vector_indexing_suite.hpp>
10 
11 #include "Util/Types.h"
12 #include "Util/OptPilotException.h"
13 #include "Expression/Expression.h"
15 
16 using namespace boost::python;
17 
35 
37  std::string script = boost::get<std::string>(args[0]);
38  std::vector<double> pargs;
39  for(size_t i = 1; i < args.size(); i++) {
40  pargs.push_back(boost::get<double>(args[i]));
41  }
42 
43  try {
44  Py_Initialize();
45  object main_module = import("__main__");
46  object main_namespace = main_module.attr("__dict__");
47 
48  boost::python::class_<std::vector<double> >("PyVec")
49  .def(boost::python::vector_indexing_suite<std::vector<double> >());
50  main_namespace["arguments"] = pargs;
51 
52  object ignored = exec_file(script.c_str(), main_namespace);
53  double res = extract<double>(main_namespace["result"]);
54 
55  Py_Finalize();
56  return boost::make_tuple(res, true);
57 
58  } catch (error_already_set) {
59  PyErr_Print();
60  return boost::make_tuple(0.0, false);
61  }
62  }
63 };
64 
65 
66 #endif
boost::tuple< double, bool > Result_t
Definition: Expression.h:37
std::vector< argument_t > arguments_t
Definition: function.hpp:19
Expressions::Result_t operator()(client::function::arguments_t args)
Definition: PythonExpr.h:36