





#
# Sort large text files in a minimum amount of memory
#
import argparse
import os

class 			__A:
     __A							=							'''block_{0}.dat'''

     def __init__(   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    filename
            _UpperCamelCase														=    []

     def 						_UpperCamelCase  (   self,							A,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    self.BLOCK_FILENAME_FORMAT.format(__SCREAMING_SNAKE_CASE   )
            with open(__SCREAMING_SNAKE_CASE,							'''w'''   ) as file:
                   file.write(__SCREAMING_SNAKE_CASE   )
            self.block_filenames.append(__SCREAMING_SNAKE_CASE   )

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            return self.block_filenames

     def 						_UpperCamelCase  (   self,							A,							A=None   ):





            """simple docstring"""

            _UpperCamelCase														=    0
            with open(self.filename   ) as file:
                   while True:
                          _UpperCamelCase														=    file.readlines(__SCREAMING_SNAKE_CASE   )

                          if lines == []:
                                 break

                          if sort_key is None:
                                 lines.sort()
                          else:
                                 lines.sort(key=__SCREAMING_SNAKE_CASE   )

                          self.write_block(''''''.join(__SCREAMING_SNAKE_CASE   ),							__SCREAMING_SNAKE_CASE   )
                          i += 1



     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            map(os.remove,							self.block_filenames   )






class 			__A:

     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    -1
            _UpperCamelCase														=    None

            for i in range(len(__SCREAMING_SNAKE_CASE   )   ):
                   if min_str is None or choices[i] < min_str:
                          _UpperCamelCase														=    i

            return min_index






class 			__A:

     def __init__(   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    files
            _UpperCamelCase														=    set()
            _UpperCamelCase														=    len(__SCREAMING_SNAKE_CASE   )
            _UpperCamelCase														=    {i: None for i in range(self.num_buffers   )}

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            return {
                i: self.buffers[i] for i in range(self.num_buffers   ) if i not in self.empty
            }

     def 						_UpperCamelCase  (   self   ):





            """simple docstring"""

            for i in range(self.num_buffers   ):
                   if self.buffers[i] is None and i not in self.empty:
                          _UpperCamelCase														=    self.files[i].readline()

                          if self.buffers[i] == "":
                                 self.empty.add(__SCREAMING_SNAKE_CASE   )
                                 self.files[i].close()

            if len(self.empty   ) == self.num_buffers:
                   return False

            return True



     def 						_UpperCamelCase  (   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    self.buffers[index]
            _UpperCamelCase														=    None

            return value






class 			__A:

     def __init__(   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    merge_strategy

     def 						_UpperCamelCase  (   self,							A,							A,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    FilesArray(self.get_file_handles(__SCREAMING_SNAKE_CASE,							__SCREAMING_SNAKE_CASE   )   )
            with open(__SCREAMING_SNAKE_CASE,							'''w''',							__SCREAMING_SNAKE_CASE   ) as outfile:
                   while buffers.refresh():
                          _UpperCamelCase														=    self.merge_strategy.select(buffers.get_dict()   )
                          outfile.write(buffers.unshift(__SCREAMING_SNAKE_CASE   )   )



     def 						_UpperCamelCase  (   self,							A,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    {}

            for i in range(len(__SCREAMING_SNAKE_CASE   )   ):
                   _UpperCamelCase														=    open(filenames[i],							'''r''',							__SCREAMING_SNAKE_CASE   )  # noqa: UP015

            return files






class 			__A:

     def __init__(   self,							A   ):





            """simple docstring"""

            _UpperCamelCase														=    block_size

     def 						_UpperCamelCase  (   self,							A,							A=None   ):





            """simple docstring"""

            _UpperCamelCase														=    self.get_number_blocks(__SCREAMING_SNAKE_CASE,							self.block_size   )
            _UpperCamelCase														=    FileSplitter(__SCREAMING_SNAKE_CASE   )
            splitter.split(self.block_size,							__SCREAMING_SNAKE_CASE   )

            _UpperCamelCase														=    FileMerger(NWayMerge()   )
            _UpperCamelCase														=    self.block_size / (num_blocks + 1)
            merger.merge(splitter.get_block_filenames(),							filename + '''.out''',							__SCREAMING_SNAKE_CASE   )

            splitter.cleanup()



     def 						_UpperCamelCase  (   self,							A,							A   ):





            """simple docstring"""

            return (os.stat(__SCREAMING_SNAKE_CASE   ).st_size / block_size) + 1






def      SCREAMING_SNAKE_CASE      (   lowerCAmelCase       ):
       if string[-1].lower() == "k":
              return int(string[:-1]       ) * 1_024
       elif string[-1].lower() == "m":
              return int(string[:-1]       ) * 1_024 * 1_024
       elif string[-1].lower() == "g":
              return int(string[:-1]       ) * 1_024 * 1_024 * 1_024
       else:
              return int(_UpperCAmelCase       )


def      SCREAMING_SNAKE_CASE      (   ):
       _UpperCamelCase														=    argparse.ArgumentParser()
       parser.add_argument(
           '''-m'''     ,							'''--mem'''     ,							help='''amount of memory to use for sorting'''     ,							default='''100M'''       )
       parser.add_argument(
           '''filename'''     ,							metavar='''<filename>'''     ,							nargs=1     ,							help='''name of file to sort'''       )
       _UpperCamelCase														=    parser.parse_args()

       _UpperCamelCase														=    ExternalSort(parse_memory(args.mem       )       )
       sorter.sort(args.filename[0]       )


if __name__ == "__main__":
       main()











def 	__snake_case      (     _UpperCAmelCase  ,					_UpperCAmelCase  ,					_UpperCAmelCase							):
    __a					=							[1 / first_term]
    __a					=							1 / first_term
    for _ in range(number_of_terms - 1							):
        first_term += common_difference
        arithmetic_progression.append(_UpperCAmelCase							)
    __a					=							[1 / step for step in arithmetic_progression]
    return sum(_UpperCAmelCase							)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 print(sum_of_harmonic_progression(1 / 2, 2, 2))





