Show More
@@ -1,47 +1,56 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 | """ IPython extension: add %clear magic """ |
|
2 | """ IPython extension: add %clear magic """ | |
3 |
|
3 | |||
4 | import IPython.ipapi |
|
4 | import IPython.ipapi | |
5 | import gc |
|
5 | import gc | |
6 | ip = IPython.ipapi.get() |
|
6 | ip = IPython.ipapi.get() | |
7 |
|
7 | |||
8 |
|
8 | |||
9 | def clear_f(self,arg): |
|
9 | def clear_f(self,arg): | |
10 | """ Clear various data (e.g. stored history data) |
|
10 | """ Clear various data (e.g. stored history data) | |
11 |
|
11 | |||
12 | %clear out - clear output history |
|
12 | %clear out - clear output history | |
13 | %clear in - clear input history |
|
13 | %clear in - clear input history | |
14 | """ |
|
14 | """ | |
15 |
|
15 | |||
16 | api = self.getapi() |
|
16 | api = self.getapi() | |
17 | for target in arg.split(): |
|
17 | for target in arg.split(): | |
18 | if target == 'out': |
|
18 | if target == 'out': | |
19 | print "Flushing output cache (%d entries)" % len(api.user_ns['_oh']) |
|
19 | print "Flushing output cache (%d entries)" % len(api.user_ns['_oh']) | |
20 | self.outputcache.flush() |
|
20 | self.outputcache.flush() | |
21 | elif target == 'in': |
|
21 | elif target == 'in': | |
22 | print "Flushing input history" |
|
22 | print "Flushing input history" | |
23 | from IPython import iplib |
|
23 | from IPython import iplib | |
24 | del self.input_hist[:] |
|
24 | del self.input_hist[:] | |
25 | del self.input_hist_raw[:] |
|
25 | del self.input_hist_raw[:] | |
26 | for n in range(1,self.outputcache.prompt_count + 1): |
|
26 | for n in range(1,self.outputcache.prompt_count + 1): | |
27 | key = '_i'+`n` |
|
27 | key = '_i'+`n` | |
28 | try: |
|
28 | try: | |
29 | del self.user_ns[key] |
|
29 | del self.user_ns[key] | |
30 | except: pass |
|
30 | except: pass | |
31 | elif target == 'array': |
|
31 | elif target == 'array': | |
32 | try: |
|
32 | try: | |
33 | pylab=ip.IP.pylab |
|
33 | pylab=ip.IP.pylab | |
34 | for x in self.user_ns.keys(): |
|
34 | for x in self.user_ns.keys(): | |
35 | if isinstance(self.user_ns[x],pylab.arraytype): |
|
35 | if isinstance(self.user_ns[x],pylab.arraytype): | |
36 | del self.user_ns[x] |
|
36 | del self.user_ns[x] | |
37 | except AttributeError: |
|
37 | except AttributeError: | |
38 | print "Clear array only available in -pylab mode" |
|
38 | print "Clear array only available in -pylab mode" | |
39 | gc.collect() |
|
39 | gc.collect() | |
40 | elif target == 'shadow': |
|
40 | ||
|
41 | elif target == 'shadow_compress': | |||
|
42 | print "Compressing shadow history" | |||
41 | api.db.hcompress('shadowhist') |
|
43 | api.db.hcompress('shadowhist') | |
42 |
|
44 | |||
|
45 | elif target == 'shadow_nuke': | |||
|
46 | print "Erased all keys from shadow history " | |||
|
47 | for k in ip.db.keys('shadowhist/*'): | |||
|
48 | del ip.db[k] | |||
|
49 | ||||
43 | ip.expose_magic("clear",clear_f) |
|
50 | ip.expose_magic("clear",clear_f) | |
|
51 | import ipy_completers | |||
|
52 | ipy_completers.quick_completer('clear','in out shadow_nuke shadow_compress') | |||
44 |
|
53 | |||
45 |
|
54 | |||
46 |
|
55 | |||
47 |
|
56 |
General Comments 0
You need to be logged in to leave comments.
Login now