IronLanguages / ironpython2

Implementation of the Python programming language for .NET Framework; built on top of the Dynamic Language Runtime (DLR).
http://ironpython.net
Apache License 2.0
1.07k stars 228 forks source link

IronPython2 source code Documentation #690

Open RaviMDhoble opened 4 years ago

RaviMDhoble commented 4 years ago

Hello, @slozier, I am looking for the documentation guide of ironpython2 source code repo and really want to understand the functioning of individual code projects in the IronPython.sln file. I got the IronPython related documents from ironpython.net but that is more related to use at the application level.

I am developing a software suite, based on c# and IronPython. Here is the concept, I want to add my own custom python function directly into the DLR (correct me, if am wrong) that will call the similar function defined in c#. Somehow, I want to design a python wrapper for the similar implemented methods in my c# modules. So, here I want to use the entire IronPython source code in my application (bcoz its written in C#) and as my application will grow, I will add my other c# project submodules to this project. So, when I will pass these python wrapper functions to the python interpreter, it will directly call the related c# function defined in the module. Any suggestions on this implementation? Thanks.

thimolangbehn commented 4 years ago

From: RaviMDhoble Here is the concept, I want to add my own custom python function directly into the DLR (correct me, if am wrong) that will call the similar function defined in c#. Somehow, I want to design a python wrapper for the similar implemented methods in my c# modules. So, here I want to use the entire IronPython source code in my application (bcoz its written in C#) and as my application will grow, I will add my other c# project submodules to this project. So, when I will pass these python wrapper functions to the python interpreter, it will directly call the related c# function defined in the module. Any suggestions on this implementation? Thanks.

You already can call C# functions from Python Code running in IronPython; that is the major feature of this implementation. All you need to do is reference the DLL, import the namespace and invoke the Method. You do not need to delve into the IronPython source code for that.

Cheers,

Thimo

EUROIMMUN a PerkinElmer company

https://www.euroimmun.de https://www.twitter.com/euroimmun https://www.euroimmunblog.de https://www.xing.com/company/euroimmun https://www.youtube.com/euroimmun https://www.kununu.com/euroimmun

Hauptsitz des Unternehmens: EUROIMMUN Medizinische Labordiagnostika AG, Seekamp 31, 23560 Lübeck Registergericht: Amtsgericht Lübeck, HRB 2330 Vorstand: Dr. Wolfgang Schlumberger (Vorsitzender), Susanne Aleksandrowicz, Dirk Beecker Vorsitzender des Aufsichtsrats: Robert F. Friel

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

RaviMDhoble commented 4 years ago

@thimolangbehn, Thanks for the info. Well, earlier I have looked into both methods-

  1. Calling CLR from Python code, which is basically using python as the main language and importing CLR as an engine module to use .net functionality.
  2. Calling DLR from C# code, which including ironpython.dll, ironpythonmodule.dll assemblies in c# application to use IronPython methods. Here, I am using 2nd methods as mentioned above. C# as a frontend and IronPython as a backend engine. ok. I can use all the different methods that iron python allows me to execute and run the python code/file and do other stuff through DLL., but HERE, I cannot add extensibility to the python language itself like adding our own custom user-defined functions through IronPython DLL APIs.

for eg. I have an ABC.py script, there is a function "def XYZ()", now python already has the definition of "def", so the interpreter will detect it while executing. Now,

What if I have to add/define my own custom function lets say "RunVI(string filepath, int param1, int param2,..etc) " directly into Python, like "def function" or like if, while, switch etc etc.

. So when I will execute this file ABC.py (which has RunVI function defined) in the python interpreter, this RunVI in python will act as a wrapper function to call a "RunVI" method defined in the C# code (It can be different c# code and assuming these two different c# modules are linked through a mechanism so that they can both access each other's methods and properties. Here, actually I am creating two c# modules 1. script file editor module and another C# module which as a method RunVI). Any Idea? if something is unclear to understand pls ask me. I am new to python, trying to understand all this stuff.

Ironpython2_issue#690

thimolangbehn commented 4 years ago

What if I have to add/define my own custom function lets say "RunVI(string filepath, int param1, int param2,..etc) " directly into Python, like "def function" or like if, while, switch etc etc.

I am not entirely sure what you mean with that. If you mean to extend the python language itself as in adding new keywords (like a switch) etc. you would indeed need to change the IronPython interpreter. This is highly inadvisable, and you will probably not find much support for this.

If you just want to define Python functions and call them from C#, or define C# functions and call them from Python. Both is possible and described in the documentation. You can extend the available python modules by writing your own .py code and put it either in the existing Lib directory, or add another to sys.path. As soon as you have done that, you can import and use the additional modules. These modules again can reference and use C# assemblies and call methods defined there. You do not need to run an entire python code file, you can execute single statements as well, or bind python objects as dynamic and call methods directly. Therefore, you could create a call chain C# -> Python -> C# and so on. None of these require modification of the IronPython.dll.

Cheers,

Thimo

EUROIMMUN a PerkinElmer company

https://www.euroimmun.de https://www.twitter.com/euroimmun https://www.euroimmunblog.de https://www.xing.com/company/euroimmun https://www.youtube.com/euroimmun https://www.kununu.com/euroimmun

Hauptsitz des Unternehmens: EUROIMMUN Medizinische Labordiagnostika AG, Seekamp 31, 23560 Lübeck Registergericht: Amtsgericht Lübeck, HRB 2330 Vorstand: Dr. Wolfgang Schlumberger (Vorsitzender), Susanne Aleksandrowicz, Dirk Beecker Vorsitzender des Aufsichtsrats: Robert F. Friel

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

