MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MyCaffe.python.PythonInterop Class Reference

The PythonInterop uses PythonNet to execute Python code. More...

Inheritance diagram for MyCaffe.python.PythonInterop:

Public Member Functions

 PythonInterop (string strPythonDllPath)
 The constructor. More...
 
void Dispose ()
 Free all resources used. More...
 
void Initialize (string strPythonDllPath)
 Initialize the Python Engine with the version of Python used. More...
 
void Shutdown ()
 Shutdown the Python engine. More...
 
void RunPythonCode (string pycode)
 Run the Python code specified. More...
 
void RunPythonCode (string pycode, object parameter, string parameterName)
 Run the Python code specified, passing a parameter to it from C# More...
 
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 More...
 
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 More...
 
Dictionary< string, object > ConvertToDictionary (object obj)
 Convert the Python object to a CLR dictionary. More...
 

Detailed Description

The PythonInterop uses PythonNet to execute Python code.

See also
Calling Python from C#:an Introduction to PythonNet

Definition at line 21 of file PythonInterop.cs.

Constructor & Destructor Documentation

◆ PythonInterop()

MyCaffe.python.PythonInterop.PythonInterop ( string  strPythonDllPath)

The constructor.

Parameters
strPythonDllPathSpecifies the path to the Python DLL (e.g. Python39.dll) which is usually located at:
@"C:\Users\" + strUserName + @"\AppData\Local\Programs\Python\Python39\python39.dll"

See https://github.com/MyCaffe/MyCaffe/blob/master/MyCaffe.app/FormGptTest.cs (dowork, line 85) for an example that demonstrates how to use the PythonInterop class to run the GPT2 pre-trained model from HuggingFace to generate text.

Definition at line 36 of file PythonInterop.cs.

Member Function Documentation

◆ ConvertToDictionary()

Dictionary< string, object > MyCaffe.python.PythonInterop.ConvertToDictionary ( object  obj)

Convert the Python object to a CLR dictionary.

Parameters
objSpecifies the PyObject returned by python.
Returns
A Dictionary of string,object type is returned.
Exceptions
ExceptionException thrown when 'obj' is not a PyObject type.

Definition at line 161 of file PythonInterop.cs.

◆ Dispose()

void MyCaffe.python.PythonInterop.Dispose ( )

Free all resources used.

Definition at line 44 of file PythonInterop.cs.

◆ Initialize()

void MyCaffe.python.PythonInterop.Initialize ( string  strPythonDllPath)

Initialize the Python Engine with the version of Python used.

Parameters
strPythonDllPathSpecifies the path to the Python DLL (e.g. Python39.dll) which is usually located at:
@"C:\Users\" + strUserName + @"\AppData\Local\Programs\Python\Python39\python39.dll"

Definition at line 57 of file PythonInterop.cs.

◆ RunPythonCode() [1/2]

void MyCaffe.python.PythonInterop.RunPythonCode ( string  pycode)

Run the Python code specified.

Parameters
pycodeSpecifies a block of Python code to run.

Definition at line 75 of file PythonInterop.cs.

◆ RunPythonCode() [2/2]

void MyCaffe.python.PythonInterop.RunPythonCode ( string  pycode,
object  parameter,
string  parameterName 
)

Run the Python code specified, passing a parameter to it from C#

Parameters
pycodeSpecifies a block of Python code to run.
parameterSpecifies the parameter value.
parameterNameSpecifies the parameter name within the Python code.

Definition at line 89 of file PythonInterop.cs.

◆ RunPythonCodeAndReturn() [1/2]

object MyCaffe.python.PythonInterop.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

Parameters
pycodeSpecifies a block of Python code to run.
parameterSpecifies the parameter value.
parameterNameSpecifies the parameter name within the Python code.
returnedVariableNameSpecifies the name of the return value in the Python code.
Returns
The Python return value is returned.

Definition at line 110 of file PythonInterop.cs.

◆ RunPythonCodeAndReturn() [2/2]

object MyCaffe.python.PythonInterop.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

Parameters
pycodeSpecifies a block of Python code to run.
returnedVariableNameSpecifies the name of the return value in the Python code.
rgArgSpecifies a list of parameter name, value pairs.
Returns
The Python return value is returned.

Definition at line 132 of file PythonInterop.cs.

◆ Shutdown()

void MyCaffe.python.PythonInterop.Shutdown ( )

Shutdown the Python engine.

Definition at line 66 of file PythonInterop.cs.


The documentation for this class was generated from the following file: