10000 Add a class to manage the context of a interaction with Python and simplify the variable exchanging · Issue #380 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Add a class to manage the context of a interaction with Python and simplify the variable exchanging #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
yagweb opened this issue Feb 15, 2017 · 3 comments

Comments

@yagweb
Copy link
Contributor
yagweb commented Feb 15, 2017

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

@yagweb
Copy link
Contributor Author
yagweb commented 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
Copy link
Contributor

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.

@yagweb
Copy link
Contributor Author
yagweb commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
0