##// END OF EJS Templates
Further refinements to history interfaces.
Thomas Kluyver -
Show More
@@ -195,17 +195,26 b' class HistoryManager(Configurable):'
195 195 return cur
196 196
197 197
198 def get_hist_tail(self, n=10, raw=True, output=False):
199 """Get the last n lines from the history database."""
198 def get_hist_tail(self, n=10, raw=True, output=False, include_latest=False):
199 """Get the last n lines from the history database.
200
201 If include_latest is False (default), n+1 lines are fetched, and
202 the latest one is discarded. This is intended to be used where
203 the function is called by a user command, which it should not
204 return."""
200 205 self.writeout_cache()
206 if not include_latest:
207 n += 1
201 208 cur = self._get_hist_sql("ORDER BY session DESC, line DESC LIMIT ?",
202 209 (n,), raw=raw, output=output)
210 if not include_latest:
211 return reversed(list(cur)[1:])
203 212 return reversed(list(cur))
204 213
205 214 def get_hist_search(self, pattern="*", raw=True, search_raw=True,
206 215 output=False):
207 """Search the database using unix glob-style matching (wildcards * and
208 ?, escape using \).
216 """Search the database using unix glob-style matching (wildcards
217 * and ?).
209 218
210 219 Returns
211 220 -------
@@ -620,19 +629,20 b" def magic_rerun(self, parameter_s=''):"
620 629 """
621 630 opts, args = self.parse_options(parameter_s, 'l:g:', mode='string')
622 631 if "l" in opts: # Last n lines
623 n = int(opts['l']) + 1
632 n = int(opts['l'])
624 633 hist = self.history_manager.get_hist_tail(n, raw=False)
625 634 elif "g" in opts: # Search
626 635 p = "*"+opts['g']+"*"
627 636 hist = self.history_manager.get_hist_search(p, raw=False)
628 hist = list(hist)[-2:]
637 hist = list(hist)
638 if 'magic("rerun' in hist[-1][2]:
639 hist = hist[:-1] # We can ignore the current line
640 hist = hist[-1:] # Just get the last match
629 641 elif args: # Specify history ranges
630 642 hist = self.history_manager.get_hist_from_rangestr(args)
631 643 else: # Last line
632 hist = self.history_manager.get_hist_tail(2, raw=False)
644 hist = self.history_manager.get_hist_tail(1, raw=False)
633 645 hist = [x[2] for x in hist]
634 if hist and parameter_s in hist[-1]:
635 hist = hist[:-1]
636 646 if not hist:
637 647 print("No lines in history match specification")
638 648 return
@@ -647,7 +657,6 b' def init_ipython(ip):'
647 657 ip.define_magic("rep", magic_rep)
648 658 ip.define_magic("recall", magic_rep)
649 659 ip.define_magic("rerun", magic_rerun)
650 ip.define_magic("r", magic_rerun)
651 660 ip.define_magic("hist",magic_history) # Alternative name
652 661 ip.define_magic("history",magic_history)
653 662
@@ -1550,7 +1550,8 b' class InteractiveShell(Configurable, Magic):'
1550 1550 readline.set_history_length(self.history_length)
1551 1551
1552 1552 # Load the last 1000 lines from history
1553 for _, _, cell in self.history_manager.get_hist_tail(1000):
1553 for _, _, cell in self.history_manager.get_hist_tail(1000,
1554 include_latest=True):
1554 1555 if cell.strip(): # Ignore blank lines
1555 1556 for line in cell.splitlines():
1556 1557 readline.add_history(line)
@@ -59,13 +59,19 b' def test_history():'
59 59 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
60 60
61 61 # Check get_hist_tail
62 gothist = ip.history_manager.get_hist_tail(4, output=True)
62 gothist = ip.history_manager.get_hist_tail(4, output=True,
63 include_latest=True)
63 64 expected = [(1, 3, (hist[-1], ["spam"])),
64 65 (2, 1, (newcmds[0], None)),
65 66 (2, 2, (newcmds[1], None)),
66 67 (2, 3, (newcmds[2], None)),]
67 68 nt.assert_equal(list(gothist), expected)
68 69
70 gothist = ip.history_manager.get_hist_tail(2)
71 expected = [(2, 1, newcmds[0]),
72 (2, 2, newcmds[1])]
73 nt.assert_equal(list(gothist), expected)
74
69 75 # Check get_hist_search
70 76 gothist = ip.history_manager.get_hist_search("*test*")
71 77 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
@@ -92,3 +98,12 b' def test_extract_hist_ranges():'
92 98 (-7, 1, 6)]
93 99 actual = list(extract_hist_ranges(instr))
94 100 nt.assert_equal(actual, expected)
101
102 def test_magic_rerun():
103 """Simple test for %rerun (no args -> rerun last line)"""
104 ip = get_ipython()
105 ip.run_cell("a = 10")
106 ip.run_cell("a += 1")
107 nt.assert_equal(ip.user_ns["a"], 11)
108 ip.run_cell("%rerun")
109 nt.assert_equal(ip.user_ns["a"], 12)
General Comments 0
You need to be logged in to leave comments. Login now