##// END OF EJS Templates
Merge pull request #2609 from tkf/history-unique...
Thomas Kluyver -
r9030:751e3470 merge
parent child Browse files
Show More
@@ -307,7 +307,7 b' class HistoryAccessor(Configurable):'
307 307
308 308 @catch_corrupt_db
309 309 def search(self, pattern="*", raw=True, search_raw=True,
310 output=False, n=None):
310 output=False, n=None, unique=False):
311 311 """Search the database using unix glob-style matching (wildcards
312 312 * and ?).
313 313
@@ -322,6 +322,8 b' class HistoryAccessor(Configurable):'
322 322 n : None or int
323 323 If an integer is given, it defines the limit of
324 324 returned entries.
325 unique : bool
326 When it is true, return only unique entries.
325 327
326 328 Returns
327 329 -------
@@ -333,9 +335,13 b' class HistoryAccessor(Configurable):'
333 335 self.writeout_cache()
334 336 sqlform = "WHERE %s GLOB ?" % tosearch
335 337 params = (pattern,)
338 if unique:
339 sqlform += ' GROUP BY {0}'.format(tosearch)
336 340 if n is not None:
337 341 sqlform += " ORDER BY session DESC, line DESC LIMIT ?"
338 342 params += (n,)
343 elif unique:
344 sqlform += " ORDER BY session, line"
339 345 cur = self._run_sql(sqlform, params, raw=raw, output=output)
340 346 if n is not None:
341 347 return reversed(list(cur))
@@ -89,6 +89,11 b' class HistoryMagics(Magics):'
89 89 get the last n lines from all sessions. Specify n as a single
90 90 arg, or the default is the last 10 lines.
91 91 """)
92 @argument(
93 '-u', dest='unique', action='store_true',
94 help="""
95 when searching history using `-g`, show only unique history.
96 """)
92 97 @argument('range', nargs='*')
93 98 @skip_doctest
94 99 @line_magic
@@ -165,7 +170,7 b' class HistoryMagics(Magics):'
165 170 else:
166 171 pattern = "*"
167 172 hist = history_manager.search(pattern, raw=raw, output=get_output,
168 n=limit)
173 n=limit, unique=args.unique)
169 174 print_nums = True
170 175 elif args.limit is not _unspecified:
171 176 n = 10 if limit is None else limit
@@ -61,7 +61,10 b' def test_history():'
61 61
62 62 # New session
63 63 ip.history_manager.reset()
64 newcmds = ["z=5","class X(object):\n pass", "k='p'"]
64 newcmds = [u"z=5",
65 u"class X(object):\n pass",
66 u"k='p'",
67 u"z=5"]
65 68 for i, cmd in enumerate(newcmds, start=1):
66 69 ip.history_manager.store_inputs(i, cmd)
67 70 gothist = ip.history_manager.get_range(start=1, stop=4)
@@ -70,35 +73,53 b' def test_history():'
70 73 gothist = ip.history_manager.get_range(-1, 1, 4)
71 74 nt.assert_equal(list(gothist), zip([1,1,1],[1,2,3], hist))
72 75
76 newhist = [(2, i, c) for (i, c) in enumerate(newcmds, 1)]
77
73 78 # Check get_hist_tail
74 gothist = ip.history_manager.get_tail(4, output=True,
79 gothist = ip.history_manager.get_tail(5, output=True,
75 80 include_latest=True)
76 expected = [(1, 3, (hist[-1], "spam")),
77 (2, 1, (newcmds[0], None)),
78 (2, 2, (newcmds[1], None)),
79 (2, 3, (newcmds[2], None)),]
81 expected = [(1, 3, (hist[-1], "spam"))] \
82 + [(s, n, (c, None)) for (s, n, c) in newhist]
80 83 nt.assert_equal(list(gothist), expected)
81 84
82 85 gothist = ip.history_manager.get_tail(2)
83 expected = [(2, 1, newcmds[0]),
84 (2, 2, newcmds[1])]
86 expected = newhist[-3:-1]
85 87 nt.assert_equal(list(gothist), expected)
86 88
87 89 # Check get_hist_search
88 90 gothist = ip.history_manager.search("*test*")
89 91 nt.assert_equal(list(gothist), [(1,2,hist[1])] )
92
90 93 gothist = ip.history_manager.search("*=*")
91 94 nt.assert_equal(list(gothist),
92 95 [(1, 1, hist[0]),
93 96 (1, 2, hist[1]),
94 97 (1, 3, hist[2]),
95 (2, 1, newcmds[0]),
96 (2, 3, newcmds[2])])
97 gothist = ip.history_manager.search("*=*", n=3)
98 newhist[0],
99 newhist[2],
100 newhist[3]])
101
102 gothist = ip.history_manager.search("*=*", n=4)
103 nt.assert_equal(list(gothist),
104 [(1, 3, hist[2]),
105 newhist[0],
106 newhist[2],
107 newhist[3]])
108
109 gothist = ip.history_manager.search("*=*", unique=True)
110 nt.assert_equal(list(gothist),
111 [(1, 1, hist[0]),
112 (1, 2, hist[1]),
113 (1, 3, hist[2]),
114 newhist[2],
115 newhist[3]])
116
117 gothist = ip.history_manager.search("*=*", unique=True, n=3)
98 118 nt.assert_equal(list(gothist),
99 119 [(1, 3, hist[2]),
100 (2, 1, newcmds[0]),
101 (2, 3, newcmds[2])])
120 newhist[2],
121 newhist[3]])
122
102 123 gothist = ip.history_manager.search("b*", output=True)
103 124 nt.assert_equal(list(gothist), [(1,3,(hist[2],"spam"))] )
104 125
General Comments 0
You need to be logged in to leave comments. Login now