##// END OF EJS Templates
Work to address the review comments on Fernando's branch....
Work to address the review comments on Fernando's branch. * Added comment about Magic(object) (r1224) * Moved InteractiveTB.set_mode from IPythonApp -> InteractiveShell (r1229) * Moved pylabtools.py to IPython/lib (r1229) * Cleaned up comments and copyrights in testing (r1233) * Added comment about ip.shell._ofind (r1237) * Removed "Bye." from quitter (r1240). * Refactored and removed :mod:`IPython.utils.genutils` and :mod:`IPython.utils.platutils`. These modules have been replaced by topical focused modules in :mod:`IPython.utils`. * Refactored tests in :mod:`IPython.utils.tests`. * Moved :func:`IPython.testing.tools.temp_pyfile` to :mod:`IPython.utils.io`. * Moved :func:`IPython.testing.tools.cmd2argv` to :func:`IPython.testing.tools.pycmd2argv` and documented the fact that this only works with Python based command line programs. * Created a new :func:`IPython.utils.path.get_ipython_module_path` to use in finding paths to IPython modules.

File last commit:

r2266:eda4ef85
r2498:3eae1372
Show More
clearcmd.py
87 lines | 2.6 KiB | text/x-python | PythonLexer
# -*- coding: utf-8 -*-
""" IPython extension: add %clear magic """
from IPython.core import ipapi
import gc
ip = ipapi.get()
def clear_f(self,arg):
""" Clear various data (e.g. stored history data)
%clear in - clear input history
%clear out - clear output history
%clear shadow_compress - Compresses shadow history (to speed up ipython)
%clear shadow_nuke - permanently erase all entries in shadow history
%clear dhist - clear dir history
%clear array - clear only variables that are NumPy arrays
Examples:
In [1]: clear in
Flushing input history
In [2]: clear shadow_compress
Compressing shadow history
In [3]: clear shadow_nuke
Erased all keys from shadow history
In [4]: clear dhist
Clearing directory history
"""
api = self.getapi()
user_ns = self.user_ns # local lookup, heavily used
for target in arg.split():
if target == 'out':
print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
self.outputcache.flush()
elif target == 'in':
print "Flushing input history"
pc = self.outputcache.prompt_count + 1
for n in range(1, pc):
key = '_i'+`n`
user_ns.pop(key,None)
try:
del user_ns[key]
except: pass
# must be done in-place
self.input_hist[:] = ['\n'] * pc
self.input_hist_raw[:] = ['\n'] * pc
elif target == 'array':
# Support cleaning up numpy arrays
try:
from numpy import ndarray
# This must be done with items and not iteritems because we're
# going to modify the dict in-place.
for x,val in user_ns.items():
if isinstance(val,ndarray):
del user_ns[x]
except AttributeError:
print "Clear array only works if Numpy is available."
elif target == 'shadow_compress':
print "Compressing shadow history"
api.db.hcompress('shadowhist')
elif target == 'shadow_nuke':
print "Erased all keys from shadow history "
for k in ip.db.keys('shadowhist/*'):
del ip.db[k]
elif target == 'dhist':
print "Clearing directory history"
del user_ns['_dh'][:]
gc.collect()
# Activate the extension
ip.define_magic("clear",clear_f)
import ipy_completers
ipy_completers.quick_completer(
'%clear','in out shadow_nuke shadow_compress dhist')