##// END OF EJS Templates
Changed %hist to default to NOT printing numbers, added -p and -o options....
Fernando Perez -
Show More
@@ -15,19 +15,24 b" def magic_history(self, parameter_s = ''):"
15 15 %history n -> print at most n inputs\\
16 16 %history n1 n2 -> print inputs between n1 and n2 (n2 not included)\\
17 17
18 Each input's number <n> is shown, and is accessible as the
19 automatically generated variable _i<n>. Multi-line statements are
20 printed starting at a new line for easy copy/paste.
18 By default, input history is printed without line numbers so it can be
19 directly pasted into an editor.
21 20
21 With -n, each input's number <n> is shown, and is accessible as the
22 automatically generated variable _i<n> as well as In[<n>]. Multi-line
23 statements are printed starting at a new line for easy copy/paste.
22 24
23 25 Options:
24 26
25 -n: do NOT print line numbers. This is useful if you want to get a
26 printout of many lines which can be directly pasted into a text
27 editor.
28
27 -n: print line numbers for each input.
29 28 This feature is only available if numbered prompts are in use.
30 29
30 -o: also print outputs for each input.
31
32 -p: print classic '>>>' python prompts before each input. This is useful
33 for making documentation, and in conjunction with -o, for producing
34 doctest-ready output.
35
31 36 -t: (default) print the 'translated' history, as IPython understands it.
32 37 IPython filters your input and converts it all into valid Python source
33 38 before executing it (things like magics or aliases are turned into
@@ -50,7 +55,7 b" def magic_history(self, parameter_s = ''):"
50 55 if not self.outputcache.do_full_cache:
51 56 print 'This feature is only available if numbered prompts are in use.'
52 57 return
53 opts,args = self.parse_options(parameter_s,'gntsrf:',mode='list')
58 opts,args = self.parse_options(parameter_s,'gnoptsrf:',mode='list')
54 59
55 60 # Check if output to specific file was requested.
56 61 try:
@@ -97,9 +102,12 b" def magic_history(self, parameter_s = ''):"
97 102 warn('%hist takes 0, 1 or 2 arguments separated by spaces.')
98 103 print self.magic_hist.__doc__
99 104 return
105
100 106 width = len(str(final))
101 107 line_sep = ['','\n']
102 print_nums = not opts.has_key('n')
108 print_nums = 'n' in opts
109 print_outputs = 'o' in opts
110 pyprompts = 'p' in opts
103 111
104 112 found = False
105 113 if pattern is not None:
@@ -123,7 +131,19 b" def magic_history(self, parameter_s = ''):"
123 131 if print_nums:
124 132 print >> outfile, \
125 133 '%s:%s' % (str(in_num).ljust(width),line_sep[multiline]),
134 if pyprompts:
135 print >> outfile, '>>>',
136 if multiline:
137 lines = inline.splitlines()
138 print >> outfile, '\n... '.join(lines)
139 print >> outfile, '... '
140 else:
141 print >> outfile, inline,
142 else:
126 143 print >> outfile, inline,
144 output = self.shell.user_ns['Out'].get(in_num)
145 if output is not None:
146 print repr(output)
127 147
128 148 if close_at_end:
129 149 outfile.close()
@@ -1619,7 +1619,6 b' class InteractiveShell(Component, Magic):'
1619 1619 valid Python code you can type at the interpreter, including loops and
1620 1620 compound statements.
1621 1621 """
1622
1623 1622 args = arg_s.split(' ',1)
1624 1623 magic_name = args[0]
1625 1624 magic_name = magic_name.lstrip(prefilter.ESC_MAGIC)
@@ -60,18 +60,86 b' def doctest_hist_f():'
60 60 def doctest_hist_r():
61 61 """Test %hist -r
62 62
63 XXX - This test is not recording the output correctly. Not sure why...
63 XXX - This test is not recording the output correctly. For some reason, in
64 testing mode the raw history isn't getting populated. No idea why.
65 Disabling the output checking for now, though at least we do run it.
64 66
65 In [20]: 'hist' in _ip.lsmagic()
66 Out[20]: True
67 In [1]: 'hist' in _ip.lsmagic()
68 Out[1]: True
67 69
68 In [6]: x=1
70 In [2]: x=1
69 71
70 In [7]: %hist -n -r 2
72 In [3]: %hist -r 2
71 73 x=1 # random
72 hist -n -r 2 # random
74 %hist -r 2
73 75 """
74 76
77 def doctest_hist_op():
78 """Test %hist -op
79
80 In [1]: class b:
81 ...: pass
82 ...:
83
84 In [2]: class s(b):
85 ...: def __str__(self):
86 ...: return 's'
87 ...:
88
89 In [3]:
90
91 In [4]: class r(b):
92 ...: def __repr__(self):
93 ...: return 'r'
94 ...:
95
96 In [5]: class sr(s,r): pass
97 ...:
98
99 In [6]:
100
101 In [7]: bb=b()
102
103 In [8]: ss=s()
104
105 In [9]: rr=r()
106
107 In [10]: ssrr=sr()
108
109 In [11]: bb
110 Out[11]: <...b instance at ...>
111
112 In [12]: ss
113 Out[12]: <...s instance at ...>
114
115 In [13]:
116
117 In [14]: %hist -op
118 >>> class b:
119 ... pass
120 ...
121 >>> class s(b):
122 ... def __str__(self):
123 ... return 's'
124 ...
125 >>>
126 >>> class r(b):
127 ... def __repr__(self):
128 ... return 'r'
129 ...
130 >>> class sr(s,r): pass
131 >>>
132 >>> bb=b()
133 >>> ss=s()
134 >>> rr=r()
135 >>> ssrr=sr()
136 >>> bb
137 <...b instance at ...>
138 >>> ss
139 <...s instance at ...>
140 >>>
141 >>> get_ipython().magic("hist -op")
142 """
75 143
76 144 def test_shist():
77 145 # Simple tests of ShadowHist class - test generator.
@@ -134,12 +134,9 b' def start_ipython():'
134 134 ip = ipapp.IPythonApp(argv, user_ns=user_ns, user_global_ns=global_ns)
135 135 ip.initialize()
136 136 ip.shell.builtin_trap.set()
137 # Set stderr to stdout so nose can doctest exceptions
138 ## Term.cerr = sys.stdout
139 ## sys.stderr = sys.stdout
137
138 # Set error printing to stdout so nose can doctest exceptions
140 139 ip.shell.InteractiveTB.out_stream = 'stdout'
141 # Butcher the logger
142 ip.shell.log = lambda *a,**k: None
143 140
144 141 # Deactivate the various python system hooks added by ipython for
145 142 # interactive convenience so we don't confuse the doctest system
@@ -160,12 +157,4 b' def start_ipython():'
160 157 # doctest machinery would miss them.
161 158 ip.shell.system = xsys
162 159
163 # XXX - For some very bizarre reason, the loading of %history by default is
164 # failing. This needs to be fixed later, but for now at least this ensures
165 # that tests that use %hist run to completion.
166 from IPython.core import history
167 history.init_ipython(ip.shell)
168 if not hasattr(ip.shell,'magic_history'):
169 raise RuntimeError("Can't load magics, aborting")
170
171 160 return _ip
General Comments 0
You need to be logged in to leave comments. Login now