Code Search for Developers
 
 
  

Tutorial.htm from p4shelf at Krugle


Show Tutorial.htm syntax highlighted

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252" />
<title>IronPython Tutorial</title>
<link rel="stylesheet" type="text/css" href="../Doc/IronPython.css">
</head>

<body lang="EN-US">

<div class="Section1">
	<p class="Title1">IronPython Tutorial</p>
	<p class="Title2">A tour of Python on .NET</p>
	<p class="Title2">&nbsp;</p>
	<hr />
	<p class="CopyrightText">Information in this document is subject to change without 
	notice. The example companies, organizations, products, people, and events depicted 
	herein are fictitious. No association with any real company, organization, product, 
	person or event is intended or should be inferred. Complying with all applicable 
	copyright laws is the responsibility of the user. Without limiting the rights 
	under copyright, no part of this document may be reproduced, stored in or introduced 
	into a retrieval system, or transmitted in any form or by any means (electronic, 
	mechanical, photocopying, recording, or otherwise), or for any purpose, without 
	the express written permission of Microsoft Corporation.</p>
	<p class="CopyrightText">&nbsp;</p>
	<p class="CopyrightText">Microsoft may have patents, patent applications, trademarked, 
	copyrights, or other intellectual property rights covering subject matter in 
	this document. Except as expressly provided in any written license agreement 
	from Microsoft, the furnishing of this document does not give you any license 
	to these patents, trademarks, copyrights, or other intellectual property.</p>
	<p class="CopyrightText">&nbsp;</p>
	<p class="CopyrightText">© Microsoft Corporation. All rights reserved.</p>
	<p class="CopyrightText">&nbsp;</p>
	<p class="CopyrightText">Microsoft, MS-DOS, MS, Windows, Windows NT, MSDN, Active 
	Directory, BizTalk, SQL Server, SharePoint, Outlook, PowerPoint, FrontPage, 
	Visual Basic, Visual C++, Visual J++, Visual InterDev, Visual SourceSafe, Visual 
	C#, Visual J#,&nbsp; and Visual Studio are either registered trademarks or trademarks 
	of Microsoft Corporation in the U.S.A. and/or other countries.</p>
	<p class="CopyrightText">&nbsp;</p>
	<p class="CopyrightText">Other product and company names herein may be the trademarks of their respective owners.</p>
	<hr />
	<p class="ContentsHeading">Contents</p>
	<p class="Toc1"><a href="#Intro">Introduction</a></p>
	<p class="Toc1">Tutorial 1: <a href="#T1">Basic IronPython</a></p>
	<p class="Toc2">Exercise 1: <a href="#T1.1">The IronPython interactive console</a></p>
	<p class="Toc3">Task 1: <a href="#T1.1.1">IronPython console</a></p>
	<p class="Toc3">Task 2: <a href="#T1.1.2">Built-in modules and interactive exploration</a></p>
	<p class="Toc3">Task 3: <a href="#T1.1.3">External Python modules</a></p>
	<p class="Toc2">Exercise 2: <a href="#T1.2">Using the standard .NET libraries from IronPython</a></p>
	<p class="Toc3">Task 1: <a href="#T1.2.1">Basic .NET library use</a></p>
	<p class="Toc3">Task 2: <a href="#T1.2.2">Working with .NET classes</a></p>
	<p class="Toc3">Task 3: <a href="#T1.2.3">Generics</a></p>
	<p class="Toc2">Exercise 3: <a href="#T1.3">Loading .NET libraries</a></p>
	<p class="Toc3">Task 1: <a href="#T1.3.1">Using System.Xml - AddReference</a></p>
	<p class="Toc3">Task 2: <a href="#T1.3.2">Mapack - Loading the .NET libraries - AddReferenceToFile</a></p>
	<p class="Toc2">Exercise 4: <a href="#T1.4">Obtaining and Using Python Standard Library</a></p>
	<p class="Toc3">Task 1: <a href="#T1.4.1">Configuring IronPython to use the Python standard library</a></p>
	<p class="Toc1">Tutorial 2: <a href="#T2">Advanced IronPython</a></p>
	<p class="Toc2">Exercise 1: <a href="#T2.1">Events and Delegates</a></p>
	<p class="Toc3">Task 1: <a href="#T2.1.1">File System Watcher</a></p>
	<p class="Toc3">Task 2: <a href="#T2.1.2">Improving the event handler</a></p>
	<p class="Toc3">Task 3: <a href="#T2.1.3">Defining events in Python</a></p>
	<p class="Toc2">Exercise 2: <a href="#T2.2">Windows Forms</a></p>
	<p class="Toc3">Task 1: <a href="#T2.2.1">Simple Windows Forms application</a></p>
	<p class="Toc2">Exercise 3: <a href="#T2.3">Windows Presentation Foundation (Avalon)</a></p>
	<p class="Toc3">Task 1: <a href="#T2.3.1">Simple Avalon Application</a></p>
	<p class="Toc3">Task 2: <a href="#T2.3.2">Avalon calculator</a></p>
	<p class="Toc1">Tutorial 3: <a href="#T3">IronPython and COM interoperability</a></p>
	<p class="Toc2">Exercise 1: <a href="#T3.1">Merlin the Wizard</a></p>
	<p class="Toc3">Task 1: <a href="#T3.1.1">Creating COM interop assemblies</a></p>
	<p class="Toc3">Task 2: <a href="#T3.1.2">Using COM objects from IronPython</a></p>
	<p class="Toc2">Exercise 2: <a href="#T3.2">Use Word for Spell Checking</a></p>
	<p class="Toc3">Task 1: <a href="#T3.2.1">Accessing Word and Checking 
	Spelling</a></p>
	<p class="Toc3">Task 2: <a href="#T3.2.2">Use Windows Form Dialog to Correct 
	Spelling</a></p>
	<p class="Toc1">Tutorial 4: <a href="#T4">Debugging IronPython program</a></p>
	<p class="Toc2">Exercise 1: <a href="#T4.1">Debugging IronPython programs</a></p>
	<p class="Toc3">Task 1: <a href="#T4.1.1">Debugging IronPython programs using Microsoft CLR Debugger</a></p>
	<p class="Toc1">Tutorial 5: <a href="#T5">Extending IronPython</a></p>
	<p class="Toc2">Exercise 1: <a href="#T5.1">Extending using C#</a></p>
	<p class="Toc3">Task 1: <a href="#T5.1.1">Implementing simple class - constructor and ToString</a></p>
	<p class="Toc3">Task 2: <a href="#T5.1.2">Making the object enumerable</a></p>
	<p class="Toc3">Task 3: <a href="#T5.1.3">Adding custom operator</a></p>
	<p class="Toc3">Task 4: <a href="#T5.1.4">Adding delegate</a></p>
	<p class="Toc2">Exercise 2: <a href="#T5.2">Extending using Visual Basic.NET</a></p>
	<p class="Toc3">Task 1: <a href="#T5.2.1">Implementing simple class - constructor and ToString</a></p>
	<p class="Toc3">Task 2: <a href="#T5.2.2">Making the object enumerable</a></p>
	<p class="Toc3">Task 3: <a href="#T5.2.3">Adding custom operator</a></p>
	<p class="Toc3">Task 4: <a href="#T5.2.4">Adding delegate</a></p>
	<p class="Toc1">Tutorial 6: <a href="#T6">Embedding IronPython</a></p>
	<p class="Toc2">Exercise 1: <a href="#T6.1">IronPython as an expression evaluator</a></p>
	<p class="Toc3">Task 1: <a href="#T6.1.1">IronPython as an expression evaluator</a></p>
	<p class="Toc2">Exercise 2: <a href="#T6.2">IronPython as application&#39;s scripting engine</a></p>
	<p class="Toc3">Task 1: <a href="#T6.2.1">Embedding IronPython in an application</a></p>
	<p class="Toc3">Task 2: <a href="#T6.2.2">Handling IronPython exceptions gracefully</a></p>
    <p class="Toc3">Task 3: <a href="#T6.2.3">Redirecting the engine's output streams</a></p>
    <p class="Toc1">Tutorial 7: <a href="#T7">Using Visual Studio to Edit .py 
	Files and Debug Them</a></p>
	<p class="Toc2">Exercise 1: <a href="#T7.1">Setting up Visual Studio for 
	IronPython Debugging</a></p>
	<p class="Toc3">Task 1: <a href="#T7.1.1">Setting up Visual Studio for 
	IronPython Debugging</a></p>
