@@ -202,7 +202,6 @@ def __init__(self, profile="default", hist_file="", **traits):
202202 config : :class:`~traitlets.config.loader.Config`
203203 Config object. hist_file can also be set through this.
204204 """
205- # We need a pointer back to the shell for various tasks.
206205 super (HistoryAccessor , self ).__init__ (** traits )
207206 # defer setting hist_file from kwarg until after init,
208207 # otherwise the default kwarg value would clobber any value
@@ -344,11 +343,6 @@ def get_last_session_id(self):
344343 def get_tail (self , n = 10 , raw = True , output = False , include_latest = False ):
345344 """Get the last n lines from the history database.
346345
347- Most recent entry last.
348-
349- Completion will be reordered so that that the last ones are when
350- possible from current session.
351-
352346 Parameters
353347 ----------
354348 n : int
@@ -367,31 +361,12 @@ def get_tail(self, n=10, raw=True, output=False, include_latest=False):
367361 self .writeout_cache ()
368362 if not include_latest :
369363 n += 1
370- # cursor/line/entry
371- this_cur = list (
372- self ._run_sql (
373- "WHERE session == ? ORDER BY line DESC LIMIT ? " ,
374- (self .session_number , n ),
375- raw = raw ,
376- output = output ,
377- )
378- )
379- other_cur = list (
380- self ._run_sql (
381- "WHERE session != ? ORDER BY session DESC, line DESC LIMIT ?" ,
382- (self .session_number , n ),
383- raw = raw ,
384- output = output ,
385- )
364+ cur = self ._run_sql (
365+ "ORDER BY session DESC, line DESC LIMIT ?" , (n ,), raw = raw , output = output
386366 )
387-
388- everything = this_cur + other_cur
389-
390- everything = everything [:n ]
391-
392367 if not include_latest :
393- return list (everything )[: 0 : - 1 ]
394- return list (everything )[:: - 1 ]
368+ return reversed ( list (cur )[ 1 :])
369+ return reversed ( list (cur ))
395370
396371 @catch_corrupt_db
397372 def search (self , pattern = "*" , raw = True , search_raw = True ,
@@ -560,7 +535,6 @@ def _dir_hist_default(self):
560535 def __init__ (self , shell = None , config = None , ** traits ):
561536 """Create a new history manager associated with a shell instance.
562537 """
563- # We need a pointer back to the shell for various tasks.
564538 super (HistoryManager , self ).__init__ (shell = shell , config = config ,
565539 ** traits )
566540 self .save_flag = threading .Event ()
@@ -656,6 +630,59 @@ def get_session_info(self, session=0):
656630
657631 return super (HistoryManager , self ).get_session_info (session = session )
658632
633+ @catch_corrupt_db
634+ def get_tail (self , n = 10 , raw = True , output = False , include_latest = False ):
635+ """Get the last n lines from the history database.
636+
637+ Most recent entry last.
638+
639+ Completion will be reordered so that that the last ones are when
640+ possible from current session.
641+
642+ Parameters
643+ ----------
644+ n : int
645+ The number of lines to get
646+ raw, output : bool
647+ See :meth:`get_range`
648+ include_latest : bool
649+ If False (default), n+1 lines are fetched, and the latest one
650+ is discarded. This is intended to be used where the function
651+ is called by a user command, which it should not return.
652+
653+ Returns
654+ -------
655+ Tuples as :meth:`get_range`
656+ """
657+ self .writeout_cache ()
658+ if not include_latest :
659+ n += 1
660+ # cursor/line/entry
661+ this_cur = list (
662+ self ._run_sql (
663+ "WHERE session == ? ORDER BY line DESC LIMIT ? " ,
664+ (self .session_number , n ),
665+ raw = raw ,
666+ output = output ,
667+ )
668+ )
669+ other_cur = list (
670+ self ._run_sql (
671+ "WHERE session != ? ORDER BY session DESC, line DESC LIMIT ?" ,
672+ (self .session_number , n ),
673+ raw = raw ,
674+ output = output ,
675+ )
676+ )
677+
678+ everything = this_cur + other_cur
679+
680+ everything = everything [:n ]
681+
682+ if not include_latest :
683+ return list (everything )[:0 :- 1 ]
684+ return list (everything )[::- 1 ]
685+
659686 def _get_range_session (self , start = 1 , stop = None , raw = True , output = False ):
660687 """Get input and output history from the current session. Called by
661688 get_range, and takes similar parameters."""
0 commit comments