RaviMDhoble commented 4 years ago

@thimolangbehn

You can extend the available python modules by writing your own .py code and put it either in the existing Lib directory, or add another to sys.path. As soon as you have done that, you can import and use the additional modules. These modules again can reference and use C# assemblies and call methods defined there.

As you mentioned..this seems one of the simple and possible ways to do that..basically, I want to define some custom python functions which are nothing but act as a wrapper func to call the same functions in c# (exact same). Can you provide an example of this? I am new to this.

You do not need to run an entire python code file, you can execute single statements as well, or bind python objects as dynamic and call methods directly. Therefore, you could create a call chain C# -> Python -> C# and so on.

  • How to bind python objects as dynamic? What is this technique? and how this call chain will work? an example will help to gain this concept. Thanks for providing suggestions.
thimolangbehn commented 4 years ago

How to bind python objects as dynamic? What is this technique? and how this call chain will work?

Assuming you have a C# class like this in my_ns.dll namespace my_ns { public class Foo { public void show_message(string msg) { Console.WriteLine(msg); } } }

You could create a Python module bar.py accessing the above class like this: import clr clr.AddReference('my_ns.dll') # assuming the dll is in the search path import my_ns

def print_console(msg): foo = my_ns.Foo() foo.show_message(msg)

An again, create C# code using the python module:

assuming you have the script scope in scope, and bar.py in the python module search path

Microsoft.Scripting.Hosting.ScriptScope scope; scope.Engine.CreateScriptSourceFromString($"import bar", SourceCodeKind.Expression).Execute(scope); dynamic my_bar = scope.GetVariable("bar"); my_bar.show_message("hello world");

Alternative methods are listed in the documentation: https://ironpython.net/documentation/dotnet/dotnet.html#accessing-python-code-from-other-net-code

Cheers,

Thimo

EUROIMMUN a PerkinElmer company

https://www.euroimmun.de https://www.twitter.com/euroimmun https://www.euroimmunblog.de https://www.xing.com/company/euroimmun https://www.youtube.com/euroimmun https://www.kununu.com/euroimmun

Hauptsitz des Unternehmens: EUROIMMUN Medizinische Labordiagnostika AG, Seekamp 31, 23560 Lübeck Registergericht: Amtsgericht Lübeck, HRB 2330 Vorstand: Dr. Wolfgang Schlumberger (Vorsitzender), Susanne Aleksandrowicz, Dirk Beecker Vorsitzender des Aufsichtsrats: Robert F. Friel

Diese E-Mail enthält vertrauliche und/oder rechtlich geschützte Informationen. Wenn Sie nicht der richtige Adressat sind oder diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die unbefugte Weitergabe dieser Mail ist nicht gestattet.

RaviMDhoble commented 4 years ago

@thimolangbehn Thanks, for providing the example. So, I am going to import python scripts in the c# application and will embed IronPython as a background engine. So, I created a C# console project, added IronPython and Microsoft scripting assemblies. Now, am exploring the IronPython DLL C# methods to perform various operations on the python script. In the attached screenshot, Ironpython1-1.png, I am using pythonScript.execute method to run the HelloWorldModule.py file. It consists of three functions. Here, I want to execute each function line by line (means during execution first function say "def PrintHelloWorld()" will pass to IronPython compiler then it will return data back to c#, then 2nd function, 3rd so on... ) ---Queries---------------------------------------------------------------------------------------------------

  1. Here, what I see is, this pythonScript.execute() method executes the entire file at once and I cant see the result from each function execution, so how to use the correct "execute" method so that, it will pass every function to IronPython compiler one by one and get the result in c#.? How to do that?

  2. How to add 'Debugger' feature in my c# application, so that I can add a breakpoint, step in, step out, stepover etc to each function/line in my py script during execution mode? FYI, i read that Microsoft.Scripting.Debugging assembly provides such feature or Python debugger is also there, but how to integrate it?

    Ironpython1 Ironpython1-1 pyfile

RaviMDhoble commented 4 years ago

Hi @thimolangbehn, @slozier After a little bit of work around this... I can execute the python file and see the output results in the c# console application. Here, is the work summary for my above queries.

  1. Here, what I see is, this pythonScript.execute() method executes the entire file at once and I cant see the result from each function execution, so how to use the correct "execute" method so that, it will pass every function to IronPython compiler one by one and get the result in c#.? How to do that?
    • In the below screenshot.. the sample Python file has simple functions to perform add, sub, mul, div operations on two arguments, "first", "second".. using Ops.GetMember() func, I can pass the values from my c# app to the py script and get the results from each function in the R1, R2, R3, R4 variables. That's printed in the console window. So, that way it's working. Well, am still testing different PY scripts with more complex code to see the behavior. CallingPythonCode

How to add 'Debugger' feature in my c# application, so that I can add a breakpoint, step in, step out, stepover, etc to each function/line in my py script during execution mode? FYI, I read that Microsoft.Scripting.Debugging assembly provides such a feature or Python debugger is also there, but how to integrate it?

  • From Pt1. I can execute the script in one go..and get the result at the end of execution but I want to control the flow of execution. Here, I am creating a graphical script editor in C# application, in which the user will simply drag and drop UI objects (button or custom function). For reference, here is the screenshot of NI TestStand software, in which execution of the script is done step by step and the next step in the script waits for the previous step to execute completely. So, using IronPython API how to implement such features and can I use Microsoft.Scripting.Debugger to have a debugging option in my software? I have searched a lot about attaching the debugger process but not sure how to implement in c#. NI Teststand