Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Python with dot net and vs2010


Published on

Published in: Technology
  • Be the first to comment

Python with dot net and vs2010

  1. 1. Python with DotNet and VS2010
  2. 2. Python tools for VisualStudio2010 <ul><li> </li></ul>
  3. 3. <ul><li>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. </li></ul><ul><li>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. </li></ul>
  4. 4. IronPython <ul><li> </li></ul><ul><ul><li>Latest version 2.7.1 </li></ul></ul>
  5. 5. <ul><li>IronPython is an implementation of the  Python programming language  running under  .NET / Mono  and  Silverlight / Moonlight . </li></ul><ul><li>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. </li></ul><ul><li>IronPython  is an open source project freely available under the Apache License v2.0. The sources are stored on GitHub as part of the  IronLanguages project. </li></ul>
  6. 6. Usage <ul><li>Use IronPython as scripting of DotNet </li></ul>
  7. 8. <ul><li>Code example that adds reference </li></ul>
  8. 10. <ul><li>d = XmlDocument() </li></ul><ul><li>d.Load('load.xml') </li></ul><ul><li>n = d.SelectNodes('//Puzzle/SavedGames/Game/@caption') </li></ul><ul><li>for e in n: print e.Value </li></ul>
  9. 11. <ul><li>Code example: WPF </li></ul>
  10. 13. <ul><li> </li></ul>
  11. 14. Using IronPython in C# <ul><li>using System; </li></ul><ul><li>using IronPython.Hosting; </li></ul><ul><li>public class HelloWorld { </li></ul><ul><li>public static void Main(string[] args) { </li></ul><ul><li>var engine = Python.CreateEngine(); </li></ul><ul><li>engine.CreateScriptSourceFromString(&quot;print 'hello world'&quot;).Execute(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  12. 15. DotNet4 dynamic class Calculator(object): def add(self, a, b): return a + b ======================= string scriptPath = &quot;;; 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(&quot;Calculator&quot;); dynamic calc = Calculator(); return calc.add(x,y);
  13. 16. Create assembly with IronPython <ul><li>Create C# project, add PythonEngine in code </li></ul><ul><li>Load python code file or resource string </li></ul><ul><li>Run the PythonEngine </li></ul>
  14. 17. Subclassing .NET types <ul><li>class MyClass(System.ICloneable): </li></ul><ul><li>pass </li></ul><ul><li>o = MyClass() </li></ul><ul><li>import clr </li></ul><ul><li>clr.GetClrType(System.ICloneable).IsAssignableFrom(o.GetType()) </li></ul><ul><ul><li>Output>>>True </li></ul></ul><ul><li>class MyClass(System.ICloneable): </li></ul><ul><li>def Clone(self): </li></ul><ul><li>return MyClass() </li></ul><ul><li>o = MyClass() </li></ul><ul><li>o.Clone() </li></ul>
  15. 18. Methods with multiple overloads <ul><li>import clr </li></ul><ul><li>import System </li></ul><ul><li>StringComparer = System.Collections.Generic.IEqualityComparer[str] </li></ul><ul><li>class MyComparer(StringComparer): </li></ul><ul><li>def GetHashCode(self, *args): </li></ul><ul><li>if len(args) == 0: </li></ul><ul><li># Object.GetHashCode() called </li></ul><ul><li>return 100 </li></ul><ul><li>if len(args) == 1 and type(args[0]) == str: </li></ul><ul><li># StringComparer.GetHashCode() called </li></ul><ul><li>return 200 </li></ul><ul><li>assert(&quot;Should never get here&quot;) </li></ul><ul><li>comparer = MyComparer() </li></ul><ul><li>getHashCode1 = clr.GetClrType(System.Object).GetMethod(&quot;GetHashCode&quot;) </li></ul><ul><li>args = System.Array[object]([&quot;another string&quot;]) </li></ul><ul><li>getHashCode2 = clr.GetClrType(StringComparer).GetMethod(&quot;GetHashCode&quot;) </li></ul><ul><li># Use Reflection to simulate a call to the different overloads </li></ul><ul><li># from another .NET language </li></ul><ul><li>getHashCode1.Invoke(comparer, None) </li></ul><ul><li>>>>100 </li></ul><ul><li>getHashCode2.Invoke(comparer, args) </li></ul>
  16. 19. Winform example
  17. 21. IronTunes – WPF example <ul><li> </li></ul>
  18. 22. App and ViewModel
  19. 24. Diskusage - WPF
  20. 26. IronPython with SilverLight