Interactive Python Shell in Construct

Post your own tutorials, guides and demos.

Post » Mon Apr 05, 2010 2:12 am

[color=#800000:rsq3vxjr]EDIT:
5/01/10 - Updated the screen shot and attachment with an improved version.
5/02/10 - Updated again with a few minor bug fixes and features. See my next posts for details.[/color:rsq3vxjr]

Hello. I've been messing a bit with Construct for the past few weeks, and I was happy to see that Construct supported Python scripting. I've always wanted to learn Python, but never had a compelling reason to do so until now, so I've spent the last couple of weeks learning Python.

I must say, I really like this language. It's been ages since I've programmed anything, and I've never used object-oriented programming, but I've found it quite easy to learn Python and OOP so far. My previous programming experience is quite dusty (from Commodore Amiga era) in ASM, Basic, C, and ARexx.

I've had a hard time figuring out how to use Construct objects from Python scripts, so I had an idea for getting some info on them. I created a simple interactive shell from which I could query object attributes and methods, and also interact with Construct objects step-by-step. It's also o.k. for just messing around with Python interactively, but not quite as nicely as Python's own IDLE shell.

Anyway, I thought I'd post this in the event that it may be helpful to one of the other Python users some time. Here's a screen shot that shows a sample of usage:

[url:rsq3vxjr]http://dl.dropbox.com/u/5868916/PyShell.jpg[/url:rsq3vxjr]

I've found the dir() and help() commands quite helpful, and if you re-size/move things a bit, you can plop Construct objects in there and play with them through Python, too. It's not as useful as it could be as a plugin of some sort, maybe, but it's helpful as a simple tool. Usage is pretty straightforward, I think.

Cap is attached for download.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Mon Apr 05, 2010 4:14 am

I like it! Very useful.
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Tue Apr 06, 2010 3:42 am

Very nice addition! Thanks for creating and sharing.
B
8
S
3
G
7
Posts: 835
Reputation: 5,313

Post » Sat May 01, 2010 4:32 am

Thanks for the comments. :) I've updated this a bit.

I've found myself using this more often, so I set about making it a bit more user friendly. After a fair amount of trying to force Construct's EditBox object to do my bidding, the result is what I consider to be a fairly respectable command line shell. It works in a manner similar to the Windows version of the Python IDLE Shell, with a command history that works similarly to older shells. Some notable features:

- Like the Python IDLE Shell, when a command block is entered, if it evaluates to an expression, the shell will print a representation of the result (if it is not the value None.) Otherwise, it is simply executed.

- Auto indentation similar to the Python IDLE Shell.

- Keeps a display history of (approximately) the most recent 90,000 characters in the shell. This is able to be changed easily. Higher numbers may slow the shell's responsiveness. This display history may not be changed, but may be moved through in order to copy portions and such.

- Keeps a command history of all command blocks that have been entered. These commands are recalled in a manner similar to the way that old unix shells work. In this case, 'control - up arrow' moves in reverse through the commands, and 'control - down arrow' moves forward through the commands. Recalled commands replace any current command entry, and may be edited (or not) and executed again.

- All variables used in the code here were added to the 'Shell' object so that they would not pollute the global namespace. Otherwise, there is a PyShellOutput object name added to handle output.

I had also hoped to thread the output so that it didn't display all of the output only after the command block was done executing, but ran into deadlocks when trying to access Construct objects from a thread. I scrapped that, as it's not of much consequence.

I set the window/layout size to 640 x 480, but it's easy enough to change to one's preference. I decided against making window resizing possible, as I couldn't get it to work to my satisfaction.

The screen shot and attachment are updated in the original post for this version.
B
3
S
2
G
2
Posts: 187
Reputation: 1,449

Post » Mon May 03, 2010 12:47 am

Oops. After using this more, I found a bug in the way I handled detection of the 'enter' key, which also affected command editing around newlines. I also decided to add a few minor changes along with that fix.

- Fixed the bug with 'enter' key handling, and cleaned up some unnecessary code along with it.

- Added a control to reset the shell (control - delete.) This returns the shell to it's initial state, but leaves the Python environment the same. Any classes/functions/variables assigned will remain.

- The shell now loses focus when a command is executing. This serves as a visual cue, and prevents extraneous input. It also regains focus automatically after exceptions are raised.

- The first entry in the command history is now an empty string, to serve as an indicator that the beginning of the history has been reached when cycling through it. Also, other empty and exact duplicate commands are now discarded instead of being added to the command history.

I can't seem to break it now, so hopefully that will do it. :)
B
3
S
2
G
2
Posts: 187
Reputation: 1,449


Return to Your tutorials & example files

Who is online

Users browsing this forum: No registered users and 0 guests