You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a class to manage the context of a interaction with Python and simplify the variable exchanging #380
"eval" and "exec" are the most frequently used methods when interacting with Python. However, this two methods implemented here are hard to use because maintain the global variable dictionary and local variable dictionary is a tedious and repeated work.
The nature of the problem lies in the variable exchanging between .NET and Python.
I think a scope class can be added to manage the context of a interaction with Python and simplify the variable exchanging.
A Scope should act like a Python Module in .NET. In the Scope class, many python functions can be wrapped to operate with the locals and globals for easy-to-use and speeding up.
For example, the eval example provided in that PR,
dynamic sys = Py.Import("sys");
sys.attr1 = 100;
PyDict locals = new PyDict();
locals.SetItem("sys", sys);
locals.SetItem("a", new PyInt(10));
PythonEngine.Exec("c = sys.attr1 + a + 1", null, locals.Handle);
var c = locals.GetItem("c").AsManagedObject(typeof(Int32));
Console.WriteLine("c= {0}", c);
can be written as,
var ps = Py.Session('test')
sys = ps.Import("sys");
sys.attr1 = 100;
ps.SetLocal("a", 10);
ps.Exec("c = sys.attr1 + a + 1"); //pass
var c = ps.Get<System.Int32>("c"); //get the local variable and convert it to .NET type.
ps.Dispose();
or
ps.Suspend(); //use it later. Py.Session('test') will return it.
I implement a prototype of this proposal here to help discussion.
I think it's a better practice to avoid using RunString with one or two python statements in .NET code, because it is low runtime efficiency. This proposal and the proposal can help to achieve this goal.
Do you still need to initialize and finalize the engine? From your proposal it doesn't sound like it but looking at the tests you are initializing and finalizing it.
@vmuriart Thank you for your remind. I updated the unit test and removed the initialize and finalize methods. Updated some APIs and edit the description of this issue before.
Activity
yagweb commentedon Feb 15, 2017
I think it's a better practice to avoid using RunString with one or two python statements in .NET code, because it is low runtime efficiency. This proposal and the proposal can help to achieve this goal.
vmuriart commentedon Feb 15, 2017
Do you still need to
initializeandfinalizethe engine? From your proposal it doesn't sound like it but looking at the tests you are initializing and finalizing it.yagweb commentedon Feb 17, 2017
@vmuriart Thank you for your remind. I updated the unit test and removed the initialize and finalize methods. Updated some APIs and edit the description of this issue before.