Click here to Skip to main content
Click here to Skip to main content

IronTextBox2

By , 20 Jun 2008
 

Introduction

As of this writing, IronPython version 2 (IP2) is now in beta after months of alpha builds so I decided it was safe enough to start on the next version of IronTextBox to work with IP2. If you are working with IP1.1, here is the version of IronTextBox that will work with IP1.1. Many new IronPython users often ask how to host an IronPythonEngine within a Form, or for simple out of the box code to look at. I first created IronTextBox when IronPython was rather new. Since then, many improvements and other development tools have been developed by Microsoft to help dissect Python code, like IronPython Studio. IronTextBox is still pretty handy because you can quickly browse to one Python file on the fly and get it running in the CLI.

IronTextBox.dll Library

IronTextBox was designed to be a CLI-like control to be drag-n-dropped into a Form or other .NET controls.

  • UIIronTextBox: is the main namespace
  • UIIronTextBox.IPEWrapper: Overridden Stream class to handle IP output
  • UIIronTextBox.Paths: Easily accessible common paths
  • UIIronTextBox.Utils: Not much here, just some early experimentation with debugging

IronTextBox.dll Classes

What's Different in IronTextBox 2?

Too much has changed from IronPython 1.1 but one of the biggest changes in my mind is the PythonEngine and running Python code. IP2 is more centered around the namespace Microsoft.Scripting. So, you can not easily create a PythonEngine object and start running. In IronTextBox, I created three methods which pretty much handle the main things that need to happen and to easily replace IronTextBox version 1 code:

/// Executes the Python file within the IronTextBox environment.
/// A nice way to quickly get a Python module in CLI to test or use.
object DoIPExecuteFile(string pyfile)
{
    ScriptSource source = engine.CreateScriptSourceFromFile(pyfile);
    return source.Execute(scope);
}

/// Executes the code in SourceCodeKind.SingleStatement to fire the command event
/// Use DoIPEvaluate if you do not wish to fire the commandevent
object DoIPExecute(string pycode)
{
    ScriptSource source = engine.CreateScriptSourceFromString(pycode, 
                          SourceCodeKind.SingleStatement);
    return source.Execute(scope);
}

/// Executes the code in SourceCodeKind.Expression not to fire the command event
/// Use DoIPExecute if you do wish to fire the commandevent
object DoIPEvaluate(string pycode)
{
    ScriptSource source = engine.CreateScriptSourceFromString(pycode, 
                          SourceCodeKind.Expression);
    return source.Execute(scope);
}

Here are some details on what's new in each file:

//Paths.cs - version 2.0.2.0b
//
// WHAT'S NEW:
//      - Updated IronPython_Tutorial to point to IronPython 2.0B2 path
//      - Added Python25Dirs 
////////////////////////////////////
//IronTextBox.cs - version 2.0.2.0b
//WHAT'S NEW: 
//      -IronPython 2.0B2 Support
//      -Removed ParsetheText() since IronPython.Compiler.Ast removed in IP2SimEnter();
//      -Added DoIPExecuteFile(), DoIPExecute(), DoIPEvaluate()
////////////////////////////////////

Usage

When using IronTextBox, your executable directory must contain:

  • IronTextBox.dll
  • IronPython.dll
  • IronPython.Modules.dll
  • Microsoft.Scripting.Core.dll
  • Microsoft.Scripting.dll

Just add references to these in your project and set Properties for Copy Local to True. All of these files (besides IronTextBox.dll, of course) is found in the IronPython 2.0B2 download.

Tip: try including the files above if you receive the following exception: "Microsoft.Scripting.InvalidImplementationException : Type 'IronPython.Runtime.PythonContext' doesn't provide a suitable public constructor or its implementation is faulty."

Running this Demo

  1. Make sure you have downloaded and extracted IronPython 2.0B2 (or try IronPython's main website for the latest release). FYI: I have hardcoded the IronPython script folder to Environment.SpecialFolder.MyDocuments + @"\Visual Studio 2008\Projects\IronPython-2.0B2\Tutorial" in the project. However, I have implemented an OpenFileDialog to correct any incorrect paths.
  2. Download and unzip this article's .NET solution and project files.
  3. From MS VC# 2008 Solution Explorer, add a reference by browsing to IronPython.dll, IronPython.Modules.dll, Microsoft.Scripting.Core.dll, Microsoft.Scripting.Core.dll, and Microsoft.Scripting.dll located where you extracted the IronPython download.

If you are new to IronPython, you may run the IronTextBox command 'runfile' and then browse to IronPython-2.0B2\Tutorial\first.py; notice how you may use add, factorial, and print hi. You can run a 'dir()' command to see what is available.

Conclusion

IronTextBox v2 can be dropped into a Form to provide a Command Line Interface (CLI) with IronPython 2.0B2. The previous version of IronTextBox works with IP1.1. IronTextBox also displays ways to work with IronPython 2.0B2.

References

Updates

  • 5/26/08
    • Article and IronTextBox version 2.0.2.0 posted.
  • 6/10/08
    • Compiled and tested with IronPython 2.03B.
    • Updated source License from GNU to Expat/MIT.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

JoeSox
Network Administrator
United States United States
Member
Born in Bristol PA, just north of Philadelphia. Joe has been programming since he was ten. He is entirely self-taught programmer, and he is currently working as a Network Administrator in Seattle WA. He is currently US Navy Active Reservist for (SPAWAR)
In '98 was honorably discharged from the USN. He served onboard the USS Carl Vinson (94-98) He was lucky enough to drink President Clinton's leftover wine, promoted by his Captain, and flew in a plane off the flightdeck but not all at the same time. His interests, when time allows, are developing
misc apps and Artificial Intelligence proof-of-concept demos that specifically exhibits human behavior. He is a true sports-a-holic, needs plenty of caffeine, and a coding junkie. He also enjoys alternative music and a big Pearl Jam, Nirvana, and new alternative music fan.
 
Joe owns the url www.humanaiproject.org[^]. He has submitted an abstract to DARPA in Dec 03 proposing his proposed HAI framework. Joe is an INTP[^] personality type. Joe "sees everything in terms of how it could be improved, or what it could be turned into. INTP's live primarily inside their own minds." INTPs also can have the "greatest precision in thought and language. Can readily discern contradictions and inconsistencies. The world exists primarily to be understood. 1% of the total population" [

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionCompilation IssuesmemberNISM19061 Nov '12 - 2:11 
AnswerRe: Compilation IssuesmemberJoeSox1 Nov '12 - 3:51 
QuestionCan I add your code into this project and maintenance it? [modified]memberbegtostudy9 Aug '10 - 18:41 
AnswerRe: Can I add your code into this project and maintenance it?memberJoeSox9 Aug '10 - 20:00 
General2.6 Release Candidate 2memberArnie197828 Oct '09 - 22:14 
QuestionLicense listed here overrides license in source code?memberbatesbc19 Jun '08 - 7:16 
AnswerRe: License listed here overrides license in source code?memberJoeSox20 Jun '08 - 4:23 
GeneralFormatting broken in some place(s)member leppie 26 May '08 - 21:01 
GeneralRe: Formatting broken in some place(s)memberJoeSox27 May '08 - 4:08 
GeneralRe: Formatting broken in some place(s)member leppie 27 May '08 - 5:52 
GeneralRe: Formatting broken in some place(s)memberJoeSox28 May '08 - 19:26 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 20 Jun 2008
Article Copyright 2008 by JoeSox
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid