diff --git a/IPython/core/completerlib.py b/IPython/core/completerlib.py index 7717039..7db6bef 100644 --- a/IPython/core/completerlib.py +++ b/IPython/core/completerlib.py @@ -181,7 +181,6 @@ def quick_completer(cmd, completions): get_ipython().set_hook('complete_command',do_complete, str_key = cmd) - def module_completion(line): """ Returns a list containing the completion possibilities for an import line. @@ -316,3 +315,7 @@ def cd_completer(self, event): raise TryNext return [compress_user(p, tilde_expand, tilde_val) for p in found] + +def clear_completer(self, event): + "A completer for %clear magic" + return 'in out array dhist'.split() diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index f3fa1ee..14e1386 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -1840,7 +1840,7 @@ class InteractiveShell(SingletonConfigurable, Magic): """ from IPython.core.completer import IPCompleter from IPython.core.completerlib import (module_completer, - magic_run_completer, cd_completer) + magic_run_completer, cd_completer, clear_completer) self.Completer = IPCompleter(shell=self, namespace=self.user_ns, @@ -1860,6 +1860,7 @@ class InteractiveShell(SingletonConfigurable, Magic): self.set_hook('complete_command', module_completer, str_key = 'from') self.set_hook('complete_command', magic_run_completer, str_key = '%run') self.set_hook('complete_command', cd_completer, str_key = '%cd') + self.set_hook('complete_command', clear_completer, str_key = '%clear') # Only configure readline if we truly are using readline. IPython can # do tab-completion over the network, in GUIs, etc, where readline diff --git a/IPython/core/magic.py b/IPython/core/magic.py index 950a1e5..232c8f4 100644 --- a/IPython/core/magic.py +++ b/IPython/core/magic.py @@ -25,6 +25,7 @@ import sys import shutil import re import time +import gc from StringIO import StringIO from getopt import getopt,GetoptError from pprint import pformat @@ -3697,4 +3698,60 @@ Defaulting color scheme to 'NoColor'""" except Exception as e: error(e) + def magic_clear(self, s): + """Clear various data (e.g. stored history data) + + %clear in - clear input history + %clear out - clear output 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 dhist + Clearing directory history + """ + ip = self.shell + user_ns = self.user_ns # local lookup, heavily used + + for target in s.split(): + if target == 'out': + print "Flushing output cache (%d entries)" % len(user_ns['_oh']) + self.displayhook.flush() + + elif target == 'in': + print "Flushing input history" + pc = self.displayhook.prompt_count + 1 + for n in range(1, pc): + key = '_i'+repr(n) + user_ns.pop(key,None) + user_ns.update(dict(_i=u'',_ii=u'',_iii=u'')) + # 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 + + 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 ImportError: + print "Clear array only works if Numpy is available." + + elif target == 'dhist': + print "Clearing directory history" + del user_ns['_dh'][:] + + gc.collect() + # end Magic diff --git a/IPython/core/tests/test_magic.py b/IPython/core/tests/test_magic.py index 77f1ca2..6b39444 100644 --- a/IPython/core/tests/test_magic.py +++ b/IPython/core/tests/test_magic.py @@ -187,7 +187,6 @@ def test_macro_run(): @dec.skipif_not_numpy def test_numpy_clear_array_undec(): "Test '%clear array' functionality" - _ip.magic("load_ext clearcmd") _ip.ex('import numpy as np') _ip.ex('a = np.empty(2)') yield (nt.assert_true, 'a' in _ip.user_ns) @@ -197,7 +196,6 @@ def test_numpy_clear_array_undec(): def test_clear(): "Test '%clear' magic provided by IPython.extensions.clearcmd" _ip = get_ipython() - _ip.magic("load_ext clearcmd") _ip.run_cell("parrot = 'dead'", store_history=True) # test '%clear out', make an Out prompt _ip.run_cell("parrot", store_history=True) diff --git a/IPython/extensions/clearcmd.py b/IPython/extensions/clearcmd.py deleted file mode 100644 index 6787ab7..0000000 --- a/IPython/extensions/clearcmd.py +++ /dev/null @@ -1,89 +0,0 @@ -# -*- coding: utf-8 -*- -""" IPython extension: add %clear magic """ - -import gc - -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 - """ - - ip = self.shell - 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.displayhook.flush() - - elif target == 'in': - print "Flushing input history" - pc = self.displayhook.prompt_count + 1 - for n in range(1, pc): - key = '_i'+repr(n) - user_ns.pop(key,None) - user_ns.update(dict(_i=u'',_ii=u'',_iii=u'')) - # 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 - - 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 ImportError: - print "Clear array only works if Numpy is available." - - elif target == 'shadow_compress': - print "Compressing shadow history" - ip.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() - -_loaded = False - -# Activate the extension -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') diff --git a/IPython/quarantine/ipy_jot.py b/IPython/quarantine/ipy_jot.py index 5a8fec3..fce9099 100644 --- a/IPython/quarantine/ipy_jot.py +++ b/IPython/quarantine/ipy_jot.py @@ -116,8 +116,6 @@ def jot_obj(self, obj, name, comment=''): uname = 'jot/'+name+suffix - # which one works better? - #all = ip.shadowhist.all() all = ip.shell.history_manager.input_hist_parsed # We may actually want to make snapshot of files that are run-ned.