Python with dot net and vs2010

2,139
-1

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,139
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
31
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Python with dot net and vs2010

  1. 1. Python with DotNet and VS2010
  2. 2. Python tools for VisualStudio2010 <ul><li>http://pytools.codeplex.com/ </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>http://ironpython.codeplex.com/ </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>http://ironpython.net/documentation/dotnet/dotnet.html </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;Calculator.py&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>http://ironpython.codeplex.com/releases/view/12482 </li></ul>
  18. 22. App and ViewModel
  19. 24. Diskusage - WPF
  20. 26. IronPython with SilverLight http://www.silverlight.net/learn/advanced-techniques/dynamic-languages/dynamic-languages-in-silverlight#example_applications

×