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