-
Notifications
You must be signed in to change notification settings - Fork 27
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
Load Python from String or File Path #290
Comments
Like most things in Python, there's 100 ways to do this. Could you share a bit more about your requirement?
This doc explains some of the complexity https://docs.python.org/3/library/functions.html#eval If you were able to share a Python equivalent of what you're trying to do, then I'd know which APIs need implementing. |
So - just as an example from your own samples, what we're looking for is the ability to execute e.g. def format_name(name: str, max_length: int = 50) -> str:
return "Hello {}".format(name.capitalize())[:max_length] By simply assigning the above string value. Right now there's For our own use cases these would be I can give more concrete examples if the above does not suffice. In a broader sense, being able to load and execute any of the Python code found in your simple example by specifying a string containing the Python code rather than referencing a file that was there at build time would do what we're looking for. I can give examples of how we're currently doing this with another Python NuGet package if that helps. |
Its unclear what you want to do.... . But anything is already possible: either using make a python "wrapper" - a .py file included in your project which defines a function like
call that in c# via the CSnakes PythonEnvironment and import the result as module into python. Include the source generator of CSnakes in your app and do something like
After that use some code from the web to load the generated source into dotnet at runtime... But I guess you asked for a wrapper over exec (define the function) and eval (execute it) ... Just look up how you can solve that problem in pure python and wrap it into one or two functions you can call from C# via the PythonEnvironment . As a starting point on the python side take a look at https://stackoverflow.com/questions/12698028/why-is-pythons-eval-rejecting-this-multiline-string-and-how-can-i-fix-it Or the .py wrapper only creates a function object and returns it. On the C# side its a In case you don't want to use a wrapper, you must include the CSnakes project into your code to be able to write the equivalent code under the |
Take a look at my refactored fork of CSnakes at https://github.com/minesworld/CSnakes/tree/minesworld The main difference is: the CSnakes.Runtime.Python base object is PythonObject instead of PyObject. Just different name to make clear that it isn't a CPython PyObject but a dotnet wrapper on such one. Renaming of the other objects will follow... There you find a You could use it like:
The problem defining a function from source is how to obtain it as source could define many functions, variables, imports etc. . This is solved here by using a module. Another option would be to Compile the source, Run it using two PyDict for global and local variables. And get the function from the local variables dict ... The name of the function is either known, or all dict items / But thats not my cup of tea for now... I wrote that function to create and run a PS: will rename |
I apologize if I've overlooked this. I am trying out this project to compare to other options as the support and stated goals seem outstanding (saw on .NET video on YouTube). Congrats on the project and visibility!
What I'd like to be able to do for our project is load Python code from a string or an arbitrary file that is not present at build time. This is for a general scripting engine where we allow JavaScript and Python.
While the code generation is awesome and we may use it for future solutions, that's not what we are aiming to use at this time. I see from the docs that you can in fact bypass codegen, but it still seems to load a module by name rather than by file path or passing in a string of Python.
Is this currently possible or on the roadmap? Or is that outside the scope of this project. Thanks for the project!
The text was updated successfully, but these errors were encountered: