written in Python. I started a C# library class for ConceptNet and needed to trace though it. So I then found myself sidetracked by writing this IronTextBox control for myself. It may be useful to others and it may not. However, it is another small introduction into Microsoft's IronPython.
This article's IronTextBox is for IronPython 1.1. For the latest version please see
IronTextBox
The TextBox control that I developed to help me understand some unfamiliar python code is IronTextbox. I find it useful to see what IronPython's limits are since it is still in its beta stage. Plus IronPython is staged on a CLI platform and this TextBox control creates more of a non-CLI platform. It currently has three namespaces
UIIronTextBox
Here is a screen capture of the help menu for IronTextBox 1.1.5.0b
The Demo
This demo uses a TextBox and UserControl based control that I named IronTextBox. It is to be used with Microsoft's IronPython. There is a bunch of useful methods in Python and IronPython 1.0 does a great job of implementing all of the common ones. Let the record state that I am not an expert at Python nor IronPython but am just learning both at the same time and it is amazing to me the work the IronPython team has done. I created this control to help me evaluate some Python scripts and learn IronPython.
IronPython 1.0 comes with several Console classes but I decided not to use them in this demo because I would have to distribute the classes within the project. Besides, what fun would that be. Also, the IronTextBox can be used as a COM object so anyone may use it to help test their applications. At least that's what I use it for.
I first started out with a TextBox based control then thought a nice color control would be outstanding. I have a very rough draft RichTextBox control still in active development. I may post the project with this article but as I am typing this I have not decided. I had to quit developing the control because I was having coloring problems with strings. I am not that up on my Rtf knowledge so due to me spinning my wheels for a couple of days I decided I was sidetracked and bailed because learning Rtf was not my main goal. So that is why the control in black and white.
I have tried to make the code easy to follow and included as many comments as I could. Therefore, I am time pressed and will not go into depth in this article. Microsoft has plenty of outstanding demos to use, although I understand that it is beta and still does not have much documentation.
Probably the most important thing to know about IronPython's PythonEngine is that it handles Python script by Executing it or Evaluating it. As long as you understand that then learning the rest of IronPython is not that difficult.
In myIronPythonDemo 1.2 try stepping thru some Python code. If you have installed ConceptNet 2.1 (please see my ConceptNet article) click on the "MontyLemmatiser commands" button and this imports MontyREChunker (if your UIIronTextBox.Paths are set correctly) and runs its python method. THIS DEMO WILL ERROR OUT. Set a breakpoint on line 92 in MontyREChunker.py. (you may need to run this first for proper results, as of this writing I am not sure why it does not like to run behind one of the other click events)
Here is a screen shot of Microsoft Visual C# Express 2005 debugging a Python module:
If you download the latest release of the Visual Studio 2005 SDK you may run VS 2005 under it's Experimental Hive which supports IronPython integration. In this mode, it will do Python syntax coloring for you and also auto-indentation.
Below is an incomplete custom MessageBox I wrote to help me debug. It is now located in the UIIronTextBox.Utils namespace.
public void MessageBoxIronPy(Object inobject)
{
Type itstype = inobject.GetType();
switch (itstype.FullName)
{
case "IronPython.Runtime.Tuple":
Tuple IPTuple = new Tuple((Tuple)inobject);
MessageBox.Show(IPTuple.ToString());
break;
case "IronPython.Runtime.Dict":
Dict IPDict = new Dict();
IPDict = (Dict)inobject;
MessageBox.Show(IPDict.ToString());
break;
case "IronPython.Runtime.List":
List IPList = new List();
IPList = (List)inobject;
MessageBox.Show(IPList.ToCodeString());
break;
case "System.String":
MessageBox.Show(inobject.ToString());
break;
case "System.Int32":
MessageBox.Show(Convert.ToString(inobject));
break;
case "System.Collections.Specialized.StringCollection":
StringCollection IPSC = new StringCollection();
IPSC = (StringCollection)inobject;
StringEnumerator SCE = IPSC.GetEnumerator();
string output = "";
while (SCE.MoveNext())
output += SCE.Current.ToString();
MessageBox.Show(output);
break;
default:
MessageBox.Show
(inobject.GetType().ToString() + " not yet implemented.");
break;
}
}
Try using the IronTextBox console after running the first.py demo. When it has completed type "first.hi" at the prompt and then press enter. That executes first's hi method and displays its return value in the IronTextBox.