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 | 18 | import ext_rehashdir # %rehashdir magic |
|
19 | 19 | import ext_rescapture # var = !ls and var = %magic |
|
20 | 20 | import pspersistence # %store magic |
|
21 | ||
|
21 | import clearcmd # %clear | |
|
22 | 22 | # Basic readline config |
|
23 | 23 | |
|
24 | 24 | o = ip.options() No newline at end of file |
@@ -2,7 +2,7 b'' | |||
|
2 | 2 | """ |
|
3 | 3 | Classes for handling input/output prompts. |
|
4 | 4 | |
|
5 |
$Id: Prompts.py 1 |
|
|
5 | $Id: Prompts.py 1261 2006-04-11 14:37:02Z vivainio $""" | |
|
6 | 6 | |
|
7 | 7 | #***************************************************************************** |
|
8 | 8 | # Copyright (C) 2001-2006 Fernando Perez <fperez@colorado.edu> |
@@ -439,7 +439,6 b' class CachedOutput:' | |||
|
439 | 439 | # other more normal stuff |
|
440 | 440 | # b/c each call to the In[] prompt raises it by 1, even the first. |
|
441 | 441 | self.prompt_count = 0 |
|
442 | self.cache_count = 1 | |
|
443 | 442 | # Store the last prompt string each time, we need it for aligning |
|
444 | 443 | # continuation and auto-rewrite prompts |
|
445 | 444 | self.last_prompt = '' |
@@ -545,7 +544,13 b' class CachedOutput:' | |||
|
545 | 544 | |
|
546 | 545 | def update(self,arg): |
|
547 | 546 | #print '***cache_count', self.cache_count # dbg |
|
548 |
if self. |
|
|
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 | 554 | self.flush() |
|
550 | 555 | # Don't overwrite '_' and friends if '_' is in __builtin__ (otherwise |
|
551 | 556 | # we cause buggy behavior for things like gettext). |
@@ -558,7 +563,6 b' class CachedOutput:' | |||
|
558 | 563 | # hackish access to top-level namespace to create _1,_2... dynamically |
|
559 | 564 | to_main = {} |
|
560 | 565 | if self.do_full_cache: |
|
561 | self.cache_count += 1 | |
|
562 | 566 | self.entries.append(arg) |
|
563 | 567 | new_result = '_'+`self.prompt_count` |
|
564 | 568 | to_main[new_result] = self.entries[-1] |
@@ -569,16 +573,16 b' class CachedOutput:' | |||
|
569 | 573 | if not self.do_full_cache: |
|
570 | 574 | raise ValueError,"You shouldn't have reached the cache flush "\ |
|
571 | 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 | 576 | # delete auto-generated vars from global namespace |
|
577 | ||
|
578 | 578 | for n in range(1,self.prompt_count + 1): |
|
579 | 579 | key = '_'+`n` |
|
580 | 580 | try: |
|
581 | 581 | del self.user_ns[key] |
|
582 | 582 | except: pass |
|
583 | self.prompt_count = 1 | |
|
584 | self.cache_count = 1 | |
|
583 | self.user_ns['_oh'].clear() | |
|
584 | ||
|
585 | if '_' not in __builtin__.__dict__: | |
|
586 | self.user_ns.update({'_':None,'__':None, '___':None}) | |
|
587 | import gc | |
|
588 | gc.collect() # xxx needed? |
@@ -4,6 +4,11 b'' | |||
|
4 | 4 | in command line. E.g. "ipython test.ipy" runs test.ipy with ipython |
|
5 | 5 | prefilters, allowing stuff like magics and aliases in the file. |
|
6 | 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. | |
|
11 | ||
|
7 | 12 | 2006-03-28 Ville Vainio <vivainio@gmail.com> |
|
8 | 13 | |
|
9 | 14 | * iplib.py: Fix quoting of aliases so that only argless ones |
General Comments 0
You need to be logged in to leave comments.
Login now