Workspace Module for Python's Command Line Interpreter

A version of this workspace has been used twice in Hille Institute summer workshops for middle school teachers at the Oklahoma School of Science and Math. The lab sheet used at the 2007 workship is available here..

 

Workspace

Here's what the workspace module does for you:

Unlike with (at least some) IDEs, editing is not module oriented. This is an advantage when you are tinkering and a disadvantage when you are building something large.

This module requires version 2.4 or later of Python. The Tkinter module is required for full functionality. Python's Windows installer will arrange for both.

 

Download

Windows Installer (for my Python workspace, version 1.1)

Source Code (for those of you who want to see how it works)

Windows Installer (for Python, version 2.5.1)

Zip file of the 2007 version for K12 teachers. Handed out at the Hille Institute workshop for middle school teachers, held at the Oklahoma School of Science and Mathematics, June 2007. This is a complete distribution of Python 2.4 that has been modified to load the workspace module and to run from any (Windows) drive including a flash drive.

Copyright by J Adrian Zimmer, 2006,2007. Released under the Open Source Software license. Portions of K12Python are distributed under the Python Software Foundation License.

 

Saving and Loading

Saving the workspace means to place a memory of the current workspace in the current folder. Do it this way








>>> save()



>>>




What to do if you do not want to save all the variables in your current workspace? Delete the ones you do not want. For example,








>>> z = -1



>>> z



-1



>>> del z



>>> z



Traceback (most recent call last):



  File "<stdin>", line 1, in ?



NameError: name 'z' is not defined



>>>




How can you remember which variables are in the current workspace? If you are me, you cannot. So ask








>>> savable()







Global Variables: ['x','y']



Functions: []



Classes: []



Modules: []







>>>



In this case there are two savable variables, named “x” and “y”. There are no savable functions, modules, or classes. Here the rules for what is savable:

Variables

To be savable, a variable must refer to savable value. Numeric and string primitives are savable values. Other savable values are defined recursively: lists and tuples of savable values are savable; dicts are savable if their keys are primitives and their values are savable.

Functions

The only savable functions are those defined with edit().

Classes

The only savable classes are those defined with edit().

Modules

The only savable modules are those imported with import <module name>.


Now suppose it is tomorrow and you want to start where you left off. Assuming you entered the save() command before you closed the Python window, you can restart Python, import the workspace module, and then do this








>>> load()



Clear the existing workspace before loading? (answer y,



n, or ?) n







The following have been loaded from the unnamed_workspace workspace



(If you cleared the current workspace, the following is all there is.)







Global Variables: ['x','y']



Functions: []



Classes: []



Modules: []







>>>



After you enter load(), the loading mechanism asks you if you want to clear out the current workspace before proceeding. Since you haven't done anything in the current workspace, it doesn't matter how you answer. I answered with “n”. (All I entered in the above example were load() and n.)

The load mechanism continues by telling you that it is working with an unnamed workspace and by listing the things it has loaded. In this case, it is again the two variables named “x” an “y”. They will also have the values you gave them yesterday.


Both save() and load() can take an argument which names the workspace you are saving or loading. Without this argument, the file containing workspace information is “unnamed_workspace.pws” in the current directory. With the argument the file will be the one you named, but for an appended suffix of .pws.

 

Changing Directories

Enter “changeFolder()” and you will see

The popup window is a file chooser window. The current folder is highlighted in that window and you are given a chance to change it. Changing it will change the current directory your interpreter is working in.

You can also see which directory is current with changeFolder() because it always highlights the current directory as it pops up the file chooser window.

 

Editing Functions and Classes

Instead of typing def and class definitions directly into the command line interpreter, you can use the edit command. Execute








>>> edit()



What you will see is a new window something like this

Enter a single function or class definition into the top part of this window. The check button compiles it and shows messages in the bottom part. (If necessary, you can scroll with the arrow keys.)

Compiling from this window is required if you are to load your definition back into the Python interpreter. The window is modal, you cannot use the command line window until you are finished with it. (You can, however, use windows from other applications.)

Advantages:

To re-edit an existing definition of foo, enter







>>> edit(foo)



 

Enjoy!

Comments can be sent to me thru my home page jazimmer.net

— J Adrian Zimmer

Context: Teaching, Learning, and Practicing Computer Programming Author: J Adrian Zimmer

Revised: July 17, 2007