DOTNET4PY – A .NET extension for Python

Introduction

Python is a high level, object oriented and portable language. The dotnet4py library is an extension to Python which allows Python to interact with the .NET runtime. Using this library, one can, from Python:

  1. Access .Net assemblies (can load and use assemblies in both DLL and EXE formats)
  2. Instantiate .NET objects
  3. Invoke .NET APIs on the objects

 

All of this is done using the Python syntax which is very similar to that of C# or Java, but with a few differences.

Prerequisites

Software

  • Python 2.3 - Windows
  • MS-Windows with .NET framework
  • DotNet4Py 3.22 or higher

Platform

  • MS-Windows, .NET
  • Libraries compiled with Mono but which work under the Microsoft Windows environment should also work (untested)

Knowledge

  • Knowledge of Python, Python syntax

Overview

 

Here’s a simple example where we will invoke an API on a .NET class:

  1. We import the dotnet4pylib module
  2. Import the target class from the desired assembly
  3. Instantiate an object of that class
  4. Invoke a method on that object

 


 

C:\>python

Python 2.3b1 (#40, Apr 25 2003, 19:06:24) [MSC v.1200 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import dotnet4pylib

dotnet4py - A Python Wrapper for .NET

Copyright (C) 2003 Chetan Gadgil (chetan at gadgil dot net)

All Rights Reserved

Warning!!! Environment variable DOTNET4PY_ASSEMBLY_PATH not set. Setting default to '.'

>>>

Step 1: We import the dotnet4pylib module

 

Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import dotnet4pylib

dotnet4py - A Python Wrapper for .NET

Copyright (C) 2003 Chetan Gadgil (chetan at gadgil dot net)

All Rights Reserved

>>> from CLR.System.Xml.dll.System.Xml import XmlDocument

>>> doc = XmlDocument()

>>> doc.LoadXml("<tag>contents</tag>")

>>>

Step 2. Import the target class from the desired assembly and instantiate an object

 

>>> dir(doc)

['.ctor', 'AppendChild', 'Attributes', 'BaseURI', 'ChildNodes', 'Clone', 'CloneN

ode', 'CreateAttribute', 'CreateCDataSection', 'CreateComment', 'CreateDocumentF

ragment', 'CreateDocumentType', 'CreateElement', 'CreateEntityReference', 'Creat

eNavigator', 'CreateNode', 'CreateProcessingInstruction', 'CreateSignificantWhit

espace', 'CreateTextNode', 'CreateWhitespace', 'CreateXmlDeclaration', 'Document

Element', 'DocumentType', 'Equals', 'FirstChild', 'GetElementById', 'GetElements

ByTagName', 'GetEnumerator', 'GetHashCode', 'GetNamespaceOfPrefix', 'GetPrefixOf

Namespace', 'GetType', 'HasChildNodes', 'Implementation', 'ImportNode', 'InnerTe

xt', 'InnerXml', 'InsertAfter', 'InsertBefore', 'IsReadOnly', 'Item', 'LastChild

', 'Load', 'LoadXml', 'LocalName', 'Name', 'NameTable', 'NamespaceURI', 'NextSib

ling', 'NodeChanged', 'NodeChanging', 'NodeInserted', 'NodeInserting', 'NodeRemo

ved', 'NodeRemoving', 'NodeType', 'Normalize', 'OuterXml', 'OwnerDocument', 'Par

entNode', 'Prefix', 'PrependChild', 'PreserveWhitespace', 'PreviousSibling', 'Re

adNode', 'RemoveAll', 'RemoveChild', 'ReplaceChild', 'Save', 'SelectNodes', 'Sel

ectSingleNode', 'Supports', 'ToString', 'Value', 'WriteContentTo', 'WriteTo', 'X

mlResolver', '__doc__', '__getitem__', '__init__', '__len__', '__module__', 'add

_NodeChanged', 'add_NodeChanging', 'add_NodeInserted', 'add_NodeInserting', 'add

_NodeRemoved', 'add_NodeRemoving', 'dotnet4pyObj', 'fillMyDict', 'get_Attributes

', 'get_BaseURI', 'get_ChildNodes', 'get_DocumentElement', 'get_DocumentType', '

get_FirstChild', 'get_HasChildNodes', 'get_Implementation', 'get_InnerText', 'ge

t_InnerXml', 'get_IsReadOnly', 'get_Item', 'get_LastChild', 'get_LocalName', 'ge

t_Name', 'get_NameTable', 'get_NamespaceURI', 'get_NextSibling', 'get_NodeType',

 'get_OuterXml', 'get_OwnerDocument', 'get_ParentNode', 'get_Prefix', 'get_Prese

rveWhitespace', 'get_PreviousSibling', 'get_Value', 'remove_NodeChanged', 'remov

e_NodeChanging', 'remove_NodeInserted', 'remove_NodeInserting', 'remove_NodeRemo

ved', 'remove_NodeRemoving', 'set_InnerText', 'set_InnerXml', 'set_Prefix', 'set

_PreserveWhitespace', 'set_Value', 'set_XmlResolver']

>>>

 

 

Step 3 Inspect available .NET methods/properties/attributes