Description
"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.