python with dot net and vs2010

26
Python with DotNet and VS2010

Upload: wei-sun

Post on 15-May-2015

2.070 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: Python with dot net and vs2010

Python with DotNet and VS2010

Page 2: Python with dot net and vs2010

Python tools for VisualStudio2010

• http://pytools.codeplex.com/

Page 3: Python with dot net and vs2010

• 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.

Page 4: Python with dot net and vs2010

IronPython

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

Page 5: Python with dot net and vs2010

• 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.

Page 6: Python with dot net and vs2010

Usage

• Use IronPython as scripting of DotNet

Page 7: Python with dot net and vs2010
Page 8: Python with dot net and vs2010

• Code example that adds reference

Page 9: Python with dot net and vs2010
Page 10: Python with dot net and vs2010

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

Page 11: Python with dot net and vs2010

• Code example: WPF

Page 12: Python with dot net and vs2010
Page 13: Python with dot net and vs2010

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

Page 14: Python with dot net and vs2010

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();• }• }

Page 15: Python with dot net and vs2010

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);

Page 16: Python with dot net and vs2010

Create assembly with IronPython

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

Page 17: Python with dot net and vs2010

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()

Page 18: Python with dot net and vs2010

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)

Page 19: Python with dot net and vs2010

Winform example

Page 20: Python with dot net and vs2010
Page 21: Python with dot net and vs2010

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

Page 22: Python with dot net and vs2010

App and ViewModel

Page 23: Python with dot net and vs2010
Page 24: Python with dot net and vs2010

Diskusage - WPF

Page 25: Python with dot net and vs2010
Page 26: Python with dot net and vs2010

IronPython with SilverLight

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