Clearing pythonwin environment

N

Network Ninja

I want to restart the environment occasionally to default (like I
restarted pythonwin). I wrote this script. I know why it doesn't work,
cause it deletes my variable (item) on each iteration. My question is:
is it possible to do this? What other things might I try?

# Reset Pythonwin enviroment

# Variables
keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']

print
for item in dir():
if item not in keep:
del item

print
print dir()
 
S

Steven Bethard

Network said:
I want to restart the environment occasionally to default (like I
restarted pythonwin). I wrote this script. I know why it doesn't work,
cause it deletes my variable (item) on each iteration. My question is:
is it possible to do this? What other things might I try?

# Reset Pythonwin enviroment

# Variables
keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']

print
for item in dir():
if item not in keep:
del item
^^^^^^^^
del globals()[item]

I believe that should work.

Note that this won't cause imported modules to be reloaded though, so I
wouldn't rely on it too heavily. The imported modules problem can only
really be solved by restarting the interpreter.

STeVe
 
S

Scott David Daniels

Network said:
I want to restart the environment occasionally to default (like I
restarted pythonwin). I wrote this script. I know why it doesn't work,
cause it deletes my variable (item) on each iteration. My question is:
is it possible to do this? What other things might I try?

keep = ['__builtins__', '__doc__', '__name__', 'pywin', 'keep', 'item']
for item in dir():
if item not in keep:
del item

keep = set(['__builtins__', '__doc__', '__name__',
'pywin', 'keep', 'item', 'globs']) # Faster to test
globs = globals()
for item in dir():
if item not in keep:
del globs[item]
del globs, item

Note that this leaves all imported modules imported.

--Scott David Daniels
(e-mail address removed)
 
N

Network Ninja

Scott said:
keep = set(['__builtins__', '__doc__', '__name__',
'pywin', 'keep', 'item', 'globs']) # Faster to test
globs = globals()
for item in dir():
if item not in keep:
del globs[item]
del globs, item

This did not work, it gave an error saying "NameError: name 'globs' is
not defined".

However, Steven's del globals()[item] did the trick perfectly. Thanks a
million for the replies, both of you.

So can you explain the [] after globals()? How does that work? The help
docs don't mention anything about that in the function.
 
S

Scott David Daniels

Network said:
Scott said:
keep = set(['__builtins__', '__doc__', '__name__',
'pywin', 'keep', 'item', 'globs']) # Faster to test
globs = globals()
for item in dir():
if item not in keep:
del globs[item]
del globs, item

This did not work, it gave an error saying "NameError: name 'globs' is
not defined".

I suspect that was because you did not include the last entry in the
"keep" set above.

By the way, another way to clean up:

def cleanup(keep=set(['__builtins__', '__doc__', '__name__',
'pywin', 'cleanup']):
globs = globals()
for item in set(globs) - keep:
del globs[item]

Then, simply call:

cleanup()
So can you explain the [] after globals()? How does that work?
globals() returns a dict (or dict-like object) that reflects and
access the current global environment. Imagine:

globs = dict(a=1, b='2', c='iii')
print globs
del globs['b']
print globs


--Scott David Daniels
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top