</div>
<div class="Section2">
	<h1><a name="Intro">Introduction</a></h1>
	<p class="Normal">IronPython is the .NET implementation of the Python programming language
        (<a href="http://www.python.org">www.python.org</a>).&nbsp; It's a dynamically typed language with support
        for many programming paradigms such as object-oriented programming, and also allows 
	you to seamlessly use .NET code.</p>
	<p class="Normal">&nbsp;</p>
	<p class="Normal">The goal of this tutorial is to quickly 
	familiarize you with the IronPython console, and to show you how to make use of the extensive .NET libraries available.&nbsp; 
	This tutorial also shows you how to get started in more specialized areas 
	such as interoperating with COM, extending IronPython with C#, and embedding 
	IronPython.&nbsp; This tutorial is NOT meant to be an introduction to Python 
	itself, and if you're looking for that, we recommend you start with the 
	tutorial at <a href="http://docs.python.org/tut/tut.html">www.python.org</a> 
	or the often recommend book <i>Learning Python</i> by Mark Lutz and David 
	Ascher.<br><br>
        Some of the exercises in this tutorial require prerequisites.&nbsp; The prerequisites to successfully complete the whole tutorial are:</p>
	<ul type="disc">
		<li class="Normal">Microsoft .NET Framework Version 2.0 Redistributable 
		Package (x86)
		<ul type="circle">
			<li class="Normal">Required to run IronPython</li>
			<li class="Normal">Download from
			<a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5&DisplayLang=en">
			here</a>.</li>
		</ul>
		</li>
	</ul>
	<ul>
		<li class="Normal">.NET Framework 2.0 Software Development Kit (SDK) (x86)
		<ul type="circle">
			<li class="Normal">Required for the COM interoperability, extending 
			and embedding tutorials.</li>
			<li class="Normal">Download from
			<a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=fe6f2099-b7b4-4f47-a244-c96d69c35dec&DisplayLang=en">
			here</a>.</li>
		</ul>
		</li>
	</ul>
	<ul>
		<li class="Normal"><a name="Avalon">Microsoft WinFX Runtime Components</a> 
		(Avalon)<ul type="circle">
			<li class="Normal">Required for &quot;Advanced IronPython&quot; and &quot;Embedding 
			IronPython&quot; tutorials.</li>
			<li class="Normal">Download from
			<a href="http://msdn.microsoft.com/windowsvista/getthebeta/default.aspx">
			here</a>.</li>
		</ul>
		</li>
	</ul>
	<ul>
		<li class="Normal"><a name="Mapack">Mapack</a> (example assembly found 
		on the internet)<ul>
			<li>Required for the &quot;Basic IronPython&quot; tutorial, exercise &quot;Loading .NET 
			Libraries&quot;.</li>
			<li>Download Mapack from
			<a href="http://www.aisto.com/roeder/dotnet/">here</a> (direct link 
			to the Mapack.zip download is
			<a href="http://www.aisto.com/roeder/dotnet/Download.aspx?File=Mapack">
			here</a>).</li>
			<li>Extract Mapack.dll from the zip file directly into the Tutorial 
			directory.</li>
		</ul>
		</li>
	</ul>
	<p class="Normal">&nbsp;</p>
	<p class="Body">Visual Studio 2005 can be used in place of the Microsoft .NET 
	Framework Version 2.0 and the .NET Framework 2.0 Software Development Kit (SDK). 
	Since Visual Studio 2005 installs both the .NET Framework 2.0 and the .NET Framework 
	SDK, there is no need to install those explicitly if you have Visual Studio 
	2005 available.</p>
	<p class="Body">This tutorial assumes that the IronPython distribution was uncompressed 
	into the directory C:\IronPython.&nbsp; Please note that your individual setup may 
	vary.</p>
	<p class="Body">This tutorial also assumes that you will launch the 
	IronPython console (c:\ironpython\ipy.exe) from the tutorial directory.&nbsp; 
	When the tutorials direct you to start the IronPython console from the tutorial directory, 
	you should change to the tutorial directory (&gt;cd c:\ironpython\tutorial) 
	and launch the console with the tutorial as your working directory (&gt;..\ipy.exe).</p>
	<p class="Body">&nbsp;</p>
	<h1><a name="T1">Tutorial 1: Basic IronPython</a></h1>
	<p class="Body">The emphasis of this tutorial is on the basic interaction with 
	the IronPython interpreter and using the interactive environment to explore 
	the .NET libraries.</p>
	<p class="Body">Estimated time to complete this tutorial: <b>30 minutes</b></p>
	<p class="Body">The objective of this tutorial is to launch the IronPython interpreter, 
	explore the environment of the interactive console and use IronPython to interact 
	with .NET libraries.</p>
	<p class="Body">The exercises in this tutorial are:</p>
	<ul>
		<li>
		<p class="ExerciseList"><a href="#T1.1">The IronPython interactive console</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T1.2">Using the standard .NET libraries 
		from IronPython</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T1.3">Loading additional .NET libraries</a></p>
		</li>
	</ul>
	<h2><a name="T1.1">Exercise 1: The IronPython interactive console</a></h2>
	<p class="Body">In this exercise, you will start the IronPython interactive 
	interpreter and perform simple tasks to become acquainted with the IronPython 
	environment.</p>
	<p class="Normal">If you are familiar with using the Python interactive console, 
	the import statement and exploring the Python interactive environment using 
	dir() function and __doc__ attribute, you can <a href="#T1.2">skip this exercise</a>.</p>
	<h3><a name="T1.1.1">Task 1: IronPython console</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		by changing to the tutorial directory (&gt;cd c:\ironpython\tutorial) and 
		launching the console c:\ironpython\ipy.exe executable (&gt;..\ipy.exe).&nbsp; 
		This is how you should always launch the console for the tutorials, but 
		from now on, we'll just direct you to &quot;start the IronPython console from 
		the tutorial directory&quot;.</p>
		</li>
	</ol>
	<p class="Code-Background">IronPython 1.0 on .NET 2.0.50727.42<br />
	Copyright (c) Microsoft Corporation. All rights reserved.</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; _</p>
	<ol start="2">
		<li>
		<p class="Step">Execute simple statements listed below.&nbsp; After each statement IronPython prints the result, if any, and awaits more input.&nbsp; 
		(The input line starting with &quot;for&quot; requires an extra return or enter 
		key press because the interpreter prompts for more statements in the 
		'for' loop.)</p>
		</li>
	</ol>
	<p class="TypedExample">2+2</p>
	<p class="TypedExample">print &quot;Hello World!&quot;</p>
	<p class="TypedExample">for i in range(3): print i</p>
	<p class="TypedExample">x = 10</p>
	<p class="TypedExample">print x</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">After this step, the console window will contain 
	the following text:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; 2+2</p>
	<p class="Code-Background">4</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; print &quot;Hello World!&quot;</p>
	<p class="Code-Background">Hello World!</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; for i in range(3): print i</p>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">0<br />
	1<br />
	2</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; x = 10<br />
	&gt;&gt;&gt; print x</p>
	<p class="Code-Background">10</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; </p>
	<ol start="3">
		<li>
		<p class="Step">IronPython console supports multi-line statements, often 
		used by function definitions.&nbsp; IronPython prompts for additional lines of 
		multi-line statements using:</p>
		</li>
	</ol>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">&nbsp;</p>
	<p class="Stepnonumbering">Unlike C# or Java, where blocks of code are grouped by curly brackets "{...}",
        blocks of code in Python are grouped based on their level of indentation.&nbsp; Every new block of code
        must be indented one more level than the previous block of code.&nbsp; Blocks of code are used for
        function and class definitions as well as 'if' statements and loops. </p>
	<p class="Stepnonumbering">Define the &quot;add&quot; function (note, you 
	need to enter spaces before the 'return' statement):</p>
	<p class="TypedExample">def add(a, b):<br>
