IronPython
From Wikipedia, the free encyclopedia
IronPython | |
Latest release: | 1.0.1 / October 07, 2006 |
---|---|
Platform: | .NET and Mono |
Use: | Python Programming Language Interpreter |
Website: | http://www.codeplex.com/Wiki/View.aspx?ProjectName=IronPython |
IronPython is an implementation of the Python programming language, targeting .NET and Mono, created by Jim Hugunin. Version 1.0 was released on September 5, 2006 [1].
IronPython is written entirely in C# and is made available as part of Microsoft's Shared Source initiative. While IronPython was originally released under the Common Public License, it retains some of its open source heritage, and its source code seems to be "more accessible" than other projects that are offered under the Shared Source initiative. Authors claim [2] that the license, while not reviewed by the Open Source Initiative, conforms to the OSI's definition of open source.
Contents |
[edit] Interface Extensibility
One of IronPython's key advantages is in its function as an extensibility layer to application frameworks written in a .NET language. It is relatively simple to integrate an IronPython interpreter into an existing .NET application framework. Once in place, downstream developers can use scripts written in IronPython that interact with .NET objects in the framework, thereby extending the functionality in the framework's interface, without having to change any of the framework's code base.
IronPython makes extensive use of reflection. When passed in a reference to a .NET object, it will automatically import the types and methods available to that object. This results in a highly intuitive experience when working with .NET objects from within an IronPython script.
[edit] Example
The following IronPython script manipulates .NET framework objects. This script can be supplied by a third-party client-side application developer and passed into the server-side framework through an interface. Note that neither the interface, nor the server-side code is modified to support the analytics required by the client application.
from BookService import BookDictionary booksWrittenByBookerPrizeWinners = [] for book in BookDictionary.GetAllBooks: if "Booker Prize" in book.Author.MajorAwards: booksWrittenByBookerPrizeWinners.append(book.Title) booksWrittenByBookerPrizeWinners
In this case, assume that the .NET framework implements a class, BookDictionary, in a module called BookService, and publishes an interface into which IronPython scripts can be sent and executed.
This script, when sent to that interface, will iterate over the entire list of books maintained by the framework, and pick out those written by Booker Prize-winning authors.
What's interesting is that the responsibility for writing the actual analytics reside with the client-side developer. The demands on the server-side developer are minimal, essentially just providing access to the data maintained by the server. This design pattern greatly simplifies the deployment and maintenance of complex application frameworks.
[edit] Simple Windows Forms application
The following example illustrates how IronPython can be used in tandem with Windows Forms (from the tutorial document included with the Beta 2 release):
Start the IronPython console ("Iron Python Console.bat" or ip.bat), and initialize Windows Forms by loading the winforms module/script:
import winforms
The Python modules get automatically initialized (executed) upon import so the Windows Forms initialization code has executed as part of the import statement.
Import the contents of the System.Windows.Forms and System.Drawing namespaces into the global namespace:
import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import * from System.Drawing import *
Create an instance of the Form class and display it:
f = Form() f.Show()
Now set the form Text property:
f.Text = "My First Interactive Application"
To bring the application alive, let's focus on the Click event of the form. Create an event handler for the Click event and click on the form to receive the event. Then remove the event handler:
def click(*args): print args f.Click += click
Click on the form to receive the event. The output will be similar to:
>>> (System.Windows.Forms.Form, Text: My First Interactive Application, <System.Windows.Forms.MouseEventArgs object at 0x02324551>)
You can also remove this event handler as follows:
f.Click -= click
You may use the dir() function to explore the functionality exposed by the MouseEventArgs class:
dir(MouseEventArgs)
Knowing the contents of the MouseEventArgs, we are able to create an improved event handler for the Click event:
def click(f, a): l = Label(Text = "Hello") l.Location = a.Location f.Controls.Add(l)
Registering this all-new event handler:
f.Click += click
Now clicking on the form with the mouse will add "Hello" labels. We can also access the controls we added via mouse clicks and change them by using the Controls collection:
for i in f.Controls: i.Font = Font("Verdana", 15) for i in f.Controls: i.Text = "Hi"
After a few moments of clicking, the form will get quite crowded, so we can clear it out:
f.Controls.Clear()
[edit] References
- ^ Jim Hugunin's blog: IronPython 1.0 released today! (September 05, 2006). Retrieved on 2006-12-14.
- ^ Shared Source License for IronPython (April 28, 2006). Retrieved on 2006-09-07.
[edit] See also
- Python Programming Language
- Boo, a language for .NET and Mono with Python-inspired syntax and features borrowed from C# and Ruby.
- Jython - an implementation of Python for the JVM.
[edit] External links
- CodePlex IronPython Workspace (new development home)
- GotDotNet IronPython Workspace (ongoing development)
- Original IronPython Site (page last updated August 9, 2004)
- Visual Studio SDK (includes IronPython - Visual Studio integration sample)
- Tutorial Series on IronPython & Windows Forms
- IronPython IDE Project
- Microsoft Shared Source Initiative
- Oracle Data Provider for .NET under IronPython Przemek Piotrowski Blogging