original_docstring: a generator that returns the lines of a file in reverse order
docstring: a generator that returns the lines of a file in reverse order
docstring_tokens: a generator that returns the lines of a file in reverse order
code: def reverse_readline(
      fh: IO, start_byte: int = 0, buf_size: int = 8192
  ) -> Generator[str, None, None]:
      """a generator that returns the lines of a file in reverse order"""
      segment: OptionalType[str] = None
      offset = 0
      if start_byte:
          fh.seek(start_byte)
      else:
          fh.seek(0, os.SEEK_END)
      total_size = remaining_size = fh.tell()
      while remaining_size > 0:
          offset = min(total_size, offset + buf_size)
          fh.seek(-offset, os.SEEK_END)
          buf = fh.read(min(remaining_size, buf_size))
          remaining_size -= buf_size
          lines = buf.decode(sys.getfilesystemencoding()).split('\n')
          # the first line of the buffer is probably not a complete line so
          # we'll save it and append it to the last line of the next buffer
          # we read
          if segment is not None:
              # if the previous chunk starts right from the beginning of line
              # do not concact the segment to the last line of new chunk
              # instead, yield the segment first
              if buf[-1] != '\n':
                  lines[-1] += segment
              else:
                  yield segment
          segment = lines[0]
          for index in range(len(lines) - 1, 0, -1):
              if len(lines[index]):
                  yield lines[index]
      yield segment
code_tokens: def reverse_readline ( fh : IO , start_byte : int = 0 , buf_size : int = 8192 ) -> Generator [ str , None , None ] : segment : OptionalType [ str ] = None offset = 0 if start_byte : fh . seek ( start_byte ) else : fh . seek ( 0 , os . SEEK_END ) total_size = remaining_size = fh . tell ( ) while remaining_size > 0 : offset = min ( total_size , offset + buf_size ) fh . seek ( - offset , os . SEEK_END ) buf = fh . read ( min ( remaining_size , buf_size ) ) remaining_size -= buf_size lines = buf . decode ( sys . getfilesystemencoding ( ) ) . split ( '\n' ) if segment is not None : if buf [ - 1 ] != '\n' : lines [ - 1 ] += segment else : yield segment segment = lines [ 0 ] for index in range ( len ( lines ) - 1 , 0 , - 1 ) : if len ( lines [ index ] ) : yield lines [ index ] yield segment
short_docstring: a generator that returns the lines of a file in reverse order
short_docstring_tokens: a generator that returns the lines of a file in reverse order
comment: """a generator that returns the lines of a file in reverse order"""$SEP$# the first line of the buffer is probably not a complete line so$SEP$# we'll save it and append it to the last line of the next buffer$SEP$# we read$SEP$# if the previous chunk starts right from the beginning of line$SEP$# do not concact the segment to the last line of new chunk$SEP$# instead, yield the segment first