&nbsp;&nbsp;&nbsp; return a + b</p>
	<p class="Stepnonumbering">To complete the function definition, press Enter 
	once more at this point</p>
	<p class="TypedExample">add(3, 2)</p>
	<p class="TypedExample">add(&quot;Iron&quot;, &quot;Python&quot;)</p>
	<p class="Stepnonumbering">After this step, the console contents will be:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; def add(a, b):<br />
	...&nbsp;&nbsp;&nbsp;&nbsp; return a + b<br />
	...<br />
	&gt;&gt;&gt; add(3, 2)</p>
	<p class="Code-Background">5</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; add(&quot;Iron&quot;, &quot;Python&quot;)</p>
	<p class="Code-Background">&#39;IronPython&#39;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; </p>
	<p class="Step">&nbsp;</p>
	<ol start="4">
		<li>
		<p class="Step">To exit the IronPython interactive console, type Ctrl+Z and Enter (alternatively, 
		press F6 followed by Enter).</p>
		</li>
	</ol>
	<p class="TypedExample">^Z</p>
	<h3><a name="T1.1.2">Task 2: Built-in modules and interactive exploration</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Using the built-in dir() function, list the contents of 
		the IronPython environment:</p>
		</li>
	</ol>
	<p class="TypedExample">dir()</p>
	<p class="Stepnonumbering">The output in the console window will be:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; dir()</p>
	<p class="Code-Background">['__builtins__', '__doc__', '__name__']<br />
	&gt;&gt;&gt; </p>
	<ol start="3">
		<li>
		<p class="Step">IronPython comes with several built-in modules, the most 
		frequently used one being &quot;sys&quot;.&nbsp; Import &quot;sys&quot; module using the &quot;import&quot; 
		statement:</p>
		</li>
	</ol>
	<p class="TypedExample">import sys</p>
	<ol start="4">
		<li>
		<p class="Step">The Python import statement is similar to the &quot;using&quot; statement 
		of C# or &quot;Imports&quot; statement of Visual Basic. The important difference is 
		that the C# and VB statements bring the names from the imported namespace 
		into to the global namespace to be accessed directly. Python’s import doesn’t 
		do that. To access the names or attributes in an imported module, prefix 
		the 
		names with the module's name:</p>
		</li>
	</ol>
	<p class="TypedExample">sys.version</p>
	<ol start="5">
		<li>
		<p class="Step">Use the dir() function to explore the environment:</p>
		</li>
	</ol>
	<p class="TypedExample">dir()</p>
	<p class="Stepnonumbering" style="margin-left: 0.5in">The environment (global namespace) has changed, now 
	it contains the &quot;sys&quot; module:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; dir()</p>
	<p class="Code-Background">['__builtins__', '__doc__', 
	'__name__', 'sys']<br />
	&gt;&gt;&gt;</p>
	<ol start="6">
		<li>
		<p class="Step">Use the dir() function to explore the contents of the &quot;sys&quot; 
		module:</p>
		</li>
	</ol>
	<p class="TypedExample">dir(sys)</p>
	<ol start="7">
		<li>
		<p class="Step">Print the values of some of the &quot;sys&quot; module attributes:</p>
		</li>
	</ol>
	<p class="TypedExample">sys.path</p>
	<p class="TypedExample">sys.executable</p>
	<h3><a name="T1.1.3">Task 3: External Python modules</a></h3>
	<p class="Normal">This task uses the module &quot;first.py&quot; located in the Tutorial 
	folder.</p>
	<ol>
		<li>
		<p class="Step">Import the &quot;first.py&quot; module located in the Tutorial 
		(because you launched ipy.exe from the Tutorial directory, &quot;first&quot; will 
		be found on your sys.path):</p>
		</li>
	</ol>
	<p class="TypedExample">import first</p>
	<ol start="2">
		<li>
		<p class="Step">Explore the module &quot;first&quot; using dir() function:</p>
		</li>
	</ol>
	<p class="TypedExample">dir(first)</p>
	<p class="TypedExample">&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; dir(first)</p>
	<p class="Code-Background">[&#39;__builtins__&#39;, &#39;__doc__&#39;, &#39;__file__&#39;, &#39;__name__&#39;, 
	&#39;add&#39;, &#39;factorial&#39;, &#39;hi&#39;]</p>
	<ol start="3">
		<li>
		<p class="Step">Print the documentation for the &quot;add&quot; and &quot;factorial&quot; functions, 
		using __doc__ attribute:</p>
		</li>
	</ol>
	<p class="TypedExample">first.add.__doc__</p>
	<p class="TypedExample">first.factorial.__doc__</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">The __doc__ attribute will be later used also for 
	exploring .NET methods and their parameter types.</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; first.add.__doc__</p>
	<p class="Code-Background">&#39;add(a, b) -&gt; returns a + b&#39;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; first.factorial.__doc__</p>
	<p class="Code-Background">&#39;factorial(n) -&gt; returns factorial of n&#39;<br />
	&gt;&gt;&gt; </p>
	<ol start="4">
		<li>
		<p class="Step">Call the methods in the &quot;first&quot; module and print the contents 
		of the &quot;hi&quot; attribute</p>
		</li>
	</ol>
	<p class="TypedExample">first.add(1,2)</p>
	<p class="TypedExample">first.factorial(5)</p>
	<p class="TypedExample">first.hi</p>
	<p class="Stepnonumbering">The expected output is:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; first.add(1,2)</p>
	<p class="Code-Background">3</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; first.factorial(5)</p>
	<p class="Code-Background">120</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; first.hi</p>
	<p class="Code-Background">&#39;Hello from IronPython!&#39;</p>
	<ol start="5">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6 followed 
		by Enter)</p>
		</li>
	</ol>
	<h2><a name="T1.2">Exercise 2: Using the standard .NET libraries from IronPython</a></h2>
	<p class="Body">The power of IronPython lies within the ability to seamlessly 
	access the wealth of .NET libraries. This exercise will demonstrate how the 
	.NET libraries can be used from IronPython.</p>
	<p class="Body">In this exercise, you will use the standard .NET libraries from 
	IronPython. </p>
	<h3><a name="T1.2.1">Task 1: Basic .NET library use</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Using the &quot;import&quot; statement, import the .NET System namespace:</p>
		</li>
	</ol>
	<p class="TypedExample">import System</p>
	<ol start="3">
		<li>
		<p class="Step">Explore the System.Environment class and access some of 
		its properties:</p>
		</li>
	</ol>
	<p class="TypedExample">dir(System.Environment)</p>
	<p class="TypedExample">System.Environment.OSVersion</p>
	<p class="TypedExample">System.Environment.CommandLine</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">The expected output of these commands is as follows 
	(with some ellipsis for convenience):</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; dir(System.Environment)</p>
	<p class="Code-Background">[&#39;CommandLine&#39;, ..., &#39;OSVersion&#39;, ... ]</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; System.Environment.OSVersion</p>
	<p class="Code-Background">Microsoft Windows NT 5.2.3790 Service Pack 1</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; System.Environment.CommandLine</p>
	<p class="Code-Background">&#39;C:\\IronPython\\ipy.exe&#39;<br />
	&gt;&gt;&gt; </p>
	<ol start="4">
		<li>
		<p class="Step">The import statement can be used also to import contents of 
		a class or module into the global namespace.&nbsp; Use the &quot;from ... import ...&quot; 
		flavor of the import statement to do that and explore the contents of the 
		global namespace using dir().</p>
		</li>
	</ol>
	<p class="TypedExample">from System.Math import *</p>
	<p class="TypedExample">dir()</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">Now you can call Math methods without having to specify 
	the namespace and class name prefix:</p>
	<p class="TypedExample">Sin(PI/2)</p>
	<p class="Stepnonumbering">The expected output is:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; from System.Math import *</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; dir()</p>
	<p class="Code-Background">[&#39;Abs&#39;, &#39;Acos&#39;, &#39;Asin&#39;, &#39;Atan&#39;, &#39;Atan2&#39;, &#39;BigMul&#39;, 
	&#39;Ceiling&#39;, &#39;Cos&#39;, &#39;Cosh&#39;, &#39;DivRem&#39;, &#39;E&#39;, &#39;Equals&#39;, &#39;Exp&#39;, &#39;Floor&#39;, &#39;GetHashCode&#39;, 
	&#39;GetType&#39;, &#39;IEEERemainder&#39;, &#39;Log&#39;, &#39;Log10&#39;, &#39;Max&#39;, &#39;Min&#39;, &#39;PI&#39;, &#39;Pow&#39;, &#39;Round&#39;, 
	&#39;Sign&#39;, &#39;Sin&#39;, &#39;Sinh&#39;, &#39;Sqrt&#39;, &#39;System&#39;, &#39;Tan&#39;, &#39;Tanh&#39;, &#39;ToString&#39;, &#39;Truncate&#39;, &#39;__builtins__&#39;, &#39;__doc__&#39;, &#39;__name__&#39;]</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; Sin(PI/2)</p>
	<p class="Code-Background">1.0</p>
	<h3><a name="T1.2.2">Task 2: Working with .NET classes</a></h3>
	<ol>
		<li>
		<p class="Step">Import the contents of the &quot;System.Collections&quot; namespace 
		into the global namespace:</p>
		</li>
	</ol>
	<p class="TypedExample">from System.Collections import *</p>
	<ol start="2">
		<li>
		<p class="Step">Create instance of the Hashtable class and explore the instance 
		using dir():</p>
		</li>
	</ol>
	<p class="TypedExample">h = Hashtable()</p>
	<p class="TypedExample">dir(h)</p>
	<ol start="3">
		<li>
		<p class="Step">Insert&nbsp;a few elements into the hash table:</p>
		</li>
	</ol>
	<p class="TypedExample">h[&quot;a&quot;] = &quot;IronPython&quot;</p>
	<p class="TypedExample">h[&quot;b&quot;] = &quot;Tutorial&quot;</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">IronPython supports the C# - style syntax for accessing 
	the hash table elements.&nbsp; The same syntax applies to any index-able object (Arrays, 
	Array lists etc):</p>
	<p class="TypedExample">h[&quot;a&quot;]</p>
	<p class="Stepnonumbering" style="page-break-after:avoid">&nbsp;</p>
	<p class="Stepnonumbering" style="page-break-after:avoid">The output of this 
	step will be:</p>
	<p class="Code-Background" style="page-break-after:avoid">&gt;&gt;&gt; h[&quot;a&quot;] = &quot;IronPython&quot;<br />
	&gt;&gt;&gt; h[&quot;b&quot;] = &quot;Tutorial&quot;</p>
	<p class="Code-Highlighted" style="page-break-after:avoid">&gt;&gt;&gt; h[&quot;a&quot;]</p>
	<p class="Code-Background">&#39;IronPython&#39;</p>
	<ol start="4">
		<li>
		<p class="Step">Enumerate the contents of the hash table using the &quot;for 
		... in ...&quot; statement. The hash table elements are instances of &quot;DictionaryEntry&quot; 
		class. Print the &quot;Key&quot; and &quot;Value&quot; properties of each entry:</p>
		</li>
	</ol>
	<p class="TypedExample">for e in h: print e.Key, &quot;:&quot;, e.Value</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">The expected output in the console is (the input 
	line starting with &quot;for&quot; requires an extra return or enter key press because 
	the interpreter prompts for more statements in the 'for' loop.):</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; for e in h: print e.Key, &quot;:&quot;, e.Value</p>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">a : IronPython<br />
	b : Tutorial</p>
	<ol start="5">
		<li>
		<p class="Step">You can initialize the collection classes by passing in 
		the Python built-in list or tuple data types as arguments.&nbsp; You can 
		create a Python list by specifying the list of elements in square brackets: [1,2,3].&nbsp; 
		You create tuples by specifying elements in the parentheses: (1,2,3).</p>
		</li>
	</ol>
	<p class="TypedExample">l = ArrayList([1,2,3])</p>
	<p class="TypedExample">for i in l: print i</p>
	<p class="TypedExample">s = Stack((1,2,3))</p>
	<p class="TypedExample">while s.Count: s.Pop()</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">The expected output is:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; l = ArrayList([1,2,3])<br />
	&gt;&gt;&gt; for i in l: print i</p>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">1<br />
	2<br />
	3</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; s = Stack((1,2,3))<br />
	&gt;&gt;&gt; while s.Count: s.Pop()</p>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">3<br />
	2<br />
	1</p>
	<h3><a name="T1.2.3">Task 3: Generics</a></h3>
	<ol>
		<li>
		<p class="Step">Import the Generic collections from the System.Collections.Generic 
		namespace</p>
		</li>
	</ol>
	<p class="TypedExample">from System.Collections.Generic import *</p>
	<ol start="2">
		<li>
		<p class="Step">To instantiate a generic class, the generic type arguments 
		must be specified.&nbsp; IronPython uses the following syntax to specify the type 
		arguments: generic_type[type_argument, ...].&nbsp; Create instance of generic 
		list of string:</p>
		</li>
	</ol>
	<p class="TypedExample">l = List[str]()</p>
	<ol start="3">
		<li>
		<p class="Step">Add string values into the list. Since we created list of 
		string, adding string is possible:</p>
		</li>
	</ol>
	<p class="TypedExample">l.Add(&quot;Hello&quot;)</p>
	<p class="TypedExample">l.Add(&quot;Hi&quot;)</p>
	<ol start="4">
		<li>
		<p class="Step">Try adding objects of types other than string:</p>
		</li>
	</ol>
	<p class="TypedExample">l.Add(3)</p>
	<p class="TypedExample">l.Add(2.5)</p>
	<p class="TypedExample">l.Add([1,2,3])</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">Obviously, adding non-strings will fail with type 
	error:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; l.Add(3)</p>
	<p class="Code-Background">Traceback (most recent call last):<br />
&nbsp;&nbsp; ...</p>
	<p class="Code-Highlighted">ValueError: The value &quot;3&quot; is not of type &quot;System.String&quot; 
	and cannot be used in this generic collection.</p>
	<p class="Code-Highlighted">Parameter name: value</p>
	<ol start="5">
		<li>
		<p class="Step">Enumerate the generic collection:</p>
		</li>
	</ol>
	<p class="TypedExample">for i in l: print i</p>
	<p class="Stepnonumbering">The output will be:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; for i in l: print i</p>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">Hello<br />
	Hi</p>
	<ol start="6">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6 followed 
		by Enter)</p>
		</li>
	</ol>
	<h2><a name="T1.3">Exercise 3: Loading .NET libraries</a></h2>
	<p class="Body">IronPython can directly import only some of the .NET libraries 
	- the most commonly used ones.&nbsp; To use additional .NET libraries, they must be 
	explicitly referenced.&nbsp; IronPython maintains a list of all referenced 
	assemblies (see clr.References in Task 1).&nbsp; To add a reference to a .NET assembly, use the functions 
	available in the built-in &quot;clr&quot; module:</p>
	<ul>
		<li>
		<p class="Body"><b>clr.AddReference</b> adds a reference to the .NET assembly 
		either by passing the .NET assembly object directly, or specifying the file 
		name or the assembly name (full or partial). This function is provided primarily 
		for the interactive exploration. We recommend using the following functions 
		in the code modules, since they provide more control over which assembly 
		is being loaded.</p>
		</li>
		<li>
		<p class="Body"><b>clr.AddReferenceToFile</b> adds reference to the assembly 
		specified by its file name. This function will load the assembly from the 
		file regardless of the assembly version. As a result, it doesn&#39;t guarantee 
		that the correct assembly version is being loaded. To guarantee that correct 
		assembly version is being loaded, use clr.AddReferenceByName. Moreover, 
		AddReferenceToFile requires that the assembly be located in a 
		directory listed in sys.path.</p>
		</li>
		<li>
		<p class="Body"><b>clr.AddReferenceToFileAndPath</b> provides similar 
		functionality to AddReferenceToFile. The difference is that it accepts 
		absolute path and before loading the assembly, AddReferenceToFileAndPath 
		adds the file path into sys.path.</p>
		</li>
		<li>
		<p class="Body"><b>clr.AddReferenceByName</b> adds reference to the assembly 
		specified by its full assembly name, for example: &#39;System.Xml, Version=2.0.0.0, 
		Culture=neutral, PublicKeyToken=b77a5c561934e089&#39;.</p>
		</li>
		<li>
		<p class="Body"><b>clr.AddReferenceByPartialName</b> adds reference to the 
		assembly by specifying a partial assembly name. This function cannot guarantee 
		that the correct version of the assembly is being loaded. Use clr.AddReferenceByName 
		to add reference to specific version of the assembly.</p>
		</li>
	</ul>
	<h3><a name="T1.3.1">Task 1: Using System.Xml - AddReference</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">To import System.Xml, the reference to the assembly containing 
		the Xml components must be first added to IronPython.&nbsp; Add reference to System.Xml 
		using the following code&nbsp; (you can enter &quot;clr.References&quot; before 
		and after the call to clr.AddReference to see it change if you want):</p>
		</li>
	</ol>
	<p class="TypedExample">import clr</p>
	<p class="TypedExample">clr.AddReference(&quot;System.Xml&quot;)</p>
	<p class="TypedExample">from System.Xml import *</p>
	<p class="TypedExample">dir()</p>
	<ol start="3">
		<li>
		<p class="Step">Note that the clr.AddReference function accepts either System.Reflection.Assembly object or string 
		as a parameter. The string parameter can 
		be a full assembly name, a partial assembly name, or a file name. For more control 
		over the assembly references, use the appropriate functions described 
		above. </p>
		<p class="Step">For example, consider the following alternatives for the statement clr.AddReference(&quot;System.Xml&quot;) above:</p>
		</li>
	</ol>
	<p class="TypedExample">clr.AddReferenceByName(&#39;System.Xml, Version=2.0.0.0, 
	Culture=neutral, PublicKeyToken=b77a5c561934e089&#39;)</p>
	<p class="TypedExample">clr.AddReferenceByPartialName(&quot;System.Xml&quot;)</p>
	<ol start="4">
		<li>
		<p class="Step">Load the XML file &quot;Releases.xml&quot; into the XmlDocument. The 
		xml file contains the release dates of all versions of IronPython between 
		0.7 and 0.9.4. For direct viewing, the Releases.xml file is located in &quot;Releases.xml&quot;</p>
		</li>
	</ol>
	<p class="TypedExample">d = XmlDocument()</p>
	<p class="TypedExample">d.Load(&quot;Releases.xml&quot;)</p>
	<ol start="5">
		<li>
		<p class="Step">We can now query the document. Query for all released versions 
		of IronPython using the query below:</p>
		</li>
	</ol>
	<p class="TypedExample">n = d.SelectNodes(&quot;//@Version&quot;)</p>
	<p class="TypedExample">for e in n: print e.Value</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">The output in the console window will be:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; n = d.SelectNodes(&quot;//@Version&quot;)<br />
	&gt;&gt;&gt; for e in n: print e.Value</p>
	<p class="Code-Highlighted">...</p>
	<p class="Code-Background">0.7<br />
	0.7.1<br />
	0.7.2<br />
	0.7.3<br />
	0.7.4<br />
	0.7.5<br />
	0.7.6<br />
	0.9<br />
	0.9.1<br />
	0.9.2<br />
	0.9.3<br />
	0.9.4</p>
	<ol start="6">
		<li>
		<p class="Step">(Optional) Import the &quot;xmlutil.py&quot; module located in the 
		Tutorial directory and use the function provided in the module to walk the 
		contents of the Xml document:</p>
		</li>
	</ol>
	<p class="TypedExample">import xmlutil</p>
	<p class="TypedExample">for e in xmlutil.Walk(d): print e.Name, e.Value</p>
	<p class="Stepnonumbering">The Walk function is a generator (a Python function 
	that contains a &quot;yield&quot; statement). As the Walk function executes, it returns 
	(yields) the XML nodes one by one to the caller who iterates over the generator. 
	The source for the Walk function is:</p>
	<p class="Code-Background">def Walk(xml):</p>
	<p class="Code-Highlighted">&nbsp;&nbsp;&nbsp; yield xml</p>
	<p class="Code-Background">&nbsp;&nbsp;&nbsp; if hasattr(xml, &quot;Attributes&quot;):<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; attrs = xml.Attributes<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if attrs:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for attr in attrs:</p>
	<p class="Code-Highlighted">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	yield attr</p>
	<p class="Code-Background">&nbsp;&nbsp;&nbsp; for child in xml:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for c in Walk(child):</p>
	<p class="Code-Highlighted">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	yield c</p>
	<ol start="7">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6 followed 
		by Enter)</p>
		</li>
	</ol>
	<h3><a name="T1.3.2">Task 2: Mapack - Loading the .NET libraries - AddReferenceToFile</a></h3>
	<p class="Normal">This task requires the Mapack.dll library for linear algebra computations.&nbsp; 
	The library is not part of the IronPython distribution. See <a href="#Mapack">prerequisites</a> 
	for download details.</p>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Use the clr.AddReferenceToFile function to load the Matrix 
		library &quot;Mapack.dll&quot;:</p>
		</li>
	</ol>
	<p class="TypedExample">import clr</p>
	<p class="TypedExample">clr.AddReferenceToFile(&quot;Mapack.dll&quot;)</p>
	<p class="TypedExample">from Mapack import *</p>
	<p class="TypedExample">dir()</p>
	<p class="Stepnonumbering">The expected output will be:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; import clr<br />
	&gt;&gt;&gt; clr.AddReferenceToFile(&quot;Mapack.dll&quot;)<br />
	&gt;&gt;&gt; from Mapack import *<br />
	&gt;&gt;&gt; dir()</p>
	<p class="Code-Background">[&#39;CholeskyDecomposition&#39;, &#39;EigenvalueDecomposition&#39;, 
	&#39;LuDecomposition&#39;, &#39;Matrix&#39;, &#39;QrDecomposition&#39;, &#39;SingularValueDecomposition&#39;, 
	&#39;__builtins__&#39;, &#39;__doc__&#39;, &#39;__name__&#39;, 'clr', &#39;sys&#39;]</p>
	<ol start="3">
		<li>
		<p class="Step">Create instance of Matrix class:</p>
		</li>
	</ol>
	<p class="TypedExample">m = Matrix()</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">Oops, bad arguments for the constructor.&nbsp; In the next 
	step you&#39;ll learn the way to discover the constructors available.</p>
	<p class="Code-Background">&gt;&gt;&gt; m = Matrix()<br />
	Traceback (most recent call last):<br />
&nbsp;&nbsp; at &lt;shell&gt;</p>
	<p class="Code-Highlighted">TypeError: Matrix() takes at least 1 argument (0 
	given)</p>
	<ol start="4">
		<li>
		<p class="Step">Using the __doc__ attribute, find out information about 
		Matrix constructors:</p>
		</li>
	</ol>
	<p class="TypedExample">print Matrix.__new__.__doc__</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; print Matrix.__new__.__doc__</p>
	<p class="Code-Background">__new__(int, int)<br />
	__new__(int, int, float)<br />
	__new__(System.Double[][])</p>
	<ol start="5">
		<li>
		<p class="Step">Create instances of the Matrix class using the correct constructors 
		and set some matrix elements manually.&nbsp; IronPython supports custom indexing 
		on .NET classes. </p>
		</li>
	</ol>
	<p class="TypedExample">m = Matrix(2, 2, 1.2)</p>
	<p class="TypedExample">n = Matrix(2,1)</p>
	<p class="TypedExample">n[0,0] = 4</p>
	<p class="TypedExample">print m</p>
	<p class="TypedExample">print n</p>
	<p class="TypedExample">&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; m = Matrix(2, 2, 1.2)<br />
	&gt;&gt;&gt; n = Matrix(2,1)<br />
	&gt;&gt;&gt; n[0,0] = 4<br />
	&gt;&gt;&gt; print m</p>
	<p class="Code-Background">1.2 0<br />
	0 1.2<br />
&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; print n</p>
	<p class="Code-Background">4<br />
	0</p>
	<ol start="6">
		<li>
		<p class="Step">(Optional) IronPython also supports overloaded operators.&nbsp; Matrix does overload operators +, - (binary and unary) and *.&nbsp; You can see 
		Python representation of the operators (__add__, __mul__, __sub__, ...) 
		using dir():</p>
		</li>
	</ol>
	<p class="TypedExample">dir(m)</p>
	<ol start="7">
		<li>
		<p class="Step">Make simple calculations with the matrices:</p>
		</li>
	</ol>
	<p class="TypedExample">m * n</p>
	<p class="TypedExample">n.Transpose() * m</p>
	<p class="TypedExample">m * 3</p>
	<p class="TypedExample">n + -n</p>
	<p class="Stepnonumbering">The expected output of this step is:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; m * n</p>
	<p class="Code-Background">4.8<br />
	0<br />
&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; n.Transpose() * m</p>
	<p class="Code-Background">4.8 0<br />
&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; m * 3</p>
	<p class="Code-Background">3.6 0<br />
	0 3.6<br />
&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; n + -n</p>
	<p class="Code-Background">0<br />
	0</p>
	<ol start="8">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6 followed 
		by Enter)</p>
		</li>
	</ol>
	<h2><a name="T1.4">Exercise 4: Obtaining and Using Python Standard Library</a></h2>
	<p class="Body">In this exercise, you will obtain Python and point 
	IronPython at the Python standard library.</p>
	<h3><a name="T1.4.1">Task 1: Configuring IronPython to use the Python standard library</a></h3>
	<ol>
		<li>
		<p class="Step">Download the latest Python installer from
		<a href="http://www.python.org/download/">http://www.python.org/download/</a> 
		and install Python.&nbsp; The rest of this exercise will assume you used 
		defaults (and installed to c:\python24, for example).</p>
		</li>
		<li>
		<p class="Step">Create a file named &quot;site.py&quot; and place it into your 
		IronPython &quot;Lib&quot; directory.&nbsp; There might be one there 
		already, so you can just edit it.&nbsp; The &quot;site.py&quot; file is executed every time 
		you run IronPython.&nbsp; To tell IronPython where the Python standard library 
		is, you can add the &quot;lib&quot; directory of Python to IronPython's path.&nbsp; To 
		do so, put the following code into IronPython's &quot;site.py&quot; file (replace 
		c:\python24\lib with your actual path to the Python lib directory):</p>
		</li>
	</ol>
	<p class="Code-Highlighted">import sys</p>
