IronPython 2.0 and Access to .NET Libraries

Cumartesi, 7 Kasım 2009 18:31 by ikivanc
In This Article we’ll learn how to access .NET Libraries from IronPython.

IronPython 1.0 verison on CLR (Common Language Runtime) was really bad and Jim Hugunin who has implemented Python on JVM as Jython,  wrote an article about  “Why the CLR is a terrible platform for dynamic languages.” then he hired for CLR architecture team by Microsoft. He prototyped new architecture so IronPython 2.0 version now runs on DLR (Dynamic Language Runtime). DLR is a platform on .NET which is host Dynamicly typed languages on it. Now Dynamic Languages Communicate eachother and C#,VB, COM Objects, .NET Libraries.

Now we can access .NET Libraries from IronPython and access all .NET objects eachother easily.
 

IronPython, with 2.0 version runs on DLR (Dynamic Language Runtime); it’s a platform like CLR architecture. It’s host for Dynamic Languages on .NET. With this architecture Dynamic Languages now faster then running on CLR and easily communicate with other .NET objects!

 
As you see above, DLR works with .NET 4.0, Programming Languages easily bind .NET, silverlight, Python, Ruby and COM objects.

DLR will be part of .NET 4.0, also with this implementation C# 4.0 will have dynamic type feature.

Access To .NET Libraries
Now we’ll learn how to access .NET Libraries from IronPython. We will develop our application in “IronPython Console”. If it’s not installed in your machine you can get it from http://ironpython.codeplex.com the latest version of IronPython, right now there is 2.0.3 stable version and 2.6 RC2 version.

In Python and IronPython we import libraries with "import" command.

import System.String     #  importing System.String library.
from System import String     # also we can use this import template

with import System.String code we imported String library to our workspace and now we can use this library on IronPython workspace.


We can use one library as above also can use after importing clr like below.
 

Let’s check out Python and .NET string methods; in Python there is a “str” module and in .NET there is System.String Library for string operations.

“str” module has been loaded in IronPython for default work space. To use .NET library “import clr” or “from System import String” namespace. After importing .NET Library, IronPython “str” module is still working.

Without importing clr:

>>> word = "İBRAHİMKİVANC" 
>>>word.lower() 
'ibrahimkivanc' 
>>>word.ToLower()     # we took an error because we didn’t import .NET Libraries
Traceback (most recent call last):
  Filer “<stdin>”, line 1, in <module>
AttributeError: 'str' object has no attribute 'ToLower'

import clr
with this command we imported “clr” module in IronPython workspace. Let’s check out the example..

After importing clr module:

>>> import clr
>>> word.lower()        # python method is also working with clr
'ibrahimkivanc'
>>> word.ToLower()    # now it’s working 
'ibrahimkivanc'

As you see we took an error because we didn’t import clr, after importing clr we can use ToLower() function.


In this Console example, we can save the code as “lib.py” and we can import this code from “app.py” application.
 
# lib.py
import clr
def to_lower(word): return  word.ToLower() 
 
# app.py
import lib
word = "HELLO"
print lib. to_lower (word) # print “hello” to screen
#print word.ToUpper()      # we didn’t import clr to this space  so it throw an error 

PDF version of this article >> 7 - IronPython 2.0 and Access to .NET Libraries

If you have any questions or discover any errors / typos please let me know ik@ibrahimkivanc.com

All The Best!
 

Yorumlar

Yorum ekle


 

  Country flag

biuquote
  • Yorum
  • Canlı önizleme
Loading