docstr: Print input history (_i<n> variables), with most recent last.
  
  By default, input history is printed without line numbers so it can be
  directly pasted into an editor. Use -n to show them.
  
  By default, all input history from the current session is displayed.
  Ranges of history can be indicated using the syntax:
  
  ``4``
      Line 4, current session
  ``4-6``
      Lines 4-6, current session
  ``243/1-5``
      Lines 1-5, session 243
  ``~2/7``
      Line 7, session 2 before current
  ``~8/1-~6/5``
      From the first line of 8 sessions ago, to the fifth line of 6
      sessions ago.
  
  Multiple ranges can be entered, separated by spaces
  
  The same syntax is used by %macro, %save, %edit, %rerun
  
  Examples
  --------
  ::
  
    In [6]: %history -n 4-6
    4:a = 12
    5:print a**2
    6:%history -n 4-6
docstr_tok: ["Print","input","history","(","_i","<","n",">","variables",")",",","with","most","recent","last",".","By","default",",","input","history","is","printed","without","line","numbers","so","it","can","be","directly","pasted","into","an","editor",".","Use","-n","to","show","them",".","By","default",",","all","input","history","from","the","current","session","is","displayed",".","Ranges","of","history","can","be","indicated","using","the","syntax",":","``","4","``","Line","4",",","current","session","``","4-6","``","Lines","4-6",",","current","session","``","243\/1-5","``","Lines","1-5",",","session","243","``","~2\/7","``","Line","7",",","session","2","before","current","``","~8\/1-~6\/5","``","From","the","first","line","of","8","sessions","ago",",","to","the","fifth","line","of","6","sessions","ago",".","Multiple","ranges","can","be","entered",",","separated","by","spaces","The","same","syntax","is","used","by","%","macro",",","%","save",",","%","edit",",","%","rerun","Examples","--","--","--","--",":",":","In","[","6","]",":","%","history","-n","4-6","4",":","a","=","12","5",":","print","a","*","*","2","6",":","%","history","-n","4-6"]
code: def history(self, parameter_s=""):
      """Print input history (_i<n> variables), with most recent last.
  
      By default, input history is printed without line numbers so it can be
      directly pasted into an editor. Use -n to show them.
  
      By default, all input history from the current session is displayed.
      Ranges of history can be indicated using the syntax:
  
      ``4``
          Line 4, current session
      ``4-6``
          Lines 4-6, current session
      ``243/1-5``
          Lines 1-5, session 243
      ``~2/7``
          Line 7, session 2 before current
      ``~8/1-~6/5``
          From the first line of 8 sessions ago, to the fifth line of 6
          sessions ago.
  
      Multiple ranges can be entered, separated by spaces
  
      The same syntax is used by %macro, %save, %edit, %rerun
  
      Examples
      --------
      ::
  
        In [6]: %history -n 4-6
        4:a = 12
        5:print a**2
        6:%history -n 4-6
  
      """
  
      args = parse_argstring(self.history, parameter_s)
  
      # For brevity
      history_manager = self.shell.history_manager
  
      def _format_lineno(session, line):
          """Helper function to format line numbers properly."""
          if session in (0, history_manager.session_number):
              return str(line)
          return "%s/%s" % (session, line)
  
      # Check if output to specific file was requested.
      outfname = args.filename
      if not outfname:
          outfile = sys.stdout  # default
          # We don't want to close stdout at the end!
          close_at_end = False
      else:
          outfname = os.path.expanduser(outfname)
          if os.path.exists(outfname):
              try:
                  ans = io.ask_yes_no(
                      "File %r exists. Overwrite?" % outfname
                  )
              except StdinNotImplementedError:
                  ans = True
              if not ans:
                  print("Aborting.")
                  return
              print("Overwriting file.")
          outfile = io_open(outfname, "w", encoding="utf-8")
          close_at_end = True
  
      print_nums = args.print_nums
      get_output = args.get_output
      pyprompts = args.pyprompts
      raw = args.raw
  
      pattern = None
      limit = None if args.limit is _unspecified else args.limit
  
      range_pattern = False
      if args.pattern is not None and not args.range:
          if args.pattern:
              pattern = "*" + " ".join(args.pattern) + "*"
          else:
              pattern = "*"
          hist = history_manager.search(
              pattern,
              raw=raw,
              output=get_output,
              n=limit,
              unique=args.unique,
          )
          print_nums = True
      elif args.limit is not _unspecified:
          n = 10 if limit is None else limit
          hist = history_manager.get_tail(n, raw=raw, output=get_output)
      else:
          if args.pattern:
              range_pattern = "*" + " ".join(args.pattern) + "*"
              print_nums = True
          hist = history_manager.get_range_by_str(
              " ".join(args.range), raw, get_output
          )
  
      # We could be displaying the entire history, so let's not try to pull
      # it into a list in memory. Anything that needs more space will just
      # misalign.
      width = 4
  
      for session, lineno, inline in hist:
          # Print user history with tabs expanded to 4 spaces.  The GUI
          # clients use hard tabs for easier usability in auto-indented code,
          # but we want to produce PEP-8 compliant history for safe pasting
          # into an editor.
          if get_output:
              inline, output = inline
          if range_pattern:
              if not fnmatch.fnmatch(inline, range_pattern):
                  continue
          inline = inline.expandtabs(4).rstrip()
  
          multiline = "\n" in inline
          line_sep = "\n" if multiline else " "
          if print_nums:
              print(
                  "%s:%s"
                  % (
                      _format_lineno(session, lineno).rjust(width),
                      line_sep,
                  ),
                  file=outfile,
                  end="",
              )
          if pyprompts:
              print(">>> ", end="", file=outfile)
              if multiline:
                  inline = "\n... ".join(inline.splitlines()) + "\n..."
          print(inline, file=outfile)
          if get_output and output:
              print(output, file=outfile)
  
      if close_at_end:
          outfile.close()
  
file_path: catboost/contrib/python/ipython/py3/IPython/core/magics/history.py
imports: import os
  import sys
  from io import open
  import fnmatch
  from IPython.core.error import StdinNotImplementedError
  from IPython.core.magic import Magics, magics_class, line_magic
  from IPython.core.magic_arguments import argument, magic_arguments, parse_argstring
  from IPython.testing.skipdoctest import skip_doctest
  from IPython.utils import io
label_desc: Use image node_id 1 for calling the HistoryMagics obj's underlying member method code with example usage: obj.history(parameter_s) and returns: unknown, str