[Tutor] reset and IDE

Christopher Smith csmith@blakeschool.org
Fri, 01 Feb 2002 17:04:40 -0600


Now that I have started teaching Python in school, I can see that I would
really like it if the IDE would automatically flush out the namespace so
that all functions and modules would be reloaded when a script is re-run. 
A "reset" script was offered during the 2001 summer by Jesse W  on the
tutor list and I use it regularly myself but would love to have this be
the automatic behavior when the "Run All" button is pressed in the IDE (on
the Mac).  Here is the script:

def reset(namespace, user_safe_items=()):
	"""Reset given namespace to a "pristine" state.

	This is a widely requested function which will remove from the
	current namespace everything execpt the items listed in
	below."""
	safe_items=['__builtins__', '__doc__', '__name__', 'reset']\
		    +list(user_safe_items)
	for item in namespace.keys():
		if item not in safe_items:
			exec ("del "+item) in namespace

When running this from a script I just type "reset(vars())" at the top of
the program to avert having functions that I have changed not be read as
changed by Python.

A couple questions here:

1) If I wanted to build a copy of the IDE with this as the default
behavior, where do I have to insert this code?  (I was unable to find
where a press on the "Run All" button takes you in terms of program flow.)

2) Instead of wiping out the whole namespace, might another productive
approach be to keep track of dirty modules and reload them.  But hmm...if
someone were using a different editor to edit the files then there would
be no way of knowing that those modules had changed.  But if someone only
used the IDE for development, then this would be feasible.  Every time a
module is opened and edited, its name would be added to the list of dirty
modules.  Then, when a script is to be executed, the names of all dirty
modules would be deleted from that script's namespace if they exist *and
then* normally running of the script would occur.  Something like:

RunAll is pressed on script with namespace = "foo"
for item in dirty:
	exec ("del "+item) in foo
Do normal action that RunAll would do

Is this feasible somewhere in the Python code for the IDE?

/c