code: 
  #
  # Sort large text files in a minimum amount of memory
  #
  import argparse
  import os
  
  
  
  
  
  class      UpperCamelCase_			:
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  						UpperCamelCase			:     int							      =				'''block_{0}.dat'''
  
  
  
  
  
  						def __init__( self       :str		,						lowerCAmelCase__       :Any    )     ->List[Any]:
  										lowercase						    =			filename
  										lowercase						    =			[]
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Tuple		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :Any    )     ->str:
  										lowercase						    =			self.BLOCK_FILENAME_FORMAT.format(lowerCAmelCase__    )
  										with open(lowerCAmelCase__		,						"w"    ) as file:
  														file.write(lowerCAmelCase__    )
  										self.block_filenames.append(lowerCAmelCase__    )
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Dict    )     ->Any:
  										return self.block_filenames
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Optional[Any]		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :int=None    )     ->Tuple:
  										lowercase						    =			0
  										with open(self.filename    ) as file:
  														while True:
  																		lowercase						    =			file.readlines(lowerCAmelCase__    )
  
  																		if lines == []:
  																						break
  
  																		if sort_key is None:
  																						lines.sort()
  																		else:
  																						lines.sort(key=lowerCAmelCase__    )
  
  																		self.write_block("".join(lowerCAmelCase__    )		,						lowerCAmelCase__    )
  																		i += 1
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Tuple    )     ->Optional[int]:
  										map(os.remove		,						self.block_filenames    )
  
  
  
  
  
  class      UpperCamelCase_			:
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Tuple		,						lowerCAmelCase__       :Optional[int]    )     ->List[str]:
  										lowercase						    =			-1
  										lowercase						    =			None
  
  										for i in range(len(lowerCAmelCase__    )    ):
  														if min_str is None or choices[i] < min_str:
  																		lowercase						    =			i
  
  										return min_index
  
  
  
  
  
  class      UpperCamelCase_			:
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  						def __init__( self       :List[Any]		,						lowerCAmelCase__       :List[Any]    )     ->Tuple:
  										lowercase						    =			files
  										lowercase						    =			set()
  										lowercase						    =			len(lowerCAmelCase__    )
  										lowercase						    =			{i: None for i in range(self.num_buffers    )}
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]    )     ->Optional[Any]:
  										return {
  										    i: self.buffers[i] for i in range(self.num_buffers    ) if i not in self.empty
  										}
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]    )     ->Union[str, Any]:
  										for i in range(self.num_buffers    ):
  														if self.buffers[i] is None and i not in self.empty:
  																		lowercase						    =			self.files[i].readline()
  
  																		if self.buffers[i] == "":
  																						self.empty.add(lowerCAmelCase__    )
  																						self.files[i].close()
  
  										if len(self.empty    ) == self.num_buffers:
  														return False
  
  										return True
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :str    )     ->Tuple:
  										lowercase						    =			self.buffers[index]
  										lowercase						    =			None
  
  										return value
  
  
  
  
  
  class      UpperCamelCase_			:
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  						def __init__( self       :List[str]		,						lowerCAmelCase__       :Any    )     ->List[str]:
  										lowercase						    =			merge_strategy
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Optional[int]		,						lowerCAmelCase__       :Any		,						lowerCAmelCase__       :List[str]		,						lowerCAmelCase__       :Tuple    )     ->str:
  										lowercase						    =			FilesArray(self.get_file_handles(lowerCAmelCase__		,						lowerCAmelCase__    )    )
  										with open(lowerCAmelCase__		,						"w"		,						lowerCAmelCase__    ) as outfile:
  														while buffers.refresh():
  																		lowercase						    =			self.merge_strategy.select(buffers.get_dict()    )
  																		outfile.write(buffers.unshift(lowerCAmelCase__    )    )
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :List[str]		,						lowerCAmelCase__       :Optional[Any]		,						lowerCAmelCase__       :Any    )     ->int:
  										lowercase						    =			{}
  
  										for i in range(len(lowerCAmelCase__    )    ):
  														lowercase						    =			open(filenames[i]		,						"r"		,						lowerCAmelCase__    )  # noqa: UP015
  
  										return files
  
  
  
  
  
  class      UpperCamelCase_			:
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  						def __init__( self       :str		,						lowerCAmelCase__       :List[str]    )     ->Tuple:
  										lowercase						    =			block_size
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :Union[str, Any]		,						lowerCAmelCase__       :List[str]=None    )     ->Optional[Any]:
  										lowercase						    =			self.get_number_blocks(lowerCAmelCase__		,						self.block_size    )
  										lowercase						    =			FileSplitter(lowerCAmelCase__    )
  										splitter.split(self.block_size		,						lowerCAmelCase__    )
  
  										lowercase						    =			FileMerger(NWayMerge()    )
  										lowercase						    =			self.block_size / (num_blocks + 1)
  										merger.merge(splitter.get_block_filenames()		,						filename + ".out"		,						lowerCAmelCase__    )
  
  										splitter.cleanup()
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :List[str]		,						lowerCAmelCase__       :str		,						lowerCAmelCase__       :Optional[int]    )     ->List[str]:
  										return (os.stat(lowerCAmelCase__    ).st_size / block_size) + 1
  
  
  
  
  def    __snake_case       (  __magic_name__	):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				if string[-1].lower() == "k":
  								return int(string[:-1]	) * 1024
  				elif string[-1].lower() == "m":
  								return int(string[:-1]	) * 1024 * 1024
  				elif string[-1].lower() == "g":
  								return int(string[:-1]	) * 1024 * 1024 * 1024
  				else:
  								return int(__magic_name__	)
  
  def    __snake_case       (  ):
  
  
  
  
  
  
  
  				'''simple docstring'''
  
  
  				lowercase						    =			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"	)
  				lowercase						    =			parser.parse_args()
  
  				lowercase						    =			ExternalSort(parse_memory(args.mem	)	)
  				sorter.sort(args.filename[0]	)
  
  
  if __name__ == "__main__":
  						main()
  
style_context: 
  import sys
  from collections.abc import Mapping
  
  import numpy as np
  import pyarrow as pa
  
  from .. import config
  from ..utils.py_utils import map_nested
  from .formatting import TensorFormatter
  
  
  
  
  
  class      UpperCamelCase_			(			TensorFormatter[Mapping, np.ndarray, Mapping]		):
  
  
  
  
  
  
  
  						'''simple docstring'''
  
  
  
  						def __init__( self       :int		,						lowerCAmelCase__       :Any=None		,						**lowerCAmelCase__       :Union[str, Any]    )     ->int:
  										super().__init__(features=lowerCAmelCase__    )
  										lowercase						    =			np_array_kwargs
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Union[str, Any]		,						lowerCAmelCase__       :str    )     ->Optional[Any]:
  										if isinstance(lowerCAmelCase__		,						lowerCAmelCase__    ):
  														if column and all(
  														    isinstance(lowerCAmelCase__		,						np.ndarray    ) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column    ):
  																		return np.stack(lowerCAmelCase__    )
  														else:
  																		# don't use np.array(column, dtype=object)
  																		# since it fails in certain cases
  																		# see https://stackoverflow.com/q/51005699
  																		lowercase						    =			np.empty(len(lowerCAmelCase__    )		,						dtype=lowerCAmelCase__    )
  																		lowercase						    =			column
  																		return out
  										return column
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :str		,						lowerCAmelCase__       :List[str]    )     ->Union[str, Any]:
  										if isinstance(lowerCAmelCase__		,						(str, bytes, type(lowerCAmelCase__    ))    ):
  														return value
  										elif isinstance(lowerCAmelCase__		,						(np.character, np.ndarray)    ) and np.issubdtype(value.dtype		,						np.character    ):
  														return value
  										elif isinstance(lowerCAmelCase__		,						np.number    ):
  														return value
  
  										lowercase						    =			{}
  
  										if isinstance(lowerCAmelCase__		,						np.ndarray    ) and np.issubdtype(value.dtype		,						np.integer    ):
  														lowercase						    =			{"dtype": np.intaa}
  										elif isinstance(lowerCAmelCase__		,						np.ndarray    ) and np.issubdtype(value.dtype		,						np.floating    ):
  														lowercase						    =			{"dtype": np.floataa}
  										elif config.PIL_AVAILABLE and "PIL" in sys.modules:
  														import PIL.Image
  
  														if isinstance(lowerCAmelCase__		,						PIL.Image.Image    ):
  																		return np.asarray(lowerCAmelCase__		,						**self.np_array_kwargs    )
  
  										return np.asarray(lowerCAmelCase__		,						**{**default_dtype, **self.np_array_kwargs}    )
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :Dict    )     ->int:
  										# support for torch, tf, jax etc.
  										if config.TORCH_AVAILABLE and "torch" in sys.modules:
  														import torch
  
  														if isinstance(lowerCAmelCase__		,						torch.Tensor    ):
  																		return self._tensorize(data_struct.detach().cpu().numpy()[()]    )
  										if hasattr(lowerCAmelCase__		,						"__array__"    ) and not isinstance(lowerCAmelCase__		,						(np.ndarray, np.character, np.number)    ):
  														lowercase						    =			data_struct.__array__()
  										# support for nested types like struct of list of struct
  										if isinstance(lowerCAmelCase__		,						np.ndarray    ):
  														if data_struct.dtype == object:
  																		return self._consolidate([self.recursive_tensorize(lowerCAmelCase__    ) for substruct in data_struct]    )
  										if isinstance(lowerCAmelCase__		,						(list, tuple)    ):
  														return self._consolidate([self.recursive_tensorize(lowerCAmelCase__    ) for substruct in data_struct]    )
  										return self._tensorize(lowerCAmelCase__    )
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :int		,						lowerCAmelCase__       :dict    )     ->str:
  										return map_nested(self._recursive_tensorize		,						lowerCAmelCase__		,						map_list=lowerCAmelCase__    )
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :Tuple		,						lowerCAmelCase__       :pa.Table    )     ->Mapping:
  										lowercase						    =			self.numpy_arrow_extractor().extract_row(lowerCAmelCase__    )
  										lowercase						    =			self.python_features_decoder.decode_row(lowerCAmelCase__    )
  										return self.recursive_tensorize(lowerCAmelCase__    )
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :str		,						lowerCAmelCase__       :pa.Table    )     ->np.ndarray:
  										lowercase						    =			self.numpy_arrow_extractor().extract_column(lowerCAmelCase__    )
  										lowercase						    =			self.python_features_decoder.decode_column(lowerCAmelCase__		,						pa_table.column_names[0]    )
  										lowercase						    =			self.recursive_tensorize(lowerCAmelCase__    )
  										lowercase						    =			self._consolidate(lowerCAmelCase__    )
  										return column
  
  
  
  
  
  						def 					SCREAMING_SNAKE_CASE( self       :List[Any]		,						lowerCAmelCase__       :pa.Table    )     ->Mapping:
  										lowercase						    =			self.numpy_arrow_extractor().extract_batch(lowerCAmelCase__    )
  										lowercase						    =			self.python_features_decoder.decode_batch(lowerCAmelCase__    )
  										lowercase						    =			self.recursive_tensorize(lowerCAmelCase__    )
  										for column_name in batch:
  														lowercase						    =			self._consolidate(batch[column_name]    )
  										return batch
  