##// END OF EJS Templates
making %clear a native magic
Paul Ivanov -
Show More
@@ -181,7 +181,6 b' def quick_completer(cmd, completions):'
181
181
182 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
182 get_ipython().set_hook('complete_command',do_complete, str_key = cmd)
183
183
184
185 def module_completion(line):
184 def module_completion(line):
186 """
185 """
187 Returns a list containing the completion possibilities for an import line.
186 Returns a list containing the completion possibilities for an import line.
@@ -316,3 +315,7 b' def cd_completer(self, event):'
316 raise TryNext
315 raise TryNext
317
316
318 return [compress_user(p, tilde_expand, tilde_val) for p in found]
317 return [compress_user(p, tilde_expand, tilde_val) for p in found]
318
319 def clear_completer(self, event):
320 "A completer for %clear magic"
321 return 'in out array dhist'.split()
@@ -1840,7 +1840,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
1840 """
1840 """
1841 from IPython.core.completer import IPCompleter
1841 from IPython.core.completer import IPCompleter
1842 from IPython.core.completerlib import (module_completer,
1842 from IPython.core.completerlib import (module_completer,
1843 magic_run_completer, cd_completer)
1843 magic_run_completer, cd_completer, clear_completer)
1844
1844
1845 self.Completer = IPCompleter(shell=self,
1845 self.Completer = IPCompleter(shell=self,
1846 namespace=self.user_ns,
1846 namespace=self.user_ns,
@@ -1860,6 +1860,7 b' class InteractiveShell(SingletonConfigurable, Magic):'
1860 self.set_hook('complete_command', module_completer, str_key = 'from')
1860 self.set_hook('complete_command', module_completer, str_key = 'from')
1861 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1861 self.set_hook('complete_command', magic_run_completer, str_key = '%run')
1862 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1862 self.set_hook('complete_command', cd_completer, str_key = '%cd')
1863 self.set_hook('complete_command', clear_completer, str_key = '%clear')
1863
1864
1864 # Only configure readline if we truly are using readline. IPython can
1865 # Only configure readline if we truly are using readline. IPython can
1865 # do tab-completion over the network, in GUIs, etc, where readline
1866 # do tab-completion over the network, in GUIs, etc, where readline
@@ -25,6 +25,7 b' import sys'
25 import shutil
25 import shutil
26 import re
26 import re
27 import time
27 import time
28 import gc
28 from StringIO import StringIO
29 from StringIO import StringIO
29 from getopt import getopt,GetoptError
30 from getopt import getopt,GetoptError
30 from pprint import pformat
31 from pprint import pformat
@@ -3697,4 +3698,60 b' Defaulting color scheme to \'NoColor\'"""'
3697 except Exception as e:
3698 except Exception as e:
3698 error(e)
3699 error(e)
3699
3700
3701 def magic_clear(self, s):
3702 """Clear various data (e.g. stored history data)
3703
3704 %clear in - clear input history
3705 %clear out - clear output history
3706 %clear dhist - clear dir history
3707 %clear array - clear only variables that are NumPy arrays
3708
3709 Examples
3710 --------
3711 ::
3712
3713 In [1]: clear in
3714 Flushing input history
3715
3716 In [2]: clear dhist
3717 Clearing directory history
3718 """
3719 ip = self.shell
3720 user_ns = self.user_ns # local lookup, heavily used
3721
3722 for target in s.split():
3723 if target == 'out':
3724 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
3725 self.displayhook.flush()
3726
3727 elif target == 'in':
3728 print "Flushing input history"
3729 pc = self.displayhook.prompt_count + 1
3730 for n in range(1, pc):
3731 key = '_i'+repr(n)
3732 user_ns.pop(key,None)
3733 user_ns.update(dict(_i=u'',_ii=u'',_iii=u''))
3734 # don't delete these, as %save and %macro depending on the length
3735 # of these lists to be preserved
3736 self.history_manager.input_hist_parsed[:] = [''] * pc
3737 self.history_manager.input_hist_raw[:] = [''] * pc
3738
3739 elif target == 'array':
3740 # Support cleaning up numpy arrays
3741 try:
3742 from numpy import ndarray
3743 # This must be done with items and not iteritems because we're
3744 # going to modify the dict in-place.
3745 for x,val in user_ns.items():
3746 if isinstance(val,ndarray):
3747 del user_ns[x]
3748 except ImportError:
3749 print "Clear array only works if Numpy is available."
3750
3751 elif target == 'dhist':
3752 print "Clearing directory history"
3753 del user_ns['_dh'][:]
3754
3755 gc.collect()
3756
3700 # end Magic
3757 # end Magic
@@ -187,7 +187,6 b' def test_macro_run():'
187 @dec.skipif_not_numpy
187 @dec.skipif_not_numpy
188 def test_numpy_clear_array_undec():
188 def test_numpy_clear_array_undec():
189 "Test '%clear array' functionality"
189 "Test '%clear array' functionality"
190 _ip.magic("load_ext clearcmd")
191 _ip.ex('import numpy as np')
190 _ip.ex('import numpy as np')
192 _ip.ex('a = np.empty(2)')
191 _ip.ex('a = np.empty(2)')
193 yield (nt.assert_true, 'a' in _ip.user_ns)
192 yield (nt.assert_true, 'a' in _ip.user_ns)
@@ -197,7 +196,6 b' def test_numpy_clear_array_undec():'
197 def test_clear():
196 def test_clear():
198 "Test '%clear' magic provided by IPython.extensions.clearcmd"
197 "Test '%clear' magic provided by IPython.extensions.clearcmd"
199 _ip = get_ipython()
198 _ip = get_ipython()
200 _ip.magic("load_ext clearcmd")
201 _ip.run_cell("parrot = 'dead'", store_history=True)
199 _ip.run_cell("parrot = 'dead'", store_history=True)
202 # test '%clear out', make an Out prompt
200 # test '%clear out', make an Out prompt
203 _ip.run_cell("parrot", store_history=True)
201 _ip.run_cell("parrot", store_history=True)
@@ -116,8 +116,6 b" def jot_obj(self, obj, name, comment=''):"
116
116
117 uname = 'jot/'+name+suffix
117 uname = 'jot/'+name+suffix
118
118
119 # which one works better?
120 #all = ip.shadowhist.all()
121 all = ip.shell.history_manager.input_hist_parsed
119 all = ip.shell.history_manager.input_hist_parsed
122
120
123 # We may actually want to make snapshot of files that are run-ned.
121 # We may actually want to make snapshot of files that are run-ned.
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now