SlideShare a Scribd company logo
1 of 25
INTRODUCTION TO
PYTHON.NET
ABOUT ME
• Stefan Schukat
• Software Architect, Physicist
• Different programming stages over the last 30 years
• Pascal, TCL/TK
• C/C++, ATL/MFC, COM, (VB)
• Python
• M
• C#
• Interest in software forensic and cross language techniques
• Twitter @tschodila
• https://www.xing.com/profile/Stefan_Schukat2
WHY CONSIDER PYTHON?
BASE OF PYTHON
• High level interpreted language
• Dynamic execution with no static typing
• Only few basic data types and programming elements are
defined
• Interpreter engine could be implemented on different
languages
• Extendable and embeddable
BASE PYTHON EXECUTION ARCHITECTURE
Python Code
Byte Code
Python API
Parse
Interpreter
Use
Extension
modul
Call
Native API
Call Call
C : C-Python
C# : IronPython
Java : Jython
Python : PyPy
PYTHON .NET
• Python Extension module for C-Python
• Access .NET runtime and libraries from Python scripting
• Combines history and amount of extensions from C-Python
with .NET
• Allows scripting access to .NET libraries in C-Python
• Allows embedding in console .NET Application to use C-Python
in .NET
• https://github.com/pythonnet/pythonnet
PYTHON.NET HISTORY
• Developer Brian Lloyd, Barton Cline, Christian Heimes
• Goal integrate .NET seamlessly in Python on Mono and Windows
• 1.0 for .NET 1.0 (2005), .NET 2.0 (2006)
• 2.0 Beta .NET 4.0 (2013) project abandoned
• Revived 2014 on GitHub
• Denis Akhiyarov, David Anthoff, Tony Roberts, Victor Uriarte
• 2.1 for .NET 4.x, Python 2.x, 3.x (2016)
• 2.2 for .NET 4.x, Added Python 3.6 (2017)
• .NET Core support is planned
PYTHON.NET ARCHITECTURE
clr.pyd
.NET
Assembly
Dynam
ic Load
PyInit_Clr
Python.Runtime.
dll
.NET Assembly
C-Export
for Python
Dll Import Python C-API
Wrapper Objects for Python Type APIs
Import Hook
Type Conversions
Reflection
…
PYTHON.NET RUNTIME OVERVIEW
DllImport("python.dll",
…)
DllImport("python.dll",
…)
…
Runtime
InitExt(…)
Engine PySequence
PyDict
ModuleObject
ClassObject
PropertyObject
…
Class Wrapper
for .NET Types
P-Invoke
Wrapper for C-
API
Class Wrapper
for Python
Types
…
Main entry point
Entry from Python Translation Python/NET Access Python-API
PYTHON .NET BASICS
• Uses reflection to initialize Python wrappers from .NET objects
• All public and protected members are available
• In case of name clash static method are preferred over instance methods
• Maps .NET namespaces to Python modules via Python import
hook
• Import statement prefers Python modules over .NET modules
• All variables in Python are reference types
• Any value type is always boxed in Python.NET
• Default mapping of data types between Python and BCL
TYPES MAPPING
.NET Type Python Type
String, Char unicode
Int16, UInt16, Int32, UInt32, Byte, SByte int
Int64, UInt64, UInt32 long (Py 2.x), int (Py 3.x)
Boolean bool
Single, Double float
IEnumerable list (sequence protocol)
null None
Decimal object
Struct decimal
ACCESSING NAMESPACE / ASSEMBLIES
>>> import clr # Initialize Runtime
>>> from System import String # Import Class from namespace
>>> from System.Collections import * # Import all from subnamespace
>>> clr.AddReference("System.Windows.Forms") # Add assembly
<System.Reflection.RuntimeAssembly object at 0x0000020A5FF06CF8>
>>> import System.Windows.Forms
>>> clr.AddReference("customsigned, Version=1.5.0.0, Culture=neutral,
PublicKeyToken=12345678babeaffe")
• Search order
• Standard Python modules
• Loaded Assemblies
• Python path for .NET assemblies (Assembly.LoadFrom)
• .NET Loader (Assembly.Load)
STRUCTS, METHODS, PROPERTIES
>>> import System.Drawing import Point # Import struct
>>> p = Point(5, 5) # Instantiate struct
>>> p.X # Access property
5
>>> from System import Math # Import static class
>>> Math.Abs(-212) # Access Method
212
GENERICS AND OVERLOADS
>>> from System import String, Char, Int32 # Import used types
>>> s = String.Overloads[Char, Int32]("A", 10) # Use overloaded constructor
>>> s # display class
<System.String object at 0x0000020A5FF64908>
>>> print(s) # Use ToString method
AAAAAAAA
>>> from System.Collections.Generic import Dictionary # Import used types
>>> d = Dictionary[String, String]() # Use generic constructor
>>> d2 = Dictionary[str, str]() # Use auto mapped types
>>> print(d) # Use ToString method
System.Collections.Generic.Dictionary`2[System.String,System.String]
INDEXERS, ARRAYS
>>> from System.Collections.Generic import Dictionary as d
>>> jagged = d[str, d[str, int]]() # Create dict of dicts
>>> jagged["a"] = d[str, int]()
>>> jagged["a"]["b"] = 10
>>> jagged["a"]["b"]
10
>>> from System import Array
>>> a = Array[int]([2,2])
>>> a
<System.Int32[] object at 0x0000020A5FF838D0>
>>> a[0] # Multidimensional a[1,1]
2
DELEGATES AND EVENTS
>>> from System import AssemblyLoadEventHandler, AppDomain
>>> def LoadHandler(source, args):
... print("Python called for {0}".format(args.LoadAssembly.FullName))
...
>>> e = AssemblyLoadEventHandler(LoadHandler) # Create delegate
>>> AppDomain.CurrentDomain.AssemblyLoad += e # Register delegate
>>> clr.AddReference("System.Windows.Forms") # Add assembly
Python called for Accessibility, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a
Python called for System.Windows.Forms, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089
Python called for System.Drawing, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a
>>> AppDomain.CurrentDomain.AssemblyLoad -= e # Unregister delegate
EXCEPTIONS
>>> from System import NullReferenceException # Use BCL exception
>>> try:
... raise NullReferenceException("Empty test")
... except NullReferenceException as e:
... print(e.Message)
... print(e.Source)
Empty test
None
COLLECTIONS
>>> from System import AppDomain # Use class which supports IEnumerable
>>> for item in AppDomain.CurrentDomain.GetAssemblies():
... print(item.FullName)
...
mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
clrmodule, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null
Python.Runtime, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null
System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
__CodeGenerator_Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
e__NativeCall_Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
…
UI PYTHON .NET
• Simple way
• Write .NET Class Assembly with UI (Winforms or WPF)
• Export functions to be used
• Hard way
• Rewrite generated code via Python.NET
UI WINFORMS C# CODE
using System;
using System.Windows.Forms;
namespace UILibrary
{
public partial class MainForm : Form
{
public Action PythonCallBack { get; set; }
public MainForm()
{
InitializeComponent();
}
private void ButtonCallPython_Click(object sender, System.EventArgs e)
=> this.PythonCallBack?.Invoke();
}
}
UI WINFORMS PYTHON CODE IN UI
APPLICATIONimport clr
clr.AddReference("UILibrary")
import UILibrary
from System import Action
def CallBack():
"""Button click event handler"""
print("Button clicked!")
MainForm = UILibrary.MainForm()
MainForm.PythonCallBack = Action(CallBack)
MainForm.ShowDialog()
UI WINFORMS PYTHON CODE IN CONSOLE
APPimport clr
clr.AddReference("System.Windows.Forms")
clr.AddReference("UILibrary")
import System.Windows.Forms as WinForms
import UILibrary
from System import Action
class WinFormsTest:
def __init__(self):
self.MainForm = UILibrary.MainForm()
self.MainForm.PythonCallBack = Action(self.CallBack)
app = WinForms.Application
app.Run(self.MainForm)
def CallBack(self):
"""Button click event handler"""
print("Button clicked!")
if __name__ == '__main__':
ui = WinFormsTest()
WINFORMS PURE PYTHON
self.buttonCallPython.Click +=
System.EventHandler(self.ButtonCallPython_Click)
self.AutoScaleDimensions =
System.Drawing.SizeF(6.0, 13.0)
self.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font
self.ClientSize = System.Drawing.Size(245, 44)
self.Controls.Add(self.buttonCallPython)
self.MaximizeBox = False
self.MinimizeBox = False
self.Name = "MainForm"
self.Text = "TestWinforms"
self.ResumeLayout(False)
if __name__ == "__main__":
m = MainForm()
m.ShowDialog()
import clr
import System
import System.Windows.Forms
import System.Drawing
class MainForm(System.Windows.Forms.Form):
def __init__(self):
self.InitializeComponent()
def ButtonCallPython_Click(self, source, args):
print("Button clicked")
def InitializeComponent(self):
self.buttonCallPython =
System.Windows.Forms.Button()
self.SuspendLayout()
self.buttonCallPython.Location =
System.Drawing.Point(12, 12)
self.buttonCallPython.Name = "buttonCallPython"
self.buttonCallPython.Size =
System.Drawing.Size(75, 23)
self.buttonCallPython.TabIndex = 0
self.buttonCallPython.Text = "Call Python"
self.buttonCallPython.UseVisualStyleBackColor =
True
FURTHER USE CASES
• Automatic conversion of Python dictionary as .NET dictionary
• Usage of .NET enums as Python enums
• Routing of win32com.client COM objects as __ComObject
THANK YOU!
???????

More Related Content

What's hot

O domínio da tecnologia
O domínio da tecnologiaO domínio da tecnologia
O domínio da tecnologia
Professor
 
Design de interação e usabilidade
Design de interação e usabilidadeDesign de interação e usabilidade
Design de interação e usabilidade
InformantTalks
 

What's hot (20)

Princípios de Design de Interação
Princípios de Design de InteraçãoPrincípios de Design de Interação
Princípios de Design de Interação
 
最近のUIデザインプロセス
最近のUIデザインプロセス最近のUIデザインプロセス
最近のUIデザインプロセス
 
02 algoritmo
02   algoritmo02   algoritmo
02 algoritmo
 
あの日見たスライドの作り方を僕達はまだ知らない
あの日見たスライドの作り方を僕達はまだ知らないあの日見たスライドの作り方を僕達はまだ知らない
あの日見たスライドの作り方を僕達はまだ知らない
 
ユーザエクスペリエンスを正しく理解する-UXとUXデザイン
ユーザエクスペリエンスを正しく理解する-UXとUXデザインユーザエクスペリエンスを正しく理解する-UXとUXデザイン
ユーザエクスペリエンスを正しく理解する-UXとUXデザイン
 
História dos Sistemas Operativos
História dos Sistemas OperativosHistória dos Sistemas Operativos
História dos Sistemas Operativos
 
Módulo 1 de PSI
Módulo 1 de PSIMódulo 1 de PSI
Módulo 1 de PSI
 
プレゼンテーション講義スライド
プレゼンテーション講義スライドプレゼンテーション講義スライド
プレゼンテーション講義スライド
 
登録数2倍にしてと言われた時の正しい対処法
登録数2倍にしてと言われた時の正しい対処法登録数2倍にしてと言われた時の正しい対処法
登録数2倍にしてと言われた時の正しい対処法
 
O domínio da tecnologia
O domínio da tecnologiaO domínio da tecnologia
O domínio da tecnologia
 
ユーザエクスペリエンス~それは何か・どう捉え・どう開発につなげるのか?
ユーザエクスペリエンス~それは何か・どう捉え・どう開発につなげるのか?ユーザエクスペリエンス~それは何か・どう捉え・どう開発につなげるのか?
ユーザエクスペリエンス~それは何か・どう捉え・どう開発につなげるのか?
 
Linha do tempo
Linha do tempoLinha do tempo
Linha do tempo
 
ディレクターのためのUX基本講座〜カスタマージャーニーマップを体験してみよう〜
ディレクターのためのUX基本講座〜カスタマージャーニーマップを体験してみよう〜ディレクターのためのUX基本講座〜カスタマージャーニーマップを体験してみよう〜
ディレクターのためのUX基本講座〜カスタマージャーニーマップを体験してみよう〜
 
Redmineで始めるチケット駆動開発
Redmineで始めるチケット駆動開発Redmineで始めるチケット駆動開発
Redmineで始めるチケット駆動開発
 
UX専門家から見た「Running Lean」の改善ポイント 〜「Lean Customer Development」と読み比べ〜:2015年10月9日 ...
UX専門家から見た「Running Lean」の改善ポイント 〜「Lean Customer Development」と読み比べ〜:2015年10月9日 ...UX専門家から見た「Running Lean」の改善ポイント 〜「Lean Customer Development」と読み比べ〜:2015年10月9日 ...
UX専門家から見た「Running Lean」の改善ポイント 〜「Lean Customer Development」と読み比べ〜:2015年10月9日 ...
 
KPI から生まれるアクセシビリティ
KPI から生まれるアクセシビリティKPI から生まれるアクセシビリティ
KPI から生まれるアクセシビリティ
 
Design de interação e usabilidade
Design de interação e usabilidadeDesign de interação e usabilidade
Design de interação e usabilidade
 
Modelagem 3D e Blender
Modelagem 3D e Blender Modelagem 3D e Blender
Modelagem 3D e Blender
 
Mercado de TI: Carreiras, atuação e formação
Mercado de TI: Carreiras, atuação e formaçãoMercado de TI: Carreiras, atuação e formação
Mercado de TI: Carreiras, atuação e formação
 
Aula I - Introdução à hardware e à software
Aula I - Introdução à hardware e à software Aula I - Introdução à hardware e à software
Aula I - Introdução à hardware e à software
 

Similar to Introduction to Python.Net

Format of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptxFormat of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptx
MOHAMMADANISH12
 
Introduction to Python.pdf
Introduction to Python.pdfIntroduction to Python.pdf
Introduction to Python.pdf
Rahul Mogal
 

Similar to Introduction to Python.Net (20)

Introduction to python.pptx
Introduction to python.pptxIntroduction to python.pptx
Introduction to python.pptx
 
Programming using c++ tool
Programming using c++ toolProgramming using c++ tool
Programming using c++ tool
 
Lecture1_introduction to python.pptx
Lecture1_introduction to python.pptxLecture1_introduction to python.pptx
Lecture1_introduction to python.pptx
 
Iron python
Iron pythonIron python
Iron python
 
Pemrograman Python untuk Pemula
Pemrograman Python untuk PemulaPemrograman Python untuk Pemula
Pemrograman Python untuk Pemula
 
Python Basis Tutorial
Python Basis TutorialPython Basis Tutorial
Python Basis Tutorial
 
Pythonpresent
PythonpresentPythonpresent
Pythonpresent
 
Introduction to cython
Introduction to cythonIntroduction to cython
Introduction to cython
 
Preprocessor , IOSTREAM Library,IOMANIP Library
Preprocessor , IOSTREAM Library,IOMANIP LibraryPreprocessor , IOSTREAM Library,IOMANIP Library
Preprocessor , IOSTREAM Library,IOMANIP Library
 
Anish PPT-GIT.pptx
Anish PPT-GIT.pptxAnish PPT-GIT.pptx
Anish PPT-GIT.pptx
 
Format of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptxFormat of first slide for main PPT-GIT.pptx
Format of first slide for main PPT-GIT.pptx
 
PPT-GIT.pptx
PPT-GIT.pptxPPT-GIT.pptx
PPT-GIT.pptx
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
web programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Malothweb programming UNIT VIII python by Bhavsingh Maloth
web programming UNIT VIII python by Bhavsingh Maloth
 
05 python.pdf
05 python.pdf05 python.pdf
05 python.pdf
 
Revision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdfRevision of the basics of python1 (1).pdf
Revision of the basics of python1 (1).pdf
 
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...Python Functions Tutorial | Working With Functions In Python | Python Trainin...
Python Functions Tutorial | Working With Functions In Python | Python Trainin...
 
Introduction to Python.pdf
Introduction to Python.pdfIntroduction to Python.pdf
Introduction to Python.pdf
 
Python ppt
Python pptPython ppt
Python ppt
 
PYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdfPYTHON PROGRAMMING NOTES RKREDDY.pdf
PYTHON PROGRAMMING NOTES RKREDDY.pdf
 

Recently uploaded

Recently uploaded (20)

WSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go PlatformlessWSO2CON2024 - It's time to go Platformless
WSO2CON2024 - It's time to go Platformless
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
WSO2Con2024 - GitOps in Action: Navigating Application Deployment in the Plat...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
%in Stilfontein+277-882-255-28 abortion pills for sale in Stilfontein
 
Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2Driving Innovation: Scania's API Revolution with WSO2
Driving Innovation: Scania's API Revolution with WSO2
 
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
WSO2Con2024 - From Blueprint to Brilliance: WSO2's Guide to API-First Enginee...
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & InnovationWSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
WSO2CON 2024 - OSU & WSO2: A Decade Journey in Integration & Innovation
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
WSO2CON 2024 - Lessons from the Field: Legacy Platforms – It's Time to Let Go...
 

Introduction to Python.Net

  • 2. ABOUT ME • Stefan Schukat • Software Architect, Physicist • Different programming stages over the last 30 years • Pascal, TCL/TK • C/C++, ATL/MFC, COM, (VB) • Python • M • C# • Interest in software forensic and cross language techniques • Twitter @tschodila • https://www.xing.com/profile/Stefan_Schukat2
  • 4. BASE OF PYTHON • High level interpreted language • Dynamic execution with no static typing • Only few basic data types and programming elements are defined • Interpreter engine could be implemented on different languages • Extendable and embeddable
  • 5. BASE PYTHON EXECUTION ARCHITECTURE Python Code Byte Code Python API Parse Interpreter Use Extension modul Call Native API Call Call C : C-Python C# : IronPython Java : Jython Python : PyPy
  • 6. PYTHON .NET • Python Extension module for C-Python • Access .NET runtime and libraries from Python scripting • Combines history and amount of extensions from C-Python with .NET • Allows scripting access to .NET libraries in C-Python • Allows embedding in console .NET Application to use C-Python in .NET • https://github.com/pythonnet/pythonnet
  • 7. PYTHON.NET HISTORY • Developer Brian Lloyd, Barton Cline, Christian Heimes • Goal integrate .NET seamlessly in Python on Mono and Windows • 1.0 for .NET 1.0 (2005), .NET 2.0 (2006) • 2.0 Beta .NET 4.0 (2013) project abandoned • Revived 2014 on GitHub • Denis Akhiyarov, David Anthoff, Tony Roberts, Victor Uriarte • 2.1 for .NET 4.x, Python 2.x, 3.x (2016) • 2.2 for .NET 4.x, Added Python 3.6 (2017) • .NET Core support is planned
  • 8. PYTHON.NET ARCHITECTURE clr.pyd .NET Assembly Dynam ic Load PyInit_Clr Python.Runtime. dll .NET Assembly C-Export for Python Dll Import Python C-API Wrapper Objects for Python Type APIs Import Hook Type Conversions Reflection …
  • 9. PYTHON.NET RUNTIME OVERVIEW DllImport("python.dll", …) DllImport("python.dll", …) … Runtime InitExt(…) Engine PySequence PyDict ModuleObject ClassObject PropertyObject … Class Wrapper for .NET Types P-Invoke Wrapper for C- API Class Wrapper for Python Types … Main entry point Entry from Python Translation Python/NET Access Python-API
  • 10. PYTHON .NET BASICS • Uses reflection to initialize Python wrappers from .NET objects • All public and protected members are available • In case of name clash static method are preferred over instance methods • Maps .NET namespaces to Python modules via Python import hook • Import statement prefers Python modules over .NET modules • All variables in Python are reference types • Any value type is always boxed in Python.NET • Default mapping of data types between Python and BCL
  • 11. TYPES MAPPING .NET Type Python Type String, Char unicode Int16, UInt16, Int32, UInt32, Byte, SByte int Int64, UInt64, UInt32 long (Py 2.x), int (Py 3.x) Boolean bool Single, Double float IEnumerable list (sequence protocol) null None Decimal object Struct decimal
  • 12. ACCESSING NAMESPACE / ASSEMBLIES >>> import clr # Initialize Runtime >>> from System import String # Import Class from namespace >>> from System.Collections import * # Import all from subnamespace >>> clr.AddReference("System.Windows.Forms") # Add assembly <System.Reflection.RuntimeAssembly object at 0x0000020A5FF06CF8> >>> import System.Windows.Forms >>> clr.AddReference("customsigned, Version=1.5.0.0, Culture=neutral, PublicKeyToken=12345678babeaffe") • Search order • Standard Python modules • Loaded Assemblies • Python path for .NET assemblies (Assembly.LoadFrom) • .NET Loader (Assembly.Load)
  • 13. STRUCTS, METHODS, PROPERTIES >>> import System.Drawing import Point # Import struct >>> p = Point(5, 5) # Instantiate struct >>> p.X # Access property 5 >>> from System import Math # Import static class >>> Math.Abs(-212) # Access Method 212
  • 14. GENERICS AND OVERLOADS >>> from System import String, Char, Int32 # Import used types >>> s = String.Overloads[Char, Int32]("A", 10) # Use overloaded constructor >>> s # display class <System.String object at 0x0000020A5FF64908> >>> print(s) # Use ToString method AAAAAAAA >>> from System.Collections.Generic import Dictionary # Import used types >>> d = Dictionary[String, String]() # Use generic constructor >>> d2 = Dictionary[str, str]() # Use auto mapped types >>> print(d) # Use ToString method System.Collections.Generic.Dictionary`2[System.String,System.String]
  • 15. INDEXERS, ARRAYS >>> from System.Collections.Generic import Dictionary as d >>> jagged = d[str, d[str, int]]() # Create dict of dicts >>> jagged["a"] = d[str, int]() >>> jagged["a"]["b"] = 10 >>> jagged["a"]["b"] 10 >>> from System import Array >>> a = Array[int]([2,2]) >>> a <System.Int32[] object at 0x0000020A5FF838D0> >>> a[0] # Multidimensional a[1,1] 2
  • 16. DELEGATES AND EVENTS >>> from System import AssemblyLoadEventHandler, AppDomain >>> def LoadHandler(source, args): ... print("Python called for {0}".format(args.LoadAssembly.FullName)) ... >>> e = AssemblyLoadEventHandler(LoadHandler) # Create delegate >>> AppDomain.CurrentDomain.AssemblyLoad += e # Register delegate >>> clr.AddReference("System.Windows.Forms") # Add assembly Python called for Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Python called for System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Python called for System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a >>> AppDomain.CurrentDomain.AssemblyLoad -= e # Unregister delegate
  • 17. EXCEPTIONS >>> from System import NullReferenceException # Use BCL exception >>> try: ... raise NullReferenceException("Empty test") ... except NullReferenceException as e: ... print(e.Message) ... print(e.Source) Empty test None
  • 18. COLLECTIONS >>> from System import AppDomain # Use class which supports IEnumerable >>> for item in AppDomain.CurrentDomain.GetAssemblies(): ... print(item.FullName) ... mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 clrmodule, Version=2.3.0.0, Culture=neutral, PublicKeyToken=null Python.Runtime, Version=2.4.0.0, Culture=neutral, PublicKeyToken=null System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 __CodeGenerator_Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null e__NativeCall_Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a …
  • 19. UI PYTHON .NET • Simple way • Write .NET Class Assembly with UI (Winforms or WPF) • Export functions to be used • Hard way • Rewrite generated code via Python.NET
  • 20. UI WINFORMS C# CODE using System; using System.Windows.Forms; namespace UILibrary { public partial class MainForm : Form { public Action PythonCallBack { get; set; } public MainForm() { InitializeComponent(); } private void ButtonCallPython_Click(object sender, System.EventArgs e) => this.PythonCallBack?.Invoke(); } }
  • 21. UI WINFORMS PYTHON CODE IN UI APPLICATIONimport clr clr.AddReference("UILibrary") import UILibrary from System import Action def CallBack(): """Button click event handler""" print("Button clicked!") MainForm = UILibrary.MainForm() MainForm.PythonCallBack = Action(CallBack) MainForm.ShowDialog()
  • 22. UI WINFORMS PYTHON CODE IN CONSOLE APPimport clr clr.AddReference("System.Windows.Forms") clr.AddReference("UILibrary") import System.Windows.Forms as WinForms import UILibrary from System import Action class WinFormsTest: def __init__(self): self.MainForm = UILibrary.MainForm() self.MainForm.PythonCallBack = Action(self.CallBack) app = WinForms.Application app.Run(self.MainForm) def CallBack(self): """Button click event handler""" print("Button clicked!") if __name__ == '__main__': ui = WinFormsTest()
  • 23. WINFORMS PURE PYTHON self.buttonCallPython.Click += System.EventHandler(self.ButtonCallPython_Click) self.AutoScaleDimensions = System.Drawing.SizeF(6.0, 13.0) self.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font self.ClientSize = System.Drawing.Size(245, 44) self.Controls.Add(self.buttonCallPython) self.MaximizeBox = False self.MinimizeBox = False self.Name = "MainForm" self.Text = "TestWinforms" self.ResumeLayout(False) if __name__ == "__main__": m = MainForm() m.ShowDialog() import clr import System import System.Windows.Forms import System.Drawing class MainForm(System.Windows.Forms.Form): def __init__(self): self.InitializeComponent() def ButtonCallPython_Click(self, source, args): print("Button clicked") def InitializeComponent(self): self.buttonCallPython = System.Windows.Forms.Button() self.SuspendLayout() self.buttonCallPython.Location = System.Drawing.Point(12, 12) self.buttonCallPython.Name = "buttonCallPython" self.buttonCallPython.Size = System.Drawing.Size(75, 23) self.buttonCallPython.TabIndex = 0 self.buttonCallPython.Text = "Call Python" self.buttonCallPython.UseVisualStyleBackColor = True
  • 24. FURTHER USE CASES • Automatic conversion of Python dictionary as .NET dictionary • Usage of .NET enums as Python enums • Routing of win32com.client COM objects as __ComObject

Editor's Notes

  1. [DllExport("PyInit_clr", CallingConvention.StdCall)] public static IntPtr PyInit_clr()
  2. [DllExport("PyInit_clr", CallingConvention.StdCall)] public static IntPtr PyInit_clr()
  3. Search sequence with given name Python path + name + (.dll|.exe) (Assembly.LoadFrom) .NET loader (Assembly.Load)
  4. Any error in a delegate is swallowed
  5. Any error in a delegate is swallowed
  6. Any error in a delegate is swallowed
  7. Any error in a delegate is swallowed
  8. Any error in a delegate is swallowed
  9. Any error in a delegate is swallowed
  10. Any error in a delegate is swallowed