python with dot net and vs2010
TRANSCRIPT
![Page 1: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/1.jpg)
Python with DotNet and VS2010
![Page 3: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/3.jpg)
• 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 5: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/5.jpg)
• 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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/6.jpg)
Usage
• Use IronPython as scripting of DotNet
![Page 7: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/7.jpg)
![Page 8: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/8.jpg)
• Code example that adds reference
![Page 9: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/9.jpg)
![Page 10: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/10.jpg)
• 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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/11.jpg)
• Code example: WPF
![Page 12: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/12.jpg)
![Page 13: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/13.jpg)
• http://ironpython.net/documentation/dotnet/dotnet.html
![Page 14: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/19.jpg)
Winform example
![Page 20: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/20.jpg)
![Page 21: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/21.jpg)
IronTunes – WPF example• http://ironpython.codeplex.com/releases/view/12482
![Page 22: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/22.jpg)
App and ViewModel
![Page 23: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/23.jpg)
![Page 24: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/24.jpg)
Diskusage - WPF
![Page 25: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/25.jpg)
![Page 26: Python with dot net and vs2010](https://reader035.vdocuments.us/reader035/viewer/2022070315/55557bdab4c9058a5a8b50cc/html5/thumbnails/26.jpg)
IronPython with SilverLight
http://www.silverlight.net/learn/advanced-techniques/dynamic-languages/dynamic-languages-in-silverlight#example_applications