brainbaking/content/wiki/code/python/ironpython.md

3.1 KiB

+++ title = "ironpython" draft = false tags = [ "code", "python", "ironpython" ] date = "2014-03-07" +++

IronPython

Interessante links:

C#/Python interop

C# in Python

Overerving

In Python C# klassen over laten erven gaat zonder meer. Constructors werken niet via _ _init_ _ maar via new:

The init method is the Python initializer method. It is an instance method and so receives the instance as the first argument, which by convention is called self. If you want to override the .NET constructor (responsible for creating the instance) then from Python you override the new class method.

import clr
clr.AddReference('System.Windows.Forms')

from System.Windows.Forms import (
    Application, Form
)


class MainForm(Form):

    def __new__(cls, text):
        instance = Form.__new__(cls)
        instance.Text = text

        return instance

app = MainForm('Hello World')
Application.Run(app)

Python in C#

De script engine manueel firen
 using IronPython;  
 using IronPython.Modules;  
 using System.Text;  

 public class ScriptExample {  
     [MenuItem("Python/HelloWorld")]  
     public static void ScriptTest()  
     {  
         // create the engine  
         var ScriptEngine = IronPython.Hosting.Python.CreateEngine();  
         // and the scope (ie, the python namespace)  
         var ScriptScope = ScriptEngine.CreateScope();  
         // execute a string in the interpreter and grab the variable  
         string example ###### "output  'hello world'";  
         var ScriptSource = ScriptEngine.CreateScriptSourceFromString(example);  
         ScriptSource.Execute(ScriptScope);  
         string came_from_script = ScriptScope.GetVariable<string>("output");  
         // Should be what we put into 'output' in the script.  
         Debug.Log(came_from_script);              
     }  
 }  

Via Embedding IronPython in Unity.

Visual Studio Integration

Installeer IronPython & Python tools for VS. File -> New Project -> IronPython Application.

References leggen

References voor het python project zijn niet voldoende, je moet ook het Search Paths mapje updaten, kan enkel op dll toegevoegd worden.

Vanaf dan kan je via clr de imports doen zoals using in C#:

import clr
clr.AddReferenceByName('Wizards')
from Wizards import *
# use some class from the Wizards namespace below.
Unit testen uitvoeren

MSTest & Test Explorer werkt automatisch en herkent alle testen - ReSharpner werkt niet meer. Een test python file uitvoeren kan ook met F5 (file als startup file), en unittest.main() erbij plaatsen.