<p class="Code-Highlighted">sys.path.append(<i>r&quot;c:\python24\lib&quot;</i>)</p>
	<ol start="3">
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Now you can use the Python standard library from 
		IronPython, for example to get the current working directory (output 
		uses assumed location of your IronPython installation):</p>
		</li>
	</ol>
	<p class="Code-Highlighted">import os</p>
	<p class="Code-Highlighted">os.getcwd()</p>
	<p class="Code-Background">'C:\\ironpython\\Tutorial'</p>
	<p class="LabSummary">Tutorial Summary</p>
	<p class="Body">In this tutorial you performed the following exercises.</p>
	<ul>
		<li>
		<p class="Body"><a href="#T1.1">The IronPython interactive console</a></p>
		</li>
		<li>
		<p class="Body"><a href="#T1.2">Using the standard .NET libraries from IronPython</a></p>
		</li>
		<li>
		<p class="Body"><a href="#T1.3">Loading .NET libraries</a></p>
		</li>
		<li>
		<p class="Body"><a href="#T1.4">Obtaining and Using Python Standard 
		Library</a></p>
		</li>
	</ul>
	<p class="Body">In this tutorial, you became acquainted with IronPython interactive 
	console, including the dynamic exploratory way of development using the dir() function 
	and __doc__ attribute to explore the environment.&nbsp; You learned to load and import 
	.NET libraries from IronPython (using the import statement), create instances 
	of .NET classes (including generic classes), call methods, enumerate .NET collections, 
	and even use overloaded operators on .NET objects.&nbsp; Finally, you 
	learned how to access standard Python libraries.</p>
	<p class="Body">&nbsp;</p>
	<h1><a name="T2">Tutorial 2: Advanced IronPython</a></h1>
	<p class="Body">The large part of the beauty of IronPython lies within the dynamic-style 
	development -- modifying the live application by adding functioning elements 
	to it.&nbsp; With Windows applications, this often requires delegates and event handling 
	(i.e., adding a button to an existing form and adding functionality to the button 
	to handle the user pressing the button).</p>
	<p class="Body">This tutorial will focus on creating delegates, handling 
	events in IronPython, and creating Windows applications using Windows Forms and 
	the new Windows Presentation Foundation (formerly known as Avalon).</p>
	<p class="Body">Estimated time to complete this tutorial: <b>25 minutes</b></p>
	<p class="Body">The objective of this tutorial is to learn how to create delegates 
	and handle events using IronPython, and to use that knowledge to build working Windows 
	applications using Windows Forms and Windows Presentation Foundation.</p>
	<ul>
		<li>
		<p class="ExerciseList"><a href="#T2.1">Events and Delegates</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T2.2">Windows Forms</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T2.3">Windows Presentation Foundation 
		(Avalon)</a></p>
		</li>
	</ul>
	<h2><a name="T2.1">Exercise 1: Events and Delegates</a></h2>
	<p class="Body">In this exercise, you will create a simple event handler and 
	learn how to explore event handler use. The event handler we&#39;ll use in this 
	exercise is the FileSystemWatcher - a component that raises events on file system 
	changes.</p>
	<h3><a name="T2.1.1">Task 1: File System Watcher</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Import the contents of System.IO into the global namespace:</p>
		</li>
	</ol>
	<p class="TypedExample">from System.IO import *</p>
	<ol start="3">
		<li>
		<p class="Step">Create instance of the FileSystemWatcher class and set Path 
		property to watch over the current directory:</p>
		</li>
	</ol>
	<p class="TypedExample">w = FileSystemWatcher()</p>
	<p class="TypedExample">dir(w)</p>
	<p class="TypedExample">w.Path = &quot;.&quot;</p>
	<ol start="4">
		<li>
		<p class="Step" style="page-break-after:avoid">Create the function to handle 
		the events.&nbsp; Because we don&#39;t know yet what arguments the delegate will have, 
		let&#39;s accept any number of arguments (the *arg notation):</p>
		</li>
	</ol>
	<p class="TypedExample">def handle(*args): print args</p>
	<ol start="5">
		<li>
		<p class="Step">Register the event handler for the &#39;Changed&#39;, &#39;Created&#39;, 
		and &#39;Deleted&#39; events:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Changed += handle</p>
	<p class="TypedExample">w.Created += handle</p>
	<p class="TypedExample">w.Deleted += handle</p>
	<ol start="6">
		<li>
		<p class="Step">Enable the watcher to raise events:</p>
		</li>
	</ol>
	<p class="TypedExample">w.EnableRaisingEvents = True</p>
	<ol start="7">
		<li>
		<p class="Step">Open the Tutorial folder and create a file.&nbsp; An easy way 
		to create the file is to right-click with the mouse and select &quot;New\Text 
		Document&quot;. The file watcher will raise the &quot;Created&quot; event.</p>
		</li>
	</ol>
	<p class="Stepnonumbering">You can then open the file in Notepad, type in any 
	text, and save the file.&nbsp; This raises the &quot;Changed&quot; event.&nbsp; 
	Then finish by deleting 
	the file to see the &quot;Deleted&quot; event get raised.</p>
	<p class="Stepnonumbering">At the end of this step, the output in the command 
	window will be similar to the following:</p>
	<p class="Code-Background">(System.IO.FileSystemWatcher, &lt;System.IO.FileSystemEventArgs 
	object at 0x03CE0BB8&gt;)<br />
	(System.IO.FileSystemWatcher, &lt;System.IO.FileSystemEventArgs object at 0x039B3B4D&gt;)<br />
	(System.IO.FileSystemWatcher, &lt;System.IO.FileSystemEventArgs object at 0x039B3B4D&gt;)<br />
	(System.IO.FileSystemWatcher, &lt;System.IO.FileSystemEventArgs object at 0x014EF022&gt;)</p>
	<ol start="8">
		<li>
		<p class="Step">In the next task, we will create an improved event handler.&nbsp; For now, remove the current event handler from the file watcher events:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Changed -= handle</p>
	<p class="TypedExample">w.Created -= handle</p>
	<p class="TypedExample">w.Deleted -= handle</p>
	<ol start="9">
		<li>
		<p class="Step">(Optional) You can try step 7 again to see that the events, 
		while they are still being raised, are not being handled by the Python &quot;handler&quot; 
		function.</p>
		</li>
	</ol>
	<h3><a name="T2.1.2">Task 2: Improving the event handler</a></h3>
	<ol>
		<li>
		<p class="Step">In the previous task, step 7 we can see that the types of 
		the parameters passed to all three events were the same:</p>
		</li>
	</ol>
	<p class="Stepnonumbering">FileSystemWatcher - the instance of the object that 
	raised the event</p>
	<p class="Stepnonumbering">FileSystemEventArgs - the information about the event 
	raised</p>
	<p class="Stepnonumbering">Use dir() to explore the event arguments class to 
	find what information the event contains:</p>
	<p class="TypedExample">dir(FileSystemEventArgs)</p>
	<p class="Stepnonumbering" style="page-break-after:avoid">The output in the 
	console window will be:</p>
	<p class="Code-Highlighted" style="page-break-after:avoid">&gt;&gt;&gt; dir(FileSystemEventArgs)</p>
	<p class="Code-Background">[&#39;ChangeType&#39;, &#39;Empty&#39;, &#39;Equals&#39;, &#39;Finalize&#39;, &#39;FullPath&#39;, &#39;GetHashCode&#39;, 
	&#39;GetType&#39;, &#39;MakeDynamicType&#39;, &#39;MemberwiseClone&#39;, &#39;Name&#39;, &#39;Reduce&#39;, &#39;ReferenceEquals&#39;, &#39;ToString&#39;, 
	&#39;__class__&#39;, &#39;__doc__&#39;, &#39;__init__&#39;, &#39;__module__&#39;, &#39;__new__&#39;, &#39;__reduce__&#39;, &#39;__reduce_ex__&#39;, &#39;__repr__&#39;]
	<ol start="2">
		<li>
		<p class="Step">Now with more knowledge of the event argument properties, 
		we can create a better event handler that will print ChangeType and FullPath 
		properties of the event argument object:</p>
		</li>
	</ol>
	<p class="TypedExample">def handle(w, a): print a.ChangeType, a.FullPath</p>
	<ol start="3">
		<li>
		<p class="Step">Register the new event handler for the &#39;Changed&#39;, &#39;Created&#39; 
		and &#39;Deleted&#39; events:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Changed += handle</p>
	<p class="TypedExample">w.Created += handle</p>
	<p class="TypedExample">w.Deleted += handle</p>
	<ol start="4">
		<li>
		<p class="Step">Make sure the raising of the events is enabled:</p>
		</li>
	</ol>
	<p class="TypedExample">w.EnableRaisingEvents = True</p>
	<ol start="5">
		<li>
		<p class="Step">Open the Tutorial folder again and create a new file (&quot;Created&quot; 
		event), edit the file in Notepad and save it (&quot;Changed&quot; event) and finish 
		by deleting the file (&quot;Deleted&quot; event).</p>
		</li>
	</ol>
	<p class="Stepnonumbering">At the end of this step, the output in the command 
	window will be similar to the following:</p>
	<p class="Code-Background">Created .\New Text Document.txt<br />
	Changed .\New Text Document.txt<br />
	Changed .\New Text Document.txt<br />
	Deleted .\New Text Document.txt</p>
	<ol start="6">
		<li>
		<p class="Step">Remove the event handler from the file watcher events:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Changed -= handle</p>
	<p class="TypedExample">w.Created -= handle</p>
	<p class="TypedExample">w.Deleted -= handle</p>
	<ol start="7">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6, followed 
		by Enter).</p>
		</li>
	</ol>
	
		<h3><a name="T2.1.3">Task 3: Defining events in Python</a></h3>
	<ol>
		<li>
		<p class="Step">Finally let's look at how we can get event handler syntax from Python code:</p>
		</li>
	</ol>
	<p class="Stepnonumbering">pyevent - a module providing Python event support</p>
	<p class="Stepnonumbering">make_event - a function that returns an event tuple containing both the callable object and the object 
	that allows connecting and unconnecting to the event.</p>
	<br/>
	<p class="Stepnonumbering" style="page-break-after:avoid">To create an event:</p>
	<br/>
	<p class="TypedExample">import pyevent</p>
	<p class="TypedExample">hook,caller = pyevent.make_event()</p>
	<br/>
	<p class="Stepnonumbering">This has returned back to us two objects.  The first object allows a user to hook a 
		function up to the event.  The second object allows the owner of the event to cause the event to be raised.  
		This allows for the separation of these abilities just like .NET.&nbsp; Now let's take this and put it into a class and 
		see how it gets used.</p>
	
	<ol start="2">
		<li>
		<p class="Step">We'll define a class that has an event for</p>
		</li>
	</ol>

	<p class="TypedExample">class MyClass(object):<br>
	&nbsp;&nbsp;&nbsp;&nbsp;OnNewInstance,_NewInstance= pyevent.make_event()<br>
	&nbsp;&nbsp;&nbsp;&nbsp;def __new__(cls):<br>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;res = object.__new__(object)<br>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MyClass._NewInstance(res)<br>
	</p>
	<p class="TypedExample">def NewInst(x): print 'new inst: ', x </p>
	<p class="TypedExample">MyClass.OnNewInstance += NewInst</p>
	<p class="TypedExample">a = MyClass()</p>


	
	<ol start="3">
		<li>
		<p class="Step">Just like with CLR events you can also remove event handlers:</p>
		</li>
	</ol>
	<p class="Code-Highlighted" style="page-break-after:avoid">MyClass.OnNewInstance -= NewInst</p>


	<h2><a name="T2.2">Exercise 2: Windows Forms</a></h2>
	<p class="Body">To interactively develop Windows applications, IronPython must 
	be initialized for that purpose.&nbsp; By default, the Python console executes on one thread only.&nbsp; While this thread awaits text input in the console window, the Windows application 
	being dynamically created from the console is not able to process Windows messages.&nbsp; 
	Therefore, the application does not repaint itself or handle input to the UI.</p>
	<p class="Body">We provide a .py file to initialize IronPython for Windows Forms 
	development, see tasks below.</p>
	<p class="Body">In this exercise, you will create simple Windows Forms applications 
	dynamically from the IronPython interactive console.</p>
	<h3><a name="T2.2.1">Task 1: Simple Windows Forms application</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Initialize Windows Forms by loading the winforms module/script:</p>
		</li>
	</ol>
	<p class="TypedExample">import winforms</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">Python modules get automatically initialized 
	(executed) upon import so the Windows Forms initialization code has executed 
	as part of the import statement.</p>
	<ol start="3">
		<li>
		<p class="Step">Import the contents of the System.Windows.Forms and System.Drawing 
		namespaces into the global namespace: </p>
		</li>
	</ol>
	<p class="TypedExample">from System.Windows.Forms import *</p>
	<p class="TypedExample">from System.Drawing import *</p>
	<ol start="4">
		<li>
		<p class="Step">Create an instance of the Form class and display it:</p>
		</li>
	</ol>
	<p class="TypedExample">f = Form()</p>
	<p class="TypedExample">f.Show()</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">You may need to alt-tab or look for the running 
	application since it may not have popped to the top level on your desktop.</p>
	<ol start="5">
		<li>
		<p class="Step">Now set the form Text property:</p>
		</li>
	</ol>
	<p class="TypedExample">f.Text = &quot;My First Interactive Application&quot;</p>
	<ol start="6">
		<li>
		<p class="Step">To bring the application alive, let&#39;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</p>
		</li>
	</ol>
	<p class="TypedExample">def click(*args): print args</p>
	<p class="TypedExample">f.Click += click</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">Click on the form to receive the event..</p>
	<p class="Stepnonumbering">The output will be similar to:</p>
	<p class="Code-Background">&gt;&gt;&gt; (System.Windows.Forms.Form, Text: My First Interactive 
	Application, &lt;System.Windows.Forms.MouseEventArgs object at 0x02324551&gt;)</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">Now remove the click handler because we're going 
	to further develop it.</p>
	<p class="TypedExample">f.Click -= click</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<ol start="7">
		<li>
		<p class="Step">Use dir() function to explore the MouseEventArgs</p>
		</li>
	</ol>
	<p class="TypedExample">dir(MouseEventArgs)</p>
	<ol start="8">
		<li>
		<p class="Step" style="page-break-after:avoid">Knowing the contents of the 
		MouseEventArgs, create an improved event handler for the Click event:</p>
		</li>
	</ol>
	<p class="TypedExample" style="page-break-after:avoid">def click(f, a):<br>
