##// END OF EJS Templates
%clear magic added. fix CachedOutput.flush to flush data properly
vivainio -
Show More
@@ -0,0 +1,38 b''
1 # -*- coding: utf-8 -*-
2 """ IPython extension: add %clear magic """
3
4 import IPython.ipapi
5 ip = IPython.ipapi.get()
6
7 def clear_list(l):
8 while l:
9 l.pop()
10
11 def clear_f(self,arg):
12 """ Clear various data (e.g. stored history data)
13
14 %clear out - clear output history
15 %clear in - clear input history
16 """
17
18 api = self.getapi()
19 for target in arg.split():
20 if target == 'out':
21 print "Flushing output cache (%d entries)" % len(api.user_ns()['_oh'])
22 self.outputcache.flush()
23 elif target == 'in':
24 print "Flushing input history"
25 from IPython import iplib
26 clear_list(self.input_hist)
27 clear_list(self.input_hist_raw)
28 for n in range(1,self.outputcache.prompt_count + 1):
29 key = '_i'+`n`
30 try:
31 del self.user_ns[key]
32 except: pass
33
34 ip.expose_magic("clear",clear_f)
35
36
37
38
@@ -18,7 +18,7 b' import sys'
18 import ext_rehashdir # %rehashdir magic
18 import ext_rehashdir # %rehashdir magic
19 import ext_rescapture # var = !ls and var = %magic
19 import ext_rescapture # var = !ls and var = %magic
20 import pspersistence # %store magic
20 import pspersistence # %store magic
21
21 import clearcmd # %clear
22 # Basic readline config
22 # Basic readline config
23
23
24 o = ip.options() No newline at end of file
24 o = ip.options()
@@ -2,7 +2,7 b''
2 """
2 """
3 Classes for handling input/output prompts.
3 Classes for handling input/output prompts.
4
4
5 $Id: Prompts.py 1076 2006-01-24 17:27:05Z vivainio $"""
5 $Id: Prompts.py 1261 2006-04-11 14:37:02Z vivainio $"""
6
6
7 #*****************************************************************************
7 #*****************************************************************************
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
8 # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu>
@@ -439,7 +439,6 b' class CachedOutput:'
439 # other more normal stuff
439 # other more normal stuff
440 # b/c each call to the In[] prompt raises it by 1, even the first.
440 # b/c each call to the In[] prompt raises it by 1, even the first.
441 self.prompt_count = 0
441 self.prompt_count = 0
442 self.cache_count = 1
443 # Store the last prompt string each time, we need it for aligning
442 # Store the last prompt string each time, we need it for aligning
444 # continuation and auto-rewrite prompts
443 # continuation and auto-rewrite prompts
445 self.last_prompt = ''
444 self.last_prompt = ''
@@ -545,7 +544,13 b' class CachedOutput:'
545
544
546 def update(self,arg):
545 def update(self,arg):
547 #print '***cache_count', self.cache_count # dbg
546 #print '***cache_count', self.cache_count # dbg
548 if self.cache_count >= self.cache_size and self.do_full_cache:
547 if len(self.user_ns['_oh']) >= self.cache_size and self.do_full_cache:
548 warn('Output cache limit (currently '+\
549 `self.cache_count`+' entries) hit.\n'
550 'Flushing cache and resetting history counter...\n'
551 'The only history variables available will be _,__,___ and _1\n'
552 'with the current result.')
553
549 self.flush()
554 self.flush()
550 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
555 # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise
551 # we cause buggy behavior for things like gettext).
556 # we cause buggy behavior for things like gettext).
@@ -558,7 +563,6 b' class CachedOutput:'
558 # hackish access to top-level namespace to create _1,_2... dynamically
563 # hackish access to top-level namespace to create _1,_2... dynamically
559 to_main = {}
564 to_main = {}
560 if self.do_full_cache:
565 if self.do_full_cache:
561 self.cache_count += 1
562 self.entries.append(arg)
566 self.entries.append(arg)
563 new_result = '_'+`self.prompt_count`
567 new_result = '_'+`self.prompt_count`
564 to_main[new_result] = self.entries[-1]
568 to_main[new_result] = self.entries[-1]
@@ -569,16 +573,16 b' class CachedOutput:'
569 if not self.do_full_cache:
573 if not self.do_full_cache:
570 raise ValueError,"You shouldn't have reached the cache flush "\
574 raise ValueError,"You shouldn't have reached the cache flush "\
571 "if full caching is not enabled!"
575 "if full caching is not enabled!"
572 warn('Output cache limit (currently '+\
573 `self.cache_count`+' entries) hit.\n'
574 'Flushing cache and resetting history counter...\n'
575 'The only history variables available will be _,__,___ and _1\n'
576 'with the current result.')
577 # delete auto-generated vars from global namespace
576 # delete auto-generated vars from global namespace
577
578 for n in range(1,self.prompt_count + 1):
578 for n in range(1,self.prompt_count + 1):
579 key = '_'+`n`
579 key = '_'+`n`
580 try:
580 try:
581 del self.user_ns[key]
581 del self.user_ns[key]
582 except: pass
582 except: pass
583 self.prompt_count = 1
583 self.user_ns['_oh'].clear()
584 self.cache_count = 1
584
585 if '_' not in __builtin__.__dict__:
586 self.user_ns.update({'_':None,'__':None, '___':None})
587 import gc
588 gc.collect() # xxx needed?
@@ -3,6 +3,11 b''
3 * iplib.py, ipmaker.py: .ipy extension now means "ipython batch file"
3 * iplib.py, ipmaker.py: .ipy extension now means "ipython batch file"
4 in command line. E.g. "ipython test.ipy" runs test.ipy with ipython
4 in command line. E.g. "ipython test.ipy" runs test.ipy with ipython
5 prefilters, allowing stuff like magics and aliases in the file.
5 prefilters, allowing stuff like magics and aliases in the file.
6
7 * Prompts.py, Extensions/clearcmd.py, ipy_system_conf.py: %clear magic
8 added. Supported now are "%clear in" and "%clear out" (clear input and
9 output history, respectively). Also fixed CachedOutput.flush to
10 properly flush the output cache.
6
11
7 2006-03-28 Ville Vainio <vivainio@gmail.com>
12 2006-03-28 Ville Vainio <vivainio@gmail.com>
8
13
General Comments 0
You need to be logged in to leave comments. Login now