code: '''simple docstring'''
  
  
  
  
  
  import multiprocessing
  import os
  from typing import BinaryIO, Optional, Union
  
  from .. import Dataset, Features, NamedSplit, config
  from ..formatting import query_table
  from ..packaged_modules.csv.csv import Csv
  from ..utils import logging
  from ..utils.typing import NestedDataStructureLike, PathLike
  from .abc import AbstractDatasetReader
  
  
  class _lowercase					(       _UpperCAmelCase						):
  
  
  
  
  
  							"""simple docstring"""
  
  
  
  
  							def __init__(    self     ,   UpperCAmelCase     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   UpperCAmelCase = False     ,   UpperCAmelCase = False     ,   UpperCAmelCase = None     ,   **UpperCAmelCase     ,   ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														super().__init__(
  														    UpperCAmelCase     ,   split=UpperCAmelCase     ,   features=UpperCAmelCase     ,   cache_dir=UpperCAmelCase     ,   keep_in_memory=UpperCAmelCase     ,   streaming=UpperCAmelCase     ,   num_proc=UpperCAmelCase     ,   **UpperCAmelCase     ,   )
  														_lowercase				=	path_or_paths if isinstance(UpperCAmelCase     ,   UpperCAmelCase  ) else {self.split: path_or_paths}
  														_lowercase				=	Csv(
  														    cache_dir=UpperCAmelCase     ,   data_files=UpperCAmelCase     ,   features=UpperCAmelCase     ,   **UpperCAmelCase     ,   )
  
  
  
  							def     _UpperCAmelCase       (    self  ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														if self.streaming:
  																					_lowercase				=	self.builder.as_streaming_dataset(split=self.split  )
  														# Build regular (map-style) dataset
  														else:
  																					_lowercase				=	None
  																					_lowercase				=	None
  																					_lowercase				=	None
  																					_lowercase				=	None
  
  																					self.builder.download_and_prepare(
  																					    download_config=UpperCAmelCase     ,   download_mode=UpperCAmelCase     ,   verification_mode=UpperCAmelCase     ,   base_path=UpperCAmelCase     ,   num_proc=self.num_proc     ,   )
  																					_lowercase				=	self.builder.as_dataset(
  																					    split=self.split     ,   verification_mode=UpperCAmelCase     ,   in_memory=self.keep_in_memory  )
  														return dataset
  
  
  
  
  
  
  class _lowercase					:
  
  
  
  
  
  							"""simple docstring"""
  
  
  
  
  							def __init__(    self     ,   UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase = None     ,   UpperCAmelCase = None     ,   **UpperCAmelCase     ,   ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														if num_proc is not None and num_proc <= 0:
  																					raise ValueError(F'''num_proc {num_proc} must be an integer > 0.'''  )
  
  														_lowercase				=	dataset
  														_lowercase				=	path_or_buf
  														_lowercase				=	batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
  														_lowercase				=	num_proc
  														_lowercase				=	"""utf-8"""
  														_lowercase				=	to_csv_kwargs
  
  
  
  							def     _UpperCAmelCase       (    self  ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														_lowercase				=	self.to_csv_kwargs.pop("""path_or_buf"""     ,   UpperCAmelCase  )
  														_lowercase				=	self.to_csv_kwargs.pop("""header"""     ,   UpperCAmelCase  )
  														_lowercase				=	self.to_csv_kwargs.pop("""index"""     ,   UpperCAmelCase  )
  
  														if isinstance(self.path_or_buf     ,   (str, bytes, os.PathLike)  ):
  																					with open(self.path_or_buf     ,   """wb+"""  ) as buffer:
  																												_lowercase				=	self._write(file_obj=UpperCAmelCase     ,   header=UpperCAmelCase     ,   index=UpperCAmelCase     ,   **self.to_csv_kwargs  )
  														else:
  																					_lowercase				=	self._write(file_obj=self.path_or_buf     ,   header=UpperCAmelCase     ,   index=UpperCAmelCase     ,   **self.to_csv_kwargs  )
  														return written
  
  
  
  							def     _UpperCAmelCase       (    self     ,   UpperCAmelCase  ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														_lowercase				,       _lowercase				,       _lowercase				,       _lowercase				=	args
  
  														_lowercase				=	query_table(
  														    table=self.dataset.data     ,   key=slice(UpperCAmelCase     ,   offset + self.batch_size  )     ,   indices=self.dataset._indices     ,   )
  														_lowercase				=	batch.to_pandas().to_csv(
  														    path_or_buf=UpperCAmelCase     ,   header=header if (offset == 0) else False     ,   index=UpperCAmelCase     ,   **UpperCAmelCase  )
  														return csv_str.encode(self.encoding  )
  
  
  
  							def     _UpperCAmelCase       (    self     ,   UpperCAmelCase     ,   UpperCAmelCase     ,   UpperCAmelCase     ,   **UpperCAmelCase  ):
  
  
  
  
  
  
  
  														'''simple docstring'''
  
  
  
  
  
  
  														_lowercase				=	0
  
  														if self.num_proc is None or self.num_proc == 1:
  																					for offset in logging.tqdm(
  																					    range(0     ,   len(self.dataset  )     ,   self.batch_size  )     ,   unit="""ba"""     ,   disable=not logging.is_progress_bar_enabled()     ,   desc="""Creating CSV from Arrow format"""     ,   ):
  																												_lowercase				=	self._batch_csv((offset, header, index, to_csv_kwargs)  )
  																												written += file_obj.write(UpperCAmelCase  )
  
  														else:
  																					_lowercase				,       _lowercase				=	len(self.dataset  ), self.batch_size
  																					with multiprocessing.Pool(self.num_proc  ) as pool:
  																												for csv_str in logging.tqdm(
  																												    pool.imap(
  																												        self._batch_csv     ,   [(offset, header, index, to_csv_kwargs) for offset in range(0     ,   UpperCAmelCase     ,   UpperCAmelCase  )]     ,   )     ,   total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size     ,   unit="""ba"""     ,   disable=not logging.is_progress_bar_enabled()     ,   desc="""Creating CSV from Arrow format"""     ,   ):
  																																			written += file_obj.write(UpperCAmelCase  )
  
  														return written
  
  
  
style_context: import math
  from timeit import timeit
  
  
  def 					__lowerCAmelCase			(  _A					):
  
  
  
  							"""simple docstring"""
  
  
  							_lowercase				=	0
  							_lowercase				=	abs(_A					)
  							while True:
  														_lowercase				=	n // 10
  														digits += 1
  														if n == 0:
  																					break
  							return digits
  
  
  def 					__lowerCAmelCase			(  _A					):
  
  
  
  							"""simple docstring"""
  
  
  							return 1 if n == 0 else math.floor(math.log(abs(_A					) ,10					) + 1					)
  
  
  def 					__lowerCAmelCase			(  _A					):
  
  
  
  							"""simple docstring"""
  
  
  							return len(str(abs(_A					)					)					)
  
  
  def 					__lowerCAmelCase			(  ):
  
  
  
  							"""simple docstring"""
  
  
  							from collections.abc import Callable
  
  							def benchmark_a_function(_A ,_A					) -> None:
  														_lowercase				=	f'''{func.__name__}({value})'''
  														_lowercase				=	timeit(f'''__main__.{call}''' ,setup="""import __main__"""					)
  														print(f'''{call}: {func(_A					)} -- {timing} seconds'''					)
  
  							for value in (262_144, 1_125_899_906_842_624, 1_267_650_600_228_229_401_496_703_205_376):
  														for func in (num_digits, num_digits_fast, num_digits_faster):
  																					benchmark_a_function(_A ,_A					)
  														print()
  
  
  if __name__ == "__main__":
  				import doctest
  
  				doctest.testmod()
  				benchmark()
  
  
  