&nbsp;&nbsp;&nbsp; l = Label(Text = &quot;Hello&quot;)<br>
&nbsp;&nbsp;&nbsp; l.Location = a.Location<br>
&nbsp;&nbsp;&nbsp; f.Controls.Add(l)</p>
	<ol start="9">
		<li>
		<p class="Step" style="page-break-after:avoid">Register the event handler:</p>
		</li>
	</ol>
	<p class="TypedExample">f.Click += click</p>
	<ol start="10">
		<li>
		<p class="Step">Now clicking on the form with the mouse will add &quot;Hello&quot; 
		labels. We can also access the controls we just added via mouse clicks and 
		change them</p>
		</li>
	</ol>
	<p class="TypedExample">for i in f.Controls: i.Font = Font(&quot;Verdana&quot;, 15)</p>
	<p class="TypedExample">for i in f.Controls: i.Text = &quot;Hi&quot;</p>
	<ol start="11">
		<li>
		<p class="Step">After a few moments of clicking, the form will get quite 
		crowded, so we can clear it out:</p>
		</li>
	</ol>
	<p class="TypedExample">f.Controls.Clear()</p>
	<ol start="12">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6 followed 
		by Enter)</p>
		</li>
	</ol>
	<p class="Body">The standalone version of this script is located in wfdemo.py 
	in the Tutorial directory.</p>
	<h2><a name="T2.3">Exercise 3: Windows Presentation Foundation (Avalon)</a></h2>
	<p class="Body">This exercise requires Windows Presentation Foundation installed.&nbsp; Please see <a href="#Avalon">prerequisites</a> for more information.</p>
	<p class="Body">Just like Windows Forms, the Windows Presentation Foundation 
	also requires initialization to support interactive development. The initialization 
	code is available in the &quot;Tutorial\avalon.py&quot; file.</p>
	<p class="Body">In this exercise, you will interactively create simple interactive 
	Windows Presentation Foundation application.</p>
	<h3><a name="T2.3.1">Task 1: Simple Avalon Application</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Initialize Windows Presentation Foundation:</p>
		</li>
	</ol>
	<p class="TypedExample">from avalon import *</p>
	<ol start="3">
		<li>
		<p class="Step">Create Avalon window, display it, and set some of its properties:</p>
		</li>
	</ol>
	<p class="TypedExample">w = Window()</p>
	<p class="TypedExample">w.Show()</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">You may need to alt-tab or look for the running 
	application since it may not have popped to the top level on your desktop.&nbsp; 
	Now, let's do more.</p>
	<p class="TypedExample">&nbsp;</p>
	<p class="TypedExample">w.Title = &quot;My Avalon Application&quot;</p>
	<p class="TypedExample">w.SizeToContent = SizeToContent.WidthAndHeight</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">By setting the window property to &quot;size to 
	content&quot;, the window shrinks.</p>
	<ol start="4">
		<li>
		<p class="Step">Let&#39;s add the content now:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Content = TextBlock()</p>
	<p class="TypedExample">w.Content.Text = &quot;Hello IronPython!&quot;</p>
	<p class="TypedExample">w.Content.FontSize = 50</p>
	<ol start="5">
		<li>
		<p class="Step">Remove the window content:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Content = None</p>
	<h3><a name="T2.3.2">Task 2: Avalon calculator</a></h3>
	<ol>
		<li>
		<p class="Step">If you are continuing from the Task 1, proceed to step 2.&nbsp; Otherwise, please follow the <a href="#T2.3.1">steps 1-3</a> from the Task 
		1. </p>
		</li>
		<li>
		<p class="Step">Windows Presentation Foundation uses the XAML format to describe 
		the graphical layout and basic behaviors of UI. Load the &quot;calc.xaml&quot; and display the resulting content:</p>
		</li>
	</ol>
	<p class="TypedExample">w.Content = LoadXaml(&quot;calc.xaml&quot;)</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">If you flowed into this task from Task 1, then 
	w.SizeToContent may have been reset to manual, but in either case, you may 
	need to manually drag the window's border down and to the right to see the 
	calculator UI.</p>
	<ol start="3">
		<li>
		<p class="Step">Let's walk the calculator&#39;s object model (function 
		Walk is defined in the avalon.py file)</p>
		</li>
	</ol>
	<p class="TypedExample">for n in Walk(w): print n</p>
	<ol start="4">
		<li>
		<p class="Step">Using Python&#39;s list comprehension syntax we can capture 
		all buttons in the calculator:</p>
		</li>
	</ol>
	<p class="TypedExample">[ n for n in Walk(w) if isinstance(n, Button) ]</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">The console printed the list of all buttons.&nbsp; To 
	save the button list object in a variable, use the interpreter's &quot;_&quot; 
	variable, which always holds the last non-None value result printed by the 
	console:</p>
	<p class="TypedExample">buttons = _</p>
	<ol start="5">
		<li>
		<p class="Step">At this point we can make changes to all the buttons, for 
		example, change the colors and fonts:</p>
		</li>
	</ol>
	<p class="TypedExample">for b in buttons: b.FontSize *= 2</p>
	<p class="TypedExample">for b in buttons: b.Foreground = SolidColorBrush(Colors.Blue)</p>
	<ol start="6">
		<li>
		<p class="Step" style="page-break-after:avoid">To bring the calculator alive, 
		we need to provide event handlers for each button.&nbsp; These can be imported 
		from the calculator.py file:</p>
		</li>
	</ol>
	<p class="TypedExample">import calculator</p>
	<ol start="7">
		<li>
		<p class="Step">The calculator module contains the Calculator class that 
		will be responsible for tracking the expression as it is being built by 
		the calculator.&nbsp; To bring the calculator alive (that is, register event 
		handlers for the UI), enter:</p>
		</li>
	</ol>
	<p class="TypedExample">calculator.enliven(w)</p>
	<ol start="8">
		<li>
		<p class="Step">At this point you can click on the calculator buttons and 
		evaluate expressions.</p>
		</li>
		<li>
		<p class="Step">Feel free to explore the calculator.py script file (located 
		in Tutorial directory).&nbsp; The point of interest is the Calculator.__init__ 
		method:</p>
		</li>
	</ol>
	<p class="Code-Highlighted">def __init__(self, controls):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(1)<br />
