##// END OF EJS Templates
clear out the contents of but keep length of In[]
clear out the contents of but keep length of In[]

File last commit:

r5957:cbc53d6f
r5957:cbc53d6f
Show More
clearcmd.py
89 lines | 2.8 KiB | text/x-python | PythonLexer
vivainio
Jorgen's %clean array
r245 # -*- coding: utf-8 -*-
""" IPython extension: add %clear magic """
import gc
def clear_f(self,arg):
""" Clear various data (e.g. stored history data)
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
Jorgen's %clean array
r245 %clear in - clear input history
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 %clear out - clear output history
vivainio
doc, changelog fixes
r791 %clear shadow_compress - Compresses shadow history (to speed up ipython)
%clear shadow_nuke - permanently erase all entries in shadow history
vivainio
cd -TAB pads zeros, %clear dhist, some doc changes
r839 %clear dhist - clear dir history
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 %clear array - clear only variables that are NumPy arrays
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 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
vivainio
Jorgen's %clean array
r245 """
Bernardo B. Marques
remove all trailling spaces
r4872
Paul Ivanov
new way of loading extensions, thanks @takluyver
r5956 ip = self.shell
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 user_ns = self.user_ns # local lookup, heavily used
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
Jorgen's %clean array
r245 for target in arg.split():
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844
vivainio
Jorgen's %clean array
r245 if target == 'out':
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
Paul Ivanov
inoculate %clear magic from quarantine
r5938 self.displayhook.flush()
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844
vivainio
Jorgen's %clean array
r245 elif target == 'in':
print "Flushing input history"
Paul Ivanov
inoculate %clear magic from quarantine
r5938 pc = self.displayhook.prompt_count + 1
vivainio
%clear in now properly pads the history with empty entries
r795 for n in range(1, pc):
Paul Ivanov
%clear magic now fully restored...
r5940 key = '_i'+repr(n)
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 user_ns.pop(key,None)
Paul Ivanov
%clear magic now fully restored...
r5940 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
Paul Ivanov
clear out the contents of but keep length of In[]
r5957 # don't delete these, as %save and %macro depending on the length
# of these lists to be preserved
self.history_manager.input_hist_parsed[:] = [''] * pc
self.history_manager.input_hist_raw[:] = [''] * pc
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844
vivainio
Jorgen's %clean array
r245 elif target == 'array':
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 # Support cleaning up numpy arrays
vivainio
Jorgen's %clean array
r245 try:
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 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]
Paul Ivanov
%clear magic now fully restored...
r5940 except ImportError:
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 print "Clear array only works if Numpy is available."
vivainio
completers for %clear magic, add shadow history deletion
r790
elif target == 'shadow_compress':
print "Compressing shadow history"
Paul Ivanov
new way of loading extensions, thanks @takluyver
r5956 ip.db.hcompress('shadowhist')
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
completers for %clear magic, add shadow history deletion
r790 elif target == 'shadow_nuke':
print "Erased all keys from shadow history "
for k in ip.db.keys('shadowhist/*'):
del ip.db[k]
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844
vivainio
cd -TAB pads zeros, %clear dhist, some doc changes
r839 elif target == 'dhist':
print "Clearing directory history"
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 del user_ns['_dh'][:]
vivainio
cd -TAB pads zeros, %clear dhist, some doc changes
r839
Bernardo B. Marques
remove all trailling spaces
r4872 gc.collect()
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844
Paul Ivanov
new way of loading extensions, thanks @takluyver
r5956 _loaded = False
Fernando Perez
Add tests and fixes to clearcmd extension, when using numpy.
r1844 # Activate the extension
Paul Ivanov
new way of loading extensions, thanks @takluyver
r5956 def load_ipython_extension(ip):
"""Load the extension in IPython."""
global _loaded
if not _loaded:
ip.define_magic("clear",clear_f)
from IPython.core.completerlib import quick_completer
quick_completer('%clear','in out array shadow_nuke shadow_compress dhist')