Difference between revisions of "2013 Summer Project Week:Python Embedding"
From NAMIC Wiki
(Created page with '__NOTOC__ <gallery> Image:PW-MIT2013.png|Projects List </gallery> ==Key Investigators== * GRC: Rui Li, Jim Miller * Kitware: Jean-Christoph…') |
|||
Line 14: | Line 14: | ||
<h3>Objective</h3> | <h3>Objective</h3> | ||
Python embedding library to manage calling python functions from C++. This will eliminate the code duplication in Slicer. | Python embedding library to manage calling python functions from C++. This will eliminate the code duplication in Slicer. | ||
− | |||
− | |||
− | |||
− | |||
Line 34: | Line 30: | ||
<h3>Progress</h3> | <h3>Progress</h3> | ||
− | + | Eliminate all the compile time warnings and fixed some stylistic issues. The updated code is pushed | |
+ | on to github at https://github.com/grclirui/PythonCppAPI.git and ready to use. | ||
</div> | </div> | ||
</div> | </div> | ||
==Delivery Mechanism== | ==Delivery Mechanism== | ||
+ | PythonCppAPI is a stand alone library. To use it within Slicer, similar to SlicerExecutionModel and other external libraries, an External_PythonCppAPI.cmake is needed in SuperBuild. During superbuild, | ||
+ | it will be checked out and built as a library. | ||
+ | |||
+ | // Ok. Now we can create an instance of the interprter itself. | ||
+ | PythonCppApiCallFunction pyCppApiCallFunction(argc, argv); | ||
+ | |||
+ | // to check if the python interpreter is available and working, | ||
+ | // execute a simple python statement. | ||
+ | pyCppApiCallFunction.ExecuteString("print 'hello'"); | ||
+ | |||
+ | // execute the script in a file | ||
+ | pyCppApiCallFunction.ExecuteFile("hello.py"); | ||
+ | |||
+ | // Load the test module into the interpreter | ||
+ | pyCppApiCallFunction.ImportModule("test"); | ||
+ | |||
+ | |||
+ | ArgMap args; // our argument list that maps string values | ||
+ | bool ok; | ||
+ | |||
+ | // Call a simple function, that takes no arguments | ||
+ | // and returns nothing | ||
+ | args.clear(); | ||
+ | |||
+ | pyCppApiCallFunction.CallFunction("simple", args); | ||
− | + | // Call a function that takes two long arguments and returns | |
+ | // a long | ||
+ | { | ||
+ | PythonCppApiVariant ret(0); // value returned by python is stored here | ||
+ | args["10"] = PythonLong; | ||
+ | args["20"] = PythonLong; | ||
+ | ret = pyCppApiCallFunction.CallFunction("multiply", args); | ||
+ | std::cout << ret.toLong(&ok, 0) << '\n'; | ||
+ | args.clear(); | ||
+ | } |
Revision as of 18:36, 20 June 2013
Home < 2013 Summer Project Week:Python EmbeddingKey Investigators
- GRC: Rui Li, Jim Miller
- Kitware: Jean-Christophe Fillion-Robin
- Isomics: Steve Pieper
Objective
Python embedding library to manage calling python functions from C++. This will eliminate the code duplication in Slicer.
Approach, Plan
Discuss with Jc and Steve regarding how to incorporate into Slicer. Currently it is used as a downloadable library during superbuild, similar to SlicerExecutionModel.
Progress
Eliminate all the compile time warnings and fixed some stylistic issues. The updated code is pushed on to github at https://github.com/grclirui/PythonCppAPI.git and ready to use.
Delivery Mechanism
PythonCppAPI is a stand alone library. To use it within Slicer, similar to SlicerExecutionModel and other external libraries, an External_PythonCppAPI.cmake is needed in SuperBuild. During superbuild, it will be checked out and built as a library.
// Ok. Now we can create an instance of the interprter itself. PythonCppApiCallFunction pyCppApiCallFunction(argc, argv);
// to check if the python interpreter is available and working, // execute a simple python statement. pyCppApiCallFunction.ExecuteString("print 'hello'");
// execute the script in a file pyCppApiCallFunction.ExecuteFile("hello.py");
// Load the test module into the interpreter pyCppApiCallFunction.ImportModule("test");
ArgMap args; // our argument list that maps string values bool ok;
// Call a simple function, that takes no arguments // and returns nothing args.clear();
pyCppApiCallFunction.CallFunction("simple", args);
// Call a function that takes two long arguments and returns // a long { PythonCppApiVariant ret(0); // value returned by python is stored here args["10"] = PythonLong; args["20"] = PythonLong; ret = pyCppApiCallFunction.CallFunction("multiply", args); std::cout << ret.toLong(&ok, 0) << '\n'; args.clear(); }