&nbsp;&nbsp;&nbsp; self.expression = &quot;&quot;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(2)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(3)<br />
&nbsp;&nbsp;&nbsp; for i in controls:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(4)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if isinstance(i, Button):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(5)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if hasattr(self, 
	&quot;on_&quot; + i.Name):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(6)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	i.Click += getattr(self, &quot;on_&quot; + i.Name)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(7)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; elif isinstance(i, TextBox):&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(8)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if i.Name == 
	&quot;Result&quot;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(9)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	self.result = i&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(10)<br />
&nbsp;&nbsp;&nbsp; self.result.Text = self.expression&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
	(11)</p>
	<p class="Stepnonumbering"><br />
	The &quot;controls&quot; argument for the method is the list of buttons and text boxes, very 
	similar to the list of buttons created in steps 3 and 4 of this task.&nbsp; The initialization 
	code enumerates the list (line 4), identifies buttons (line 5), and uses the 
	name of the button (&quot;One&quot;, &quot;Multiply&quot;, &quot; Equals&quot;, ...) to find the 
	calculator attribute 
	(method) with the corresponding name (for example, &quot;on_One&quot;, &quot;on_Multiply&quot;, 
	and &quot;on_Equals&quot;, respectively).&nbsp; If such an attribute (method) is available, 
	we hook the Click event event, using the name to fetch the attribute (line 7).</p>
	<ol start="10">
		<li>
		<p class="Step">The second point of interest is the on_Equals method, especially 
		the highlighted line.&nbsp; Python has a built-in function, &quot;eval&quot;, which evaluates 
		an expression passed as a string and returns the resulting value.&nbsp; Calculator uses &quot;eval&quot; 
		to evaluate the calculator expressions.&nbsp; The &quot;str&quot; function will then convert the value 
		into a string for display.</p>
		</li>
	</ol>
	<p class="Code-Background">def on_Equals(self, b, e):<br />
&nbsp;&nbsp;&nbsp; try:</p>
	<p class="Code-Highlighted">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result 
	= str(eval(self.expression))</p>
	<p class="Code-Background">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.result.Text 
	= result<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.expression = result<br />
&nbsp;&nbsp;&nbsp; except:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.result.Text = &quot;&lt;&lt;ERROR&gt;&gt;&quot;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.expression = &quot;&quot;</p>
	<ol start="11">
		<li>
		<p class="Step">Exit the IronPython Interactive console (Ctrl+Z or F6 followed 
		by Enter)</p>
		</li>
	</ol>
	<p class="LabSummary">Tutorial Summary</p>
	<p class="Body">IronPython provides very easy way to develop live applications 
	in a dynamic and exploratory way. Both Windows Forms and Windows Presentation 
	Foundation (Avalon) applications can be easily developed this way with minimal 
	setup. The advantage is that changes are visible immediately and modifications 
	are happening to live system and don&#39;t require recompilation.</p>
	<p class="Body">In this tutorial you performed the following exercises:</p>
	<ul>
		<li>
		<p class="ExerciseList"><a href="#T2.1">Events and Delegates</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T2.2">Windows Forms</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T2.3">Windows Presentation Foundation 
		(Avalon)</a></p>
		</li>
	</ul>
	<p class="Body">&nbsp;</p>
	<p class="Body">In this tutorial, you became familiar with using delegates and 
	handling events in IronPython - an essential part of interactive development 
	of Windows applications using WinForms or Avalon. Then you dynamically created 
	simple interactive application in Windows Forms and created two applications 
	using Windows Presentation Foundation. </p>
	<h1><a name="T3">Tutorial 3: IronPython and COM interoperability</a></h1>
	<p class="Body">COM interoperability is an important part of .NET Framework. 
	To use COM objects from the .NET application, an interop assembly that contains 
	the .NET metadata for the COM objects is required. This tutorial will outline 
	how the interop assemblies can be created (in the case they are not provided 
	by the creators of the COM object) and will demonstrate the ease with which 
	COM objects can be used from IronPython.</p>
	<p class="Body">Estimated time to complete this tutorial: <b>20 minutes</b></p>
	<p class="Body">The objective of this tutorial is to explore COM interoperability 
	from IronPython.</p>
	<ul>
		<li>
		<p class="ExerciseList"><a href="#T3.1">Merlin the Wizard</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T3.2">Word Spell Checking</a></p>
		</li>
	</ul>
	<h2><a name="T3.1">Exercise 1: Merlin the Wizard</a></h2>
	<p class="Body">This exercise consists of two tasks.&nbsp; In the first one we will 
	create an interop assembly for a COM object library and explore it using the 
	.NET reflection tools.&nbsp; In the second task we will use the COM object library 
	from IronPython.</p>
	<p class="Body">In this exercise, you will create a COM interop assembly and 
	use it to drive the COM object from IronPython.</p>
	<h3><a name="T3.1.1">Task 1: Creating COM interop assemblies</a></h3>
	<ol>
		<li>
		<p class="Step">Open the &quot;SDK Command Prompt&quot; or &quot;Visual Studio 2005 Command 
		Prompt&quot; from the start menu.</p>
		</li>
		<li>
		<p class="Step">The .NET Frameworks SDK comes with the &quot;tlbimp&quot; tool which converts 
		a COM type library into .NET metadata information -- the interop assembly.&nbsp; The COM object library we want to use from IronPython 
		is msagent, located 
		in %SystemRoot%\msagent\agentsvr.exe.&nbsp; Use the tlbimp tool to generate the 
		interop assembly for msagent (tlbimp writes to the current directory):</p>
		</li>
	</ol>
	<p class="TypedExample">tlbimp %SystemRoot%\msagent\agentsvr.exe</p>
	<p class="Code-Background">C:\IronPython\Tutorial&gt;tlbimp %SystemRoot%\msagent\agentsvr.exe<br />
	Microsoft (R) .NET Framework Type Library to Assembly Converter 2.0.50727.42<br />
	Copyright (C) Microsoft Corporation.&nbsp; All rights reserved.<br />
&nbsp;</p>
	<p class="Code-Highlighted">Type library imported to AgentServerObjects.dll</p>
	<ol start="3">
		<li>
		<p class="Step">The .NET metadata information was saved in AgentServerObjects.dll.&nbsp; Use the ildasm tool to explore the generated DLL:</p>
		</li>
	</ol>
	<p class="TypedExample">ildasm AgentServerObjects.dll</p>
	<ol start="4">
		<li>
		<p class="Step">As you browse the metadata tree, notice that at the root 
		of the hierarchy there is the &quot;AgentServerObjects&quot; namespace.&nbsp; The namespace 
		contains many interfaces and a single class - AgentServerClass.&nbsp; Explore 
		the methods on the class.&nbsp; Calling some of its methods will be the subject 
		of the next task.</p>
		</li>
		<li>
		<p class="Step">When you are done exploring the AgentServerObjects.dll, 
		close the ildasm tool.&nbsp; Copy AgentServerObjects.dll to the tutorial 
		directory, and then close the Windows command prompt.</p>
		</li>
	</ol>
	<h3><a name="T3.1.2">Task 2: Using COM objects from IronPython</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Using the &quot;AddReferenceToFile&quot; function in the 
		clr module, 
		add a reference to the AgentServerObjects.dll:</p>
		</li>
	</ol>
	<p class="TypedExample">import clr</p>
	<p class="TypedExample">clr.AddReferenceToFile(&quot;AgentServerObjects.dll&quot;)</p>
	<ol start="3">
		<li>
		<p class="Step">Import the contents of the AgentServerObjects namespace 
		and explore the contents using dir():</p>
		</li>
	</ol>
	<p class="TypedExample">from AgentServerObjects import *</p>
	<p class="TypedExample">dir()</p>
	<ol start="4">
		<li>
		<p class="Step">Create instance of the AgentServerClass and explore its 
		contents:</p>
		</li>
	</ol>
	<p class="TypedExample">a = AgentServerClass()</p>
	<p class="TypedExample">dir(a)</p>
	<p class="Stepnonumbering">The output in the console window will be:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; a = AgentServerClass()<br />
	&gt;&gt;&gt; dir(a)</p>
	<p class="Code-Background">[&#39;CreateObjRef&#39;, &#39;Equals&#39;, &#39;GetCharacter&#39;, &#39;GetHashCode&#39;, 
	&#39;GetLifetimeService&#39;, &#39;GetSuspended&#39;, &#39;GetType&#39;, &#39;InitializeLifetimeService&#39;, 
	&#39;Load&#39;, 'MemberwiseClone', &#39;Register&#39;, &#39;ToString&#39;, &#39;Unload&#39;, &#39;Unregister&#39;, &#39;__class__', 
	'__init__', '__module__', '__new__']</p>
	<ol start="5">
		<li>
		<p class="Step">Explore the parameters of the Load method on AgentServerClass:</p>
		</li>
	</ol>
	<p class="TypedExample">a.Load.__doc__<br />
&nbsp;</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; a.Load.__doc__</p>
	<p class="Code-Background">&#39;(int, int) Load(object, vLoadKey)\r\n&#39;</p>
	<p class="Normal">&nbsp;</p>
	<p class="Stepnonumbering">The Load methods have an input parameter (name of 
	the character to load) and two output parameters.&nbsp; When calling this method, IronPython will turn the two output parameters into 
	multiple return values.</p>
	<ol start="6">
		<li>
		<p class="Step">Load &quot;Merlin the Wizard&quot; character. (The character data 
		is stored in c:\WINDOWS\msagent\chars). The Load method returns multiple 
		values packed into a tuple - the 
	first value is the actual character ID. Capture the character id in the variable:</p>
		</li>
	</ol>
	<p class="TypedExample">cid = a.Load(&quot;Merlin.acs&quot;)[0]</p>
	<ol start="7">
		<li>
		<p class="Step">Call GetCharacter method on the AgentServerClass instance 
		to get the actual character, display the character and have him think about 
		IronPython Tutorial:</p>
		</li>
	</ol>
	<p class="TypedExample">c = a.GetCharacter(cid)</p>
	<p class="TypedExample">c.Show(0)</p>
	<p class="TypedExample">c.Think(&quot;IronPython Tutorial&quot;)</p>
	<ol start="8">
		<li>
		<p class="Step">Merlin has a lot of animations he can play.&nbsp; Get list of 
		all the animations by calling the GetAnimationNames method. Play one of the animations, 
		&quot;Read&quot;, by calling the Play method:</p>
		</li>
	</ol>
	<p class="TypedExample">for n in c.GetAnimationNames(): print n</p>
	<p class="TypedExample">c.Play(&quot;Read&quot;)</p>
	<ol start="9">
		<li>
		<p class="Step">(Optional) To call a series of animations, we could call c.Play(animation_name) 
		for each animation.&nbsp; Python&#39;s lamda functions can simplify this process.&nbsp; We can create global functions and assign them names based on the animation 
		name:</p>
		</li>
	</ol>
	<p class="TypedExample">for n in c.GetAnimationNames(): globals()[n] = lambda 
	name=n: c.Play(name)</p>
	<p class="Stepnonumbering">&nbsp;</p>
	<p class="Stepnonumbering">At this point we can have Merlin play animations 
	simply by<br />
	calling a function from global namespace:</p>
	<p class="TypedExample">Congratulate()</p>
	<ol start="10">
		<li>
		<p class="Step">Stop Merlin&#39;s animations, hide him, and exit IronPython console</p>
		</li>
	</ol>
	<p class="TypedExample">c.StopAll(0)</p>
	<p class="TypedExample">c.Hide(0)</p>
	<h2><a name="T3.2">Exercise 2: Use Word for Spell Checking</a></h2>
	<p class="Body">In this exercise you will create an instance of Microsoft 
	Word and use it to check word spellings.</p>
	<h3><a name="T3.2.1">Task 1: Accessing Word and Checking Spelling</a></h3>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Import clr module and add a reference to the Word COM 
		interop assembly.&nbsp; Also, import System so that we can use a special 
		value from it later.</p>
		</li>
	</ol>
	<p class="TypedExample">import clr</p>
	<p class="TypedExample">
	clr.AddReferenceByPartialName(&quot;Microsoft.Office.Interop.Word&quot;)<br>
	from Microsoft.Office.Interop.Word import ApplicationClass<br>
	<br>
	import System</p>
	<ol start="3">
		<li>
		<p class="Step">Start an instance of Word as a COM server running.&nbsp; 
		You won't see it show up since it is hidden, but you can see it in the 
		Windows Task Manager by typing ctrl-shift-escape and looking for the 
		WINWORD.EXE process.</p></li>
	</ol>
	<p class="TypedExample">w = ApplicationClass()</p>
	<ol start="4">
		<li>
		<p class="Step">Define the following function to check the spelling.&nbsp; 
		We have to build up an argument list so that we can supply the 12 
		optional arguments we do not care about (System.Type.Missing).&nbsp; We 
		get the answer by taking the first (at index zero) of a few return 
		values (out parameters in COM interop).&nbsp; Remember to indent the 
		lines of the function's body extra spaces, and you have to hit an extra 
		return or enter to complete the function's definition.</p></li>
	</ol>
	<p class="TypedExample">def check_word (word):<br>
&nbsp;&nbsp; args = [word] + [System.Type.Missing]*12<br>
&nbsp;&nbsp; return w.CheckSpelling(*args)[0]</p>
	<p class="TypedExample">&nbsp;</p>
	<p class="TypedExample">check_word(&quot;foo&quot;)</p>
	<p class="TypedExample">check_word(&quot;food&quot;)</p>
	<ol start="5">
		<li>
		<p class="Step">You can try that out on a couple of words, but now lets 
		define a function that will suggest corrections for us.&nbsp; First, we 
		need to add a document so that we can call GetSpellingSuggestions(), 
		which gives a nice error message if you try to call it with no documents 
		opened.</p></li>
	</ol>
	<p class="TypedExample">w.Documents.Add(*[System.Type.Missing]*4)</p>
	<ol start="6">
		<li>
		<p class="Step">The 
		function we'll define builds an argument list just like check_word() did to 
		supply several unneeded optional arguments.&nbsp; The first result of 
		several return values from GetSpellingSuggestions() is a collection of 
		items, each of which is a correction suggestion.&nbsp; We use a Python 
		list comprehension to iterate through the COM collection object and call 
		the Name property on each item object in the collection.&nbsp; Each 
		item's Name property is a string that Word is suggesting as a correct 
		spelling.</p></li>
	</ol>
	<p class="TypedExample">def suggestions (word):<br>
&nbsp;&nbsp; args = [word] + [System.Type.Missing]*13<br>
&nbsp;&nbsp; res_objects = w.GetSpellingSuggestions(*args)[0]<br>
&nbsp;&nbsp; return [x.Name for x in res_objects]</p>
	<ol start="7">
		<li>
		<p class="Step">Now, let's shut down Word and exit the IronPython 
		console.&nbsp; When you enter the next line and hit return, if you are 
		watching the Windows Task Manager, you will see the WINWORD.EXE process 
		go away.</p></li>
	</ol>
	<p class="TypedExample">w.Quit(*[System.Type.Missing]*3)</p>
	<p class="Body">&nbsp;</p>
	<h3><a name="T3.2.2">Task 2: Use Windows Form Dialog to Correct Spelling</a></h3>
	<p class="Body">In this task you will use an example already written that is 
	very similar to what you did in Task 1, but it demonstrates two additional 
	features.&nbsp; It uses a Python standard module to register a clean up 
	function so that when the IronPython console terminates, the Word process is 
	closed.&nbsp; The example also has a function, correct_word, that uses a 
	Windows Form as a dialog to let users select a correct word to return.</p>
	<ol>
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
		<li>
		<p class="Step">Import spellcheck.</p>
		</li>
		<li>
		<p class="Step">Call spellcheck.correct_word on a correctly spelled 
		word, and you'll see the function returns the word.</p>
		</li>
		<li>
		<p class="Step">Call spellcheck.correct_word on a misspelled word, and a 
		Windows Form similar to Word's spelling correction dialog appears.&nbsp; 
		You can pick from the list and click the Replace button, double click a 
		word in the suggestions list, enter custom text and click the Use Custom 
		button, click an Ignore button to return the misspelled word, and so on. </p>
		</li>
		<li>
		<p class="Step">Open the spellcheck.py file to see the code for the the 
		Windows Form as well as the functions.</li>
	</ol>
	<p class="Body">&nbsp;</p>
	<p class="LabSummary">Tutorial Summary</p>
	<p class="Body">This tutorial was focused on using COM interop assemblies generated 
	via tlbimp tool (in the .NET Framework SDK) to control COM objects from the IronPython environment.</p>
	<p class="Body">In this tutorial you performed the following exercises.</p>
	<ul>
		<li>
		<p class="ExerciseList"><a href="#T3.1">Merlin the Wizard</a></p>
		</li>
		<li>
		<p class="ExerciseList"><a href="#T3.2">Word Spell Checking</a></p>
		</li>
	</ul>
	<p class="Body">&nbsp;</p>
	<p class="Body">In this tutorial, you created a COM interop assembly for the 
	msagent COM object.&nbsp; You used the tlbimp tool which is part of the .NET Framework 
	SDK.&nbsp; Using the ildasm tool, you explored contents of the interop assembly.</p>
	<p class="Body">Then, from IronPython you loaded the interop assembly, created 
	an instance of the COM object, and instructed Merlin to play different animations.</p>
	<p class="Body">You also used Word to check the spelling of words, learned 
	how to supply missing optional arguments, and inspected code that builds a 
	Windows Form and uses it as a dialog.</p>
	<p class="Normal">&nbsp;</p>
	<h1><a name="T4">Tutorial 4: Debugging IronPython program</a></h1>
	<p class="Body">This tutorial will walk you through very simple debug session, 
	debugging a Python script using Microsoft CLR Debugger.&nbsp; See
	<a href="#T7">using Visual Studio</a> if you have a Visual Studio SKU and 
	consider skipping this exercise.</p>
	<p class="Body">Estimated time to complete this tutorial: <b>10 minutes</b></p>
	<p class="Body">The objective of this tutorial is debugging simple Python script 
	using Microsoft CLR Debugger.</p>
	<ul>
		<li>
		<p class="Body"><a href="#T4.1">Debugging IronPython program</a></p>
		</li>
	</ul>
	<h2><a name="T4.1">Exercise 1: Debugging IronPython program</a></h2>
	<p class="Body">In this exercise, you will step through a simple Python script 
	in the Microsoft CLR Debugger.</p>
	<h3><a name="T4.1.1">Task 1: Debugging IronPython program using Microsoft CLR 
	Debugger</a></h3>
	<ol>
		<li>
		<p class="Step">Launch Microsoft CLR Debugger. </p>
		</li>
		<li>
		<p class="Step">From the debugger Menu, select Debug / Program to Debug 
		...</p>
		</li>
		<li>
		<p class="Step">For Program, browse to the ipy.exe (located 
		in the installaton directory)</p>
		</li>
		<li>
		<p class="Step">For Arguments, type in debugging.py</p>
		</li>
		<li>
		<p class="Step">Change Working directory to the Tutorial directory</p>
		</li>
		<li>
		<p class="Step">Click OK</p>
		</li>
		<li>
		<p class="Step">From Menu, select File / Open / File. Browse to the Tutorial 
		directory and select two files to open:</p>
		</li>
	</ol>
	<p class="TypedExample">debugging.py</p>
	<p class="TypedExample">first.py</p>
	<ol start="8">
		<li>
		<p class="Step">Place breakpoint at the following line of the debugging.py 
		file (Place cursor on the line and pres F9):</p>
		</li>
	</ol>
	<p class="Code-Highlighted">print first.add(1, 2)</p>
	<ol start="9">
		<li>
		<p class="Step">Press F5 - Start Debugging.</p>
		</li>
	</ol>
	<p class="Stepnonumbering">IronPython will compile the debugging.py file and 
	start executing it. You will hit the breakpoint at the line 3.</p>
	<p class="StepNote">Note: If you get poor performance starting the debugging 
	session, exit the debugger, open the Windows Explorer and delete the following 
	directory:</p>
	<p class="StepNote">%USERPROFILE%\Application Data\Microsoft\DbgClr</p>
	<ol start="10">
		<li>
		<p class="Step">Pressing F11, step through the execution of the program, 
		explore variables (even change the values of the variables in the watch 
		window) and explore the call stack. </p>
		</li>
		<li>
		<p class="Step">End the debugging session and exit the debugger.</p>
		</li>
	</ol>
	<p class="LabSummary">Tutorial Summary</p>
	<p class="Body">In this tutorial you performed the following exercises.</p>
	<ul>
		<li>
		<p class="Body"><a href="#T4.1">Debugging IronPython program</a></p>
		</li>
	</ul>
	<p class="Body">In this tutorial, you walked through the simple debug session 
	of IronPython program. You used Microsoft CLR Debugger, placed breakpoints and 
	stepped through the Python program execution.</p>
	<p class="Body">&nbsp;</p>
	<h1><a name="T5">Tutorial 5: Extending IronPython</a></h1>
	<p class="Body">Estimated time to complete this tutorial: <b>25- 60 minutes</b></p>
	<p class="Body">The objective of this tutorial is to implement the class which 
	will seamlessly fit into the IronPython environment. You can choose to follow 
	Exercise 1 - C# implementation, Exercise 2 - Visual Basic, or both. Both tracks 
	will result in the same functionality.</p>
	<ul>
		<li>
		<p class="Body"><a href="#T5.1">Extending using C#</a></p>
		</li>
		<li>
		<p class="Body"><a href="#T5.2">Extending using Visual Basic.NET</a></p>
		</li>
	</ul>
	<h2><a name="T5.1">Exercise 1: Extending using C#</a></h2>
	<p class="Body">In this exercise you will use C# language to build a class that 
	supports enumeration, custom operators and delegates and you will use that class 
	from IronPython.  It’s worth noting that as Python does not contain protected class members,
        that these members get promoted to public class members when imported into Python.  Private
        class members, however, will be invisible to Python when imported.</p>
	<h3><a name="T5.1.1">Task 1: Implementing simple class - constructor and ToString</a></h3>
	<ol>
		<li>
		<p class="Step">Open the &quot;SDK Command Prompt&quot; or &quot;Visual Studio 2005 Command 
		Prompt&quot; from the start menu.</p>
		</li>
		<li>
		<p class="Step">Open the &quot;csextend.cs&quot; file in notepad. The file is initially 
		empty</p>
		</li>
	</ol>
	<p class="TypedExample">notepad csextend.cs</p>
	<ol start="3">
		<li>
		<p class="Step">Add using clauses at the beginning of the file:</p>
		</li>
	</ol>
	<p class="Code-Highlighted">using System;<br />
	using System.Collections;</p>
	<ol start="4">
		<li>
		<p class="Step">Define a simple class:</p>
		</li>
	</ol>
	<p class="Code-Highlighted">public class Simple {<br />
&nbsp;&nbsp;&nbsp; private int data;</p>
	<p class="Code-Highlighted"><br />
&nbsp;&nbsp;&nbsp; public Simple(int data) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.data = data;<br />
&nbsp;&nbsp;&nbsp; }</p>
	<p class="Code-Highlighted"><br />
&nbsp;&nbsp;&nbsp; public override string ToString() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return String.Format(&quot;Simple&lt;{0}&gt;&quot;, data);<br />
&nbsp;&nbsp;&nbsp; }<br />
	}</p>
	<ol start="5">
		<li>
		<p class="Step">Switch back to the SDK Command Prompt window and build the 
		code using available csx.bat (C# extension):</p>
		</li>
	</ol>
	<p class="TypedExample">csx</p>
	<ol start="6">
		<li>
		<p class="Step">Start the IronPython console from the tutorial directory 
		(see <a href="#Intro">Introduction</a> for details).</p>
		</li>
	</ol>
	<ol start="7">
		<li>
		<p class="Step">Load the dll that you just built (csextend.dll) into IronPython. 
		Then explore the Simple class using built-in dir() function:</p>
		</li>
	</ol>
	<p class="TypedExample">import clr</p>
	<p class="TypedExample">clr.AddReferenceToFile(&quot;csextend.dll&quot;)</p>
	<p class="TypedExample">import Simple</p>
	<p class="TypedExample">dir(Simple)</p>
	<p class="Stepnonumbering">You will see following output:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; import clr<br />
	&gt;&gt;&gt; clr.AddReferenceToFile(&quot;csextend.dll&quot;)<br />
	&gt;&gt;&gt; import Simple<br />
	&gt;&gt;&gt; dir(Simple)</p>
	<p class="Code-Background">[&#39;Equals&#39;, &#39;GetHashCode&#39;, &#39;GetType&#39;, &#39;ToString&#39;, 
	&#39;__new__&#39;, &#39;__repr__&#39;]</p>
	<ol start="8">
		<li>
		<p class="Step">Create instance of the Simple class:</p>
		</li>
	</ol>
	<p class="TypedExample">s = Simple(10)</p>
	<p class="TypedExample">print s</p>
	<p class="Stepnonumbering">IronPython will output:</p>
	<p class="Code-Highlighted">&gt;&gt;&gt; s = Simple(10)<br />
	&gt;&gt;&gt; print s</p>
	<p class="Code-Background">Simple&lt;10&gt;</p>
	<ol start="9">
		<li>
		<p class="Step">You have just explored all the functionality currently available 
		in the Simple class. Exit IronPython and return back to the C# code to add 
		more functionality.</p>
		</li>
	</ol>
	<h3><a name="T5.1.2">Task 2: Making the object enumerable</a></h3>
	<ol>
		<li>
		<p class="Step">In Notepad, add more functionality to the Simple class.</p>
		</li>
		<li>
		<p class="Step">First, inherit the Simple class from IEnumerable and implement 
		GetEnumerator() method. Use the C#&#39;s new &quot;yield return&quot; statement:</p>
		</li>
	</ol>
	<p class="Code-Highlighted">public class Simple : IEnumerable {</p>
	<p class="Code-Background"><br />
&nbsp;&nbsp;&nbsp; private int data;<br />
&nbsp;&nbsp;&nbsp; public Simple(int data) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.data = data;<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; public override string ToString() {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return String.Format(&quot;Simple&lt;{0}&gt;&quot;, data);<br />
&nbsp;&nbsp;&nbsp; }<br />
&nbsp;</p>
	<p class="Code-Highlighted">&nbsp;&nbsp;&nbsp; public IEnumerator GetEnumerator() 
	{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; for (int i = 0; i &lt; data; i ++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; yield return 
	new Simple(i);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
&nbsp;&nbsp;&nbsp; }</p>
	<p class="Code-Background">}</p>
	<ol start="3">
		<li>
		<p class="Step">Save the changes, ccompile the code (csx), launch IronPython 
		Console (ip) and test the code:</p>
		</li>
	</ol>
	<p class="TypedExample">import sys</p>
	<p class="TypedExample">clr.AddReferenceToFile(&quot;csextend.dll&quot;)</p>
	<p class="TypedExample"