~ellisonbg/ipython/bugfixes0411409

« back to all changes in this revision

Viewing changes to IPython/Extensions/clearcmd.py

  • Committer: ville
  • Date: 2008-02-16 09:50:47 UTC
  • mto: (0.12.1 ipython_main)
  • mto: This revision was merged to the branch mainline in revision 990.
  • Revision ID: ville@ville-pc-20080216095047-500x6dluki1iz40o
initialization (no svn history)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
    %clear shadow_compress - Compresses shadow history (to speed up ipython)
 
15
    %clear shadow_nuke - permanently erase all entries in shadow history
 
16
    %clear dhist - clear dir history
 
17
    """
 
18
    
 
19
    api = self.getapi()
 
20
    for target in arg.split():
 
21
        if target == 'out':
 
22
            print "Flushing output cache (%d entries)" % len(api.user_ns['_oh'])
 
23
            self.outputcache.flush()
 
24
        elif target == 'in':
 
25
            print "Flushing input history"
 
26
            from IPython import iplib
 
27
            pc = self.outputcache.prompt_count + 1
 
28
            for n in range(1, pc):
 
29
                key = '_i'+`n`
 
30
                try:
 
31
                    del self.user_ns[key]
 
32
                except: pass
 
33
            # must be done in-place
 
34
            self.input_hist[:] = ['\n'] * pc 
 
35
            self.input_hist_raw[:] = ['\n'] * pc
 
36
        elif target == 'array':
 
37
            try:
 
38
                pylab=ip.IP.pylab
 
39
                for x in self.user_ns.keys():
 
40
                    if isinstance(self.user_ns[x],pylab.arraytype):
 
41
                        del self.user_ns[x]
 
42
            except AttributeError:
 
43
                print "Clear array only available in -pylab mode"
 
44
            gc.collect()                
 
45
 
 
46
        elif target == 'shadow_compress':
 
47
            print "Compressing shadow history"
 
48
            api.db.hcompress('shadowhist')
 
49
            
 
50
        elif target == 'shadow_nuke':
 
51
            print "Erased all keys from shadow history "
 
52
            for k in ip.db.keys('shadowhist/*'):
 
53
                del ip.db[k]
 
54
        elif target == 'dhist':
 
55
            print "Clearing directory history"
 
56
            del ip.user_ns['_dh'][:]
 
57
 
 
58
            
 
59
ip.expose_magic("clear",clear_f)
 
60
import ipy_completers
 
61
ipy_completers.quick_completer(
 
62
 '%clear','in out shadow_nuke shadow_compress dhist')
 
63
    
 
64
 
 
65
 
 
66