Difference between revisions of "2013 Summer Project Week:Python Embedding"

From NAMIC Wiki
Jump to: navigation, search
(Created page with '__NOTOC__ <gallery> Image:PW-MIT2013.png|Projects List </gallery> ==Key Investigators== * GRC: Rui Li, Jim Miller * Kitware: Jean-Christoph…')
 
 
(2 intermediate revisions by the same user not shown)
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. The sample usage of the library is shown in the following code snippet:
 +
 +
  // 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);
  
https://github.com/grclirui/PythonCppAPI.git
+
  // 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();
 +
  }

Latest revision as of 18:38, 20 June 2013

Home < 2013 Summer Project Week:Python Embedding

Key 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. The sample usage of the library is shown in the following code snippet:

 // 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();
 }