A wrapper around PythonNet that allows for execution of Python code.
$ dotnet add package Ekeel.Interop.PythonA wrapper around PythonNet that allows for execution of Python code.
using Ekeel.Interop.Python;
var pythonHandler = new Handler();
var variables = new Dictionary<string, object>();
variables.Add("test_var", "test_var_val");
pythonHandler.RunFromFile(@"C:\tmp\test.py", "test_var", variables);
Handler
void RunFromString(string code)void RunFromString(string code, Dictionary<string, object> variables)TResult RunFromString<TResult>(string code, string returnVariable)TResult RunFromString<TResult>(string code, string returnVariable, Dictionary<string, object> variables)void RunFromFile(string scriptFile)void RunFromFile(string scriptFile, Dictionary<string, object> variables)TResult RunFromFile<TResult>(string scriptFile, string returnVariable)TResult RunFromFile<TResult>(string scriptFile, string returnVariable, Dictionary<string, object> variables)HandlerClass Handler handles execution of Python scripts using a local Python setup.
Handler()This constructor initializes a new
Handlerusing a .env file.Notes:
- Read required variables from a local
.envfile.Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler();
Handler(string pythonDllPath)This constructor initializes a new
Handler.Parameters:
pythonDllPath
- The local path of the Python DLL to use for the runtime.
Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(@"C:\tmp\test.dll");
Handler(string pythonDllPath, string pythonHome)This constructor initializes a new
Handler.Parameters:
pythonDllPath
- The local path of the Python DLL to use for the runtime.
pythonHome
- The path to the python home directory.
Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(@"C:\tmp\test.dll", @"C:\python\");
void RunFromString(string code)This method runs a string of Python code.
Parameters:
code
- The Python string to execute.
Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); pythonHandler.RunFromString("import sys; sysversion = sys.version");
void RunFromString(string code, Dictionary<string, object> variables)This method runs a string of Python code.
Parameters:
code
- The Python string to execute.
variables
- Dictionary containing the key/values of variables to set.
Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); var variables = new Dictionary<string, object>(); variables.Add("test_var", "test_var_val"); pythonHandler.RunFromString("import sys; sysversion = sys.version; print('test_var')", variables);
TResult RunFromString<TResult>(string code, string returnVariable)This method runs a string of Python code.
Parameters:
code
- The Python string to execute.
returnVariable
- The variable to read from the scope.
Returns:
- The value of the
returnVariablefrom the scope.Exceptions:
KeyNotFoundException
- The
returnVariablewas not found.Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); var retVal = pythonHandler.RunFromString("import sys; sysversion = sys.version", "sysversion");
TResult RunFromString<TResult>(string code, string returnVariable, Dictionary<string, object> variables)This method runs a string of Python code.
Parameters:
code
- The Python string to execute.
returnVariable
- The variable to read from the scope.
variables
- Dictionary containing the key/values of variables to set.
Returns:
- The value of the
returnVariablefrom the scope.Exceptions:
KeyNotFoundException
- The
returnVariablewas not found.Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); var variables = new Dictionary<string, object>(); variables.Add("test_var", "test_var_val"); var retVal = pythonHandler.RunFromString("import sys; sysversion = sys.version", "sysversion", variables);
void RunFromFile(string scriptFile)This method runs a Python script.
Parameters:
scriptFile
- he path to the Python script to execute.
Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); pythonHandler.RunFromFile(@"C:\tmp\test.py");
void RunFromFile(string scriptFile, Dictionary<string, object> variables)This method runs a Python script.
Parameters:
scriptFile
- he path to the Python script to execute.
variables
- Dictionary containing the key/values of variables to set.
Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); var variables = new Dictionary<string, object>(); variables.Add("test_var", "test_var_val"); pythonHandler.RunFromFile(@"C:\tmp\test.py", variables);
TResult RunFromFile<TResult>(string scriptFile, string returnVariable)This method runs a Python script.
Parameters:
scriptFile
- he path to the Python script to execute.
returnVariable
- The variable to read from the scope.
Returns:
- The value of the
returnVariablefrom the scope.Exceptions:
KeyNotFoundException
- The
returnVariablewas not found.Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); pythonHandler.RunFromFile(@"C:\tmp\test.py", "sysversion");
TResult RunFromFile<TResult>(string scriptFile, string returnVariable, Dictionary<string, object> variables)This method runs a Python script.
Parameters:
scriptFile
- he path to the Python script to execute.
returnVariable
- The variable to read from the scope.
variables
- Dictionary containing the key/values of variables to set.
Returns:
- The value of the
returnVariablefrom the scope.Exceptions:
KeyNotFoundException
- The
returnVariablewas not found.Examples:
var pythonHandler = new Ekeel.Interop.Python.Handler(); var variables = new Dictionary<string, object>(); variables.Add("test_var", "test_var_val"); pythonHandler.RunFromFile(@"C:\tmp\test.py", "sysversion", variables);