Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Python + .NET = IronPython : Page 4

IronPython brings the interactivity and productivity of the Python language to the.NET world.

A Sample IronPython Project
Scripting languages are all about automating repetitive tasks. Python fits right in with that description becuase it simplifies the process of "gluing" things together. The Python standard library includes many different functions that reduce the implemention of complex programs to a simple task of connecting pieces together. For example, one command that demonstrates the simplicity of Python is urlretrieve. It accepts two arguments: URL and Directory. So to grab the home page from yahoo.com and save it in the file temp.html, you would enter:

>>> from urllib import urlretrieve >>> urlretrieve('http://www.yahoo.com/','temp.html')

Project Gutenberg has been around for quite some time and includes a large library of public domain literature. Recently, the site has started to provide audio book versions of some of their more popular books. You can choose from either a version read by a computer (not recommended unless you truly like listening to a computer-generated voice) or a version read by a human.

One of the available audio book collections includes a number of Sir Arthur Conan Doyle's Sherlock Holmes novels, read by one of his countrymen. Each book in the collection consists of chapters stored as individual files. Some of the audio books offer multiple versions of the same chapters at different fidelity levels in case you don't need or don't have the bandwidth to support the higher quality sound and larger file sizes.

For example, the downloadable sample code for this article is an application that shows how to download chapters from one of the Sherlock Holmes books on the Project Gutenberg site. The following code shows how to retrieve a URL and write it out to the MyDir directory:

print 'Downloading...', for x in range(num): print 'Downloading chapter', str(v)+'...', url = source+bok+hz+zfill(str(v), 2)+'.mp3' dir2 = MyDir+'\\'+str(v)+'.mp3' v = v + 1 try: urlretrieve(url, dir2) except: makedirs(MyDir)

Adding code to pop up a Windows Forms dialog to ask for user input is pretty straightforward, but note that you do need to import the System (sys) namespace and load the appropriate assemblies:

Figure 2. MP3 Audio Book GUI: This simple Audio Book Download application retrieves a list of files from the Project Gutenberg Web site and downloads them with a single button click.

import sys sys.LoadAssemblyByName("System.Drawing") sys.LoadAssemblyByName("System.Windows.Forms") from System.Windows.Forms import * from System.Drawing import * frm = Form(Text="IronPython Coding Sample", HelpButton=True, MinimizeBox=False, MaximizeBox=False)

Using the sys.LoadAssemblyByName method to load a CLR assembly is the mechanism that opens up IronPython to the entire .NET runtime. After loading the assembly, you must still use the import function to load the appropriate functions. The command from System.Windows.Forms import * instructs the Python interpreter to get a reference to all (*) the available methods and enumerations within the Drawing namespace.

Predicting the future is never easy, but the future of Python at Microsoft is pretty clear. In the world of open source software the Python language is very much alive and has a vibrant community that embraces and supports it. That will serve to further its adoption both within Microsoft and in the growing .NET developer community as a viable alternative to traditional scripting languages.

Paul Ferrill, based in Chelsea, Alabama, has been writing about computers and software for almost 20 years. He has programmed in more languages than he cares to count, but now leans toward Visual Basic and C#.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.