##// END OF EJS Templates
inoculate %clear magic from quarantine
Paul Ivanov -
Show More
@@ -1,87 +1,86 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %clear magic """
2 """ IPython extension: add %clear magic """
3
3
4 from IPython.core import ipapi
4 from IPython.core import ipapi
5 import gc
5 import gc
6 ip = ipapi.get()
6 ip = ipapi.get()
7
7
8 def clear_f(self,arg):
8 def clear_f(self,arg):
9 """ Clear various data (e.g. stored history data)
9 """ Clear various data (e.g. stored history data)
10
10
11 %clear in - clear input history
11 %clear in - clear input history
12 %clear out - clear output history
12 %clear out - clear output history
13 %clear shadow_compress - Compresses shadow history (to speed up ipython)
13 %clear shadow_compress - Compresses shadow history (to speed up ipython)
14 %clear shadow_nuke - permanently erase all entries in shadow history
14 %clear shadow_nuke - permanently erase all entries in shadow history
15 %clear dhist - clear dir history
15 %clear dhist - clear dir history
16 %clear array - clear only variables that are NumPy arrays
16 %clear array - clear only variables that are NumPy arrays
17
17
18 Examples:
18 Examples:
19
19
20 In [1]: clear in
20 In [1]: clear in
21 Flushing input history
21 Flushing input history
22
22
23 In [2]: clear shadow_compress
23 In [2]: clear shadow_compress
24 Compressing shadow history
24 Compressing shadow history
25
25
26 In [3]: clear shadow_nuke
26 In [3]: clear shadow_nuke
27 Erased all keys from shadow history
27 Erased all keys from shadow history
28
28
29 In [4]: clear dhist
29 In [4]: clear dhist
30 Clearing directory history
30 Clearing directory history
31 """
31 """
32
32
33 api = self.getapi()
33 api = self.get_ipython()
34 user_ns = self.user_ns # local lookup, heavily used
34 user_ns = self.user_ns # local lookup, heavily used
35
35
36
36
37 for target in arg.split():
37 for target in arg.split():
38
38
39 if target == 'out':
39 if target == 'out':
40 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
40 print "Flushing output cache (%d entries)" % len(user_ns['_oh'])
41 self.outputcache.flush()
41 self.displayhook.flush()
42
42
43 elif target == 'in':
43 elif target == 'in':
44 print "Flushing input history"
44 print "Flushing input history"
45 pc = self.outputcache.prompt_count + 1
45 pc = self.displayhook.prompt_count + 1
46 for n in range(1, pc):
46 for n in range(1, pc):
47 key = '_i'+`n`
47 key = '_i'+`n`
48 user_ns.pop(key,None)
48 user_ns.pop(key,None)
49 try:
49 try:
50 del user_ns[key]
50 del user_ns[key]
51 except: pass
51 except: pass
52 # must be done in-place
52 # must be done in-place
53 self.history_manager.input_hist_parsed[:] = ['\n'] * pc
53 self.history_manager.input_hist_parsed[:] = ['\n'] * pc
54 self.history_manager.input_hist_raw[:] = ['\n'] * pc
54 self.history_manager.input_hist_raw[:] = ['\n'] * pc
55
55
56 elif target == 'array':
56 elif target == 'array':
57 # Support cleaning up numpy arrays
57 # Support cleaning up numpy arrays
58 try:
58 try:
59 from numpy import ndarray
59 from numpy import ndarray
60 # This must be done with items and not iteritems because we're
60 # This must be done with items and not iteritems because we're
61 # going to modify the dict in-place.
61 # going to modify the dict in-place.
62 for x,val in user_ns.items():
62 for x,val in user_ns.items():
63 if isinstance(val,ndarray):
63 if isinstance(val,ndarray):
64 del user_ns[x]
64 del user_ns[x]
65 except AttributeError:
65 except AttributeError:
66 print "Clear array only works if Numpy is available."
66 print "Clear array only works if Numpy is available."
67
67
68 elif target == 'shadow_compress':
68 elif target == 'shadow_compress':
69 print "Compressing shadow history"
69 print "Compressing shadow history"
70 api.db.hcompress('shadowhist')
70 api.db.hcompress('shadowhist')
71
71
72 elif target == 'shadow_nuke':
72 elif target == 'shadow_nuke':
73 print "Erased all keys from shadow history "
73 print "Erased all keys from shadow history "
74 for k in ip.db.keys('shadowhist/*'):
74 for k in ip.db.keys('shadowhist/*'):
75 del ip.db[k]
75 del ip.db[k]
76
76
77 elif target == 'dhist':
77 elif target == 'dhist':
78 print "Clearing directory history"
78 print "Clearing directory history"
79 del user_ns['_dh'][:]
79 del user_ns['_dh'][:]
80
80
81 gc.collect()
81 gc.collect()
82
82
83 # Activate the extension
83 # Activate the extension
84 ip.define_magic("clear",clear_f)
84 ip.define_magic("clear",clear_f)
85 import ipy_completers
85 from IPython.core.completerlib import quick_completer
86 ipy_completers.quick_completer(
86 quick_completer( '%clear','in out shadow_nuke shadow_compress dhist')
87 '%clear','in out shadow_nuke shadow_compress dhist')
General Comments 0
You need to be logged in to leave comments. Login now