3using System.Collections.Generic;
6using System.Runtime.InteropServices;
8using System.Threading.Tasks;
59 Environment.SetEnvironmentVariable(
"PYTHONNET_PYDLL", strPythonDllPath);
60 PythonEngine.Initialize();
68 PythonEngine.Shutdown();
79 PythonEngine.RunSimpleString(pycode);
89 public void RunPythonCode(
string pycode,
object parameter,
string parameterName)
93 using (PyModule scope = Py.CreateScope())
95 scope.Set(parameterName, parameter.ToPython());
112 object returnedVariable =
new object();
115 using (PyModule scope = Py.CreateScope())
117 scope.Set(parameterName, parameter.ToPython());
119 returnedVariable = scope.Get<
object>(returnedVariableName);
122 return returnedVariable;
132 public object RunPythonCodeAndReturn(
string pycode,
string returnedVariableName, params KeyValuePair<string, object>[] rgArg)
134 PyObject returnedVariable =
null;
137 using (PyModule scope = Py.CreateScope())
141 foreach (KeyValuePair<string, object> arg
in rgArg)
143 scope.Set(arg.Key, arg.Value.ToPython());
148 returnedVariable = scope.Get<
object>(returnedVariableName) as PyObject;
152 return returnedVariable;
163 PyObject pobj = obj as PyObject;
165 throw new Exception(
"Invalid type, expected a PyObject!");
168 converter.AddListType<
int>();
169 converter.AddListType<
long>();
170 converter.AddListType<
float>();
171 converter.AddListType<
double>();
178 converter.AddDictType<string,
object>();
180 object clrObj = converter.ToClr(pobj);
181 return clrObj as Dictionary<string, object>;
The PythonInterop uses PythonNet to execute Python code.
void RunPythonCode(string pycode)
Run the Python code specified.
void Dispose()
Free all resources used.
void Initialize(string strPythonDllPath)
Initialize the Python Engine with the version of Python used.
object RunPythonCodeAndReturn(string pycode, string returnedVariableName, params KeyValuePair< string, object >[] rgArg)
Run the Python code specified, passing a set of parameters to it from C# and receiving a return value
Dictionary< string, object > ConvertToDictionary(object obj)
Convert the Python object to a CLR dictionary.
PythonInterop(string strPythonDllPath)
The constructor.
object RunPythonCodeAndReturn(string pycode, object parameter, string parameterName, string returnedVariableName)
Run the Python code specified, passing a parameter to it from C# and receiving a return value
void Shutdown()
Shutdown the Python engine.
void RunPythonCode(string pycode, object parameter, string parameterName)
Run the Python code specified, passing a parameter to it from C#
The DoubleType represents a clr double type.
The FloatType represents a clr float type.
The Int32Type represents a clr int type.
The Int64Type represents a clr long type.
use PyConverter to convert between python object and clr object.
The StringType represents a clr string type.
The MyCaffe.python namespace contains python related classes to help improve interop between C# and P...