Title: Add a class to manage the context of a interaction with Python and simplify the variable exchanging · Issue #380 · pythonnet/pythonnet · GitHub
Open Graph Title: Add a class to manage the context of a interaction with Python and simplify the variable exchanging · Issue #380 · pythonnet/pythonnet
X Title: Add a class to manage the context of a interaction with Python and simplify the variable exchanging · Issue #380 · pythonnet/pythonnet
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 an...
Open Graph 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 l...
X 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 varia...
Opengraph URL: https://github.com/pythonnet/pythonnet/issues/380
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add a class to manage the context of a interaction with Python and simplify the variable exchanging","articleBody":"\"eval\" and \"exec\" are the most frequently used methods when interacting with Python. However, this two methods implemented [here](https://github.com/pythonnet/pythonnet/pull/389) are hard to use because maintain the global variable dictionary and local variable dictionary is a tedious and repeated work.\r\n\r\nThe nature of the problem lies in the variable exchanging between .NET and Python.\r\n\r\nI think a scope class can be added to manage the context of a interaction with Python and simplify the variable exchanging.\r\nA 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. \r\n\r\nFor example, the eval example provided in that PR,\r\n```\r\ndynamic sys = Py.Import(\"sys\");\r\nsys.attr1 = 100;\r\nPyDict locals = new PyDict();\r\nlocals.SetItem(\"sys\", sys);\r\nlocals.SetItem(\"a\", new PyInt(10));\r\n \r\nPythonEngine.Exec(\"c = sys.attr1 + a + 1\", null, locals.Handle);\r\nvar c = locals.GetItem(\"c\").AsManagedObject(typeof(Int32));\r\n\r\nConsole.WriteLine(\"c= {0}\", c);\r\n```\r\n\r\ncan be written as,\r\n\r\n```\r\nvar ps = Py.Session('test')\r\nsys = ps.Import(\"sys\");\r\nsys.attr1 = 100;\r\nps.SetLocal(\"a\", 10);\r\nps.Exec(\"c = sys.attr1 + a + 1\"); //pass\r\nvar c = ps.Get\u003cSystem.Int32\u003e(\"c\"); //get the local variable and convert it to .NET type.\r\n\r\nps.Dispose(); \r\nor\r\nps.Suspend(); //use it later. Py.Session('test') will return it.\r\n```\r\n\r\nI implement a prototype of this proposal [here](https://github.com/pythonnet/pythonnet/pull/381) to help discussion.\r\n","author":{"url":"https://github.com/yagweb","@type":"Person","name":"yagweb"},"datePublished":"2017-02-15T16:35:57.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":3},"url":"https://github.com/380/pythonnet/issues/380"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:dea0085c-cb1b-66d0-5321-043c267535ef |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 9804:22A73C:22BB0AE:2E9DBE9:697036FF |
| html-safe-nonce | eba9afcba81b1b19798761a9f5bd3e330579f7f5fd3102f8e218aa29b922a5bb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI5ODA0OjIyQTczQzoyMkJCMEFFOjJFOURCRTk6Njk3MDM2RkYiLCJ2aXNpdG9yX2lkIjoiOTE3MzQzMjgzMDM3NTUwNzcxMSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 1503e1c510297747ca9f569c22ceb7b398186885944984c491037be1dda5a819 |
| hovercard-subject-tag | issue:207856825 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/pythonnet/pythonnet/380/issue_layout |
| twitter:image | https://opengraph.githubassets.com/0847e081ff10ca83bced8650cbdfc7ba834051113aac2c1b61adbfe7658a207e/pythonnet/pythonnet/issues/380 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/0847e081ff10ca83bced8650cbdfc7ba834051113aac2c1b61adbfe7658a207e/pythonnet/pythonnet/issues/380 |
| og:image:alt | "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 l... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | yagweb |
| hostname | github.com |
| expected-hostname | github.com |
| None | 9920a62ba22d06470388e2904804fb7e5ec51c9e35f81784e9191394c74b2bd2 |
| turbo-cache-control | no-preview |
| go-import | github.com/pythonnet/pythonnet git https://github.com/pythonnet/pythonnet.git |
| octolytics-dimension-user_id | 6050430 |
| octolytics-dimension-user_login | pythonnet |
| octolytics-dimension-repository_id | 14748123 |
| octolytics-dimension-repository_nwo | pythonnet/pythonnet |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 14748123 |
| octolytics-dimension-repository_network_root_nwo | pythonnet/pythonnet |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | f643964067a552f02067066d6a910b2f90a5721f |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width