MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
PythonInterop.cs
1using Python.Runtime;
2using System;
3using System.Collections.Generic;
4using System.IO;
5using System.Linq;
6using System.Runtime.InteropServices;
7using System.Text;
8using System.Threading.Tasks;
9
14{
21 public class PythonInterop : IDisposable
22 {
36 public PythonInterop(string strPythonDllPath)
37 {
38 Initialize(strPythonDllPath);
39 }
40
44 public void Dispose()
45 {
46 Shutdown();
47 }
48
57 public void Initialize(string strPythonDllPath)
58 {
59 Environment.SetEnvironmentVariable("PYTHONNET_PYDLL", strPythonDllPath);
60 PythonEngine.Initialize();
61 }
62
66 public void Shutdown()
67 {
68 PythonEngine.Shutdown();
69 }
70
75 public void RunPythonCode(string pycode)
76 {
77 using (Py.GIL())
78 {
79 PythonEngine.RunSimpleString(pycode);
80 }
81 }
82
89 public void RunPythonCode(string pycode, object parameter, string parameterName)
90 {
91 using (Py.GIL())
92 {
93 using (PyModule scope = Py.CreateScope())
94 {
95 scope.Set(parameterName, parameter.ToPython());
96 scope.Exec(pycode);
97 }
98
99 }
100 }
101
110 public object RunPythonCodeAndReturn(string pycode, object parameter, string parameterName, string returnedVariableName)
111 {
112 object returnedVariable = new object();
113 using (Py.GIL())
114 {
115 using (PyModule scope = Py.CreateScope())
116 {
117 scope.Set(parameterName, parameter.ToPython());
118 scope.Exec(pycode);
119 returnedVariable = scope.Get<object>(returnedVariableName);
120 }
121 }
122 return returnedVariable;
123 }
124
132 public object RunPythonCodeAndReturn(string pycode, string returnedVariableName, params KeyValuePair<string, object>[] rgArg)
133 {
134 PyObject returnedVariable = null;
135 using (Py.GIL())
136 {
137 using (PyModule scope = Py.CreateScope())
138 {
139 if (rgArg != null)
140 {
141 foreach (KeyValuePair<string, object> arg in rgArg)
142 {
143 scope.Set(arg.Key, arg.Value.ToPython());
144 }
145 }
146
147 scope.Exec(pycode);
148 returnedVariable = scope.Get<object>(returnedVariableName) as PyObject;
149 }
150 }
151
152 return returnedVariable;
153 }
154
161 public Dictionary<string, object> ConvertToDictionary(object obj)
162 {
163 PyObject pobj = obj as PyObject;
164 if (pobj == null)
165 throw new Exception("Invalid type, expected a PyObject!");
166
167 var converter = new PyConverter();
168 converter.AddListType<int>();
169 converter.AddListType<long>();
170 converter.AddListType<float>();
171 converter.AddListType<double>();
172 converter.Add(new StringType());
173 converter.Add(new Int64Type());
174 converter.Add(new Int32Type());
175 converter.Add(new FloatType());
176 converter.Add(new DoubleType());
177
178 converter.AddDictType<string, object>();
179
180 object clrObj = converter.ToClr(pobj);
181 return clrObj as Dictionary<string, object>;
182 }
183 }
184}
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.
Definition: PyConverter.cs:502
The FloatType represents a clr float type.
Definition: PyConverter.cs:464
The Int32Type represents a clr int type.
Definition: PyConverter.cs:388
The Int64Type represents a clr long type.
Definition: PyConverter.cs:426
use PyConverter to convert between python object and clr object.
Definition: PyConverter.cs:16
The StringType represents a clr string type.
Definition: PyConverter.cs:311
The MyCaffe.python namespace contains python related classes to help improve interop between C# and P...