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