Python with dot net and vs2010

Preview:

Citation preview

Python with DotNet and VS2010

Python tools for VisualStudio2010

• http://pytools.codeplex.com/

• Python Tools for Visual Studio turns Visual Studio into a Python IDE.  It's a free & open source plug-in for Visual Studio 2010 from Microsoft's Developer Division.

• PTVS enables developers to use all the major productivity features of Visual Studio to build Python code using either CPython or IronPython and adds new features such as using High Performance Computing clusters to scale your code.

IronPython

• http://ironpython.codeplex.com/ – Latest version 2.7.1

• IronPython is an implementation of the Python programming language running under .NET/Mono and Silverlight/Moonlight.

• It supports an interactive console with fully dynamic compilation. It's well integrated with the rest of the .NET Framework and makes all .NET libraries easily available to Python programmers, while maintaining compatibility with the Python language. There also is Visual Studio tooling integration.

• IronPython is an open source project freely available under the Apache License v2.0. The sources are stored on GitHub as part of the IronLanguagesproject.

Usage

• Use IronPython as scripting of DotNet

• Code example that adds reference

• d = XmlDocument()• d.Load('load.xml')• n = d.SelectNodes('//Puzzle/SavedGames/Game/@caption')• for e in n: print e.Value

• Code example: WPF

• http://ironpython.net/documentation/dotnet/dotnet.html

Using IronPython in C#

• using System;• using IronPython.Hosting;

• public class HelloWorld {• public static void Main(string[] args) {• var engine = Python.CreateEngine();• engine.CreateScriptSourceFromString("print 'hello

world'").Execute();• }• }

DotNet4 dynamic

class Calculator(object): def add(self, a, b): return a + b

=======================string scriptPath = "Calculator.py";ScriptEngine engine = Python.CreateEngine();engine.SetSearchPaths(new string[] { …

ScriptSource source = engine.CreateScriptSourceFromFile(scriptPath);ScriptScope scope = engine.CreateScope();ObjectOperations op = engine.Operations;source.Execute(scope);

dynamic Calculator = scope.GetVariable("Calculator");dynamic calc = Calculator();return calc.add(x,y);

Create assembly with IronPython

• Create C# project, add PythonEngine in code• Load python code file or resource string• Run the PythonEngine

Subclassing .NET types

• class MyClass(System.ICloneable):• pass• o = MyClass()• import clr• clr.GetClrType(System.ICloneable).IsAssignableFrom(o.GetType())

– Output>>>True

• class MyClass(System.ICloneable):• def Clone(self):• return MyClass()• o = MyClass()• o.Clone()

Methods with multiple overloads• import clr• import System• StringComparer = System.Collections.Generic.IEqualityComparer[str]

• class MyComparer(StringComparer):• def GetHashCode(self, *args):• if len(args) == 0:• # Object.GetHashCode() called• return 100

• if len(args) == 1 and type(args[0]) == str:• # StringComparer.GetHashCode() called• return 200

• assert("Should never get here")

• comparer = MyComparer()• getHashCode1 = clr.GetClrType(System.Object).GetMethod("GetHashCode")• args = System.Array[object](["another string"])• getHashCode2 = clr.GetClrType(StringComparer).GetMethod("GetHashCode")

• # Use Reflection to simulate a call to the different overloads• # from another .NET language• getHashCode1.Invoke(comparer, None)• >>>100• getHashCode2.Invoke(comparer, args)

Winform example

IronTunes – WPF example• http://ironpython.codeplex.com/releases/view/12482

App and ViewModel

Diskusage - WPF

IronPython with SilverLight

http://www.silverlight.net/learn/advanced-techniques/dynamic-languages/dynamic-languages-in-silverlight#example_applications