##// END OF EJS Templates
Jorgen's %clean array
vivainio -
Show More
@@ -1,35 +1,46 b''
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %clear magic """
3
4 import IPython.ipapi
5 ip = IPython.ipapi.get()
6
7
8 def clear_f(self,arg):
9 """ Clear various data (e.g. stored history data)
10
11 %clear out - clear output history
12 %clear in - clear input history
13 """
14
15 api = self.getapi()
16 for target in arg.split():
17 if target == 'out':
18 print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh'])
19 self.outputcache.flush()
20 elif target == 'in':
21 print "Flushing input history"
22 from IPython import iplib
23 del self.input_hist[:]
24 del self.input_hist_raw[:]
25 for n in range(1,self.outputcache.prompt_count + 1):
26 key = '_i'+`n`
27 try:
28 del self.user_ns[key]
29 except: pass
30
31 ip.expose_magic("clear",clear_f)
32
33
34
35
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %clear magic """
3
4 import IPython.ipapi
5 import gc
6 ip = IPython.ipapi.get()
7
8
9 def clear_f(self,arg):
10 """ Clear various data (e.g. stored history data)
11
12 %clear out - clear output history
13 %clear in - clear input history
14 """
15
16 api = self.getapi()
17 for target in arg.split():
18 if target == 'out':
19 print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh'])
20 self.outputcache.flush()
21 elif target == 'in':
22 print "Flushing input history"
23 from IPython import iplib
24 del self.input_hist[:]
25 del self.input_hist_raw[:]
26 for n in range(1,self.outputcache.prompt_count + 1):
27 key = '_i'+`n`
28 try:
29 del self.user_ns[key]
30 except: pass
31 elif target == 'array':
32 try:
33 pylab=ip.IP.pylab
34 for x in self.user_ns.keys():
35 if isinstance(self.user_ns[x],pylab.arraytype):
36 del self.user_ns[x]
37 except AttributeError:
38 print "Clear array only available in -pylab mode"
39 gc.collect()
40
41
42 ip.expose_magic("clear",clear_f)
43
44
45
46
@@ -2,7 +2,7 b''
2 2 """
3 3 Classes for handling input/output prompts.
4 4
5 $Id: Prompts.py 1261 2006-04-11 14:37:02Z vivainio $"""
5 $Id: Prompts.py 1264 2006-04-11 19:24:29Z vivainio $"""
6 6
7 7 #*****************************************************************************
8 8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
@@ -442,7 +442,6 b' class CachedOutput:'
442 442 # Store the last prompt string each time, we need it for aligning
443 443 # continuation and auto-rewrite prompts
444 444 self.last_prompt = ''
445 self.entries = [None] # output counter starts at 1 for the user
446 445 self.Pprint = Pprint
447 446 self.output_sep = output_sep
448 447 self.output_sep2 = output_sep2
@@ -563,9 +562,8 b' class CachedOutput:'
563 562 # hackish access to top-level namespace to create _1,_2... dynamically
564 563 to_main = {}
565 564 if self.do_full_cache:
566 self.entries.append(arg)
567 565 new_result = '_'+`self.prompt_count`
568 to_main[new_result] = self.entries[-1]
566 to_main[new_result] = arg
569 567 self.user_ns.update(to_main)
570 568 self.user_ns['_oh'][self.prompt_count] = arg
571 569
General Comments 0
You need to be logged in to leave comments. Login now