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