




'''simple docstring'''




import os
from typing import BinaryIO, Optional, Union

import numpy as np
import pyarrow.parquet as pq

from .. import Audio, Dataset, Features, Image, NamedSplit, Value, config
from ..features.features import FeatureType, _visit
from ..formatting import query_table
from ..packaged_modules import _PACKAGED_DATASETS_MODULES
from ..packaged_modules.parquet.parquet import Parquet
from ..utils import logging
from ..utils.typing import NestedDataStructureLike, PathLike
from .abc import AbstractDatasetReader

def       _UpperCamelCase					( SCREAMING_SNAKE_CASE__	:					Union[str, Any]				):
 '''simple docstring'''





 UpperCAmelCase__								=			np.inf

 def set_batch_size(SCREAMING_SNAKE_CASE__	:					Dict				) -> None:
  nonlocal batch_size
  if isinstance(SCREAMING_SNAKE_CASE__				,  SCREAMING_SNAKE_CASE__				):
   UpperCAmelCase__								=			min(SCREAMING_SNAKE_CASE__				,  config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS				)
  elif isinstance(SCREAMING_SNAKE_CASE__				,  SCREAMING_SNAKE_CASE__				):
   UpperCAmelCase__								=			min(SCREAMING_SNAKE_CASE__				,  config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS				)
  elif isinstance(SCREAMING_SNAKE_CASE__				,  SCREAMING_SNAKE_CASE__				) and feature.dtype == "binary":
   UpperCAmelCase__								=			min(SCREAMING_SNAKE_CASE__				,  config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS				)

 _visit(SCREAMING_SNAKE_CASE__				,  SCREAMING_SNAKE_CASE__				)

 return None if batch_size is np.inf else batch_size

class   lowerCAmelCase_      (       snake_case__      ):







  '''simple docstring'''







  def __init__(  self			:		Dict  ,						_UpperCAmelCase			:		Union[str, Any]  ,						_UpperCAmelCase			:		Optional[Any] = None  ,						_UpperCAmelCase			:		Union[str, Any] = None  ,						_UpperCAmelCase			:		List[str] = None  ,						_UpperCAmelCase			:		Optional[int] = False  ,						_UpperCAmelCase			:		List[Any] = False  ,						_UpperCAmelCase			:		Optional[Any] = None  ,						**_UpperCAmelCase			:		Optional[int]  ,						):






   """simple docstring"""






   super().__init__(
       _A  ,						split=_A  ,						features=_A  ,						cache_dir=_A  ,						keep_in_memory=_A  ,						streaming=_A  ,						num_proc=_A  ,						**_A  ,						)
   UpperCAmelCase__								=			path_or_paths if isinstance(_A  ,						_A       ) else {self.split: path_or_paths}
   UpperCAmelCase__								=			_PACKAGED_DATASETS_MODULES['parquet'][1]
   UpperCAmelCase__								=			Parquet(
       cache_dir=_A  ,						data_files=_A  ,						features=_A  ,						hash=_A  ,						**_A  ,						)



  def 							SCREAMING_SNAKE_CASE__	(  self			:		Union[str, Any]       ):






   """simple docstring"""






   if self.streaming:
    UpperCAmelCase__								=			self.builder.as_streaming_dataset(split=self.split       )
   # Build regular (map-style) dataset
   else:
    UpperCAmelCase__								=			None
    UpperCAmelCase__								=			None
    UpperCAmelCase__								=			None
    UpperCAmelCase__								=			None

    self.builder.download_and_prepare(
        download_config=_A  ,						download_mode=_A  ,						verification_mode=_A  ,						base_path=_A  ,						num_proc=self.num_proc  ,						)
    UpperCAmelCase__								=			self.builder.as_dataset(
        split=self.split  ,						verification_mode=_A  ,						in_memory=self.keep_in_memory       )
   return dataset

class   lowerCAmelCase_      :







  '''simple docstring'''







  def __init__(  self			:		Optional[int]  ,						_UpperCAmelCase			:		Tuple  ,						_UpperCAmelCase			:		Dict  ,						_UpperCAmelCase			:		Tuple = None  ,						**_UpperCAmelCase			:		Dict  ,						):






   """simple docstring"""






   UpperCAmelCase__								=			dataset
   UpperCAmelCase__								=			path_or_buf
   UpperCAmelCase__								=			batch_size or get_writer_batch_size(dataset.features       )
   UpperCAmelCase__								=			parquet_writer_kwargs


  def 							SCREAMING_SNAKE_CASE__	(  self			:		Optional[Any]       ):






   """simple docstring"""






   UpperCAmelCase__								=			self.batch_size if self.batch_size else config.DEFAULT_MAX_BATCH_SIZE

   if isinstance(self.path_or_buf  ,						(str, bytes, os.PathLike)       ):
    with open(self.path_or_buf  ,						"""wb+"""       ) as buffer:
     UpperCAmelCase__								=			self._write(file_obj=_A  ,						batch_size=_A  ,						**self.parquet_writer_kwargs       )
   else:
    UpperCAmelCase__								=			self._write(file_obj=self.path_or_buf  ,						batch_size=_A  ,						**self.parquet_writer_kwargs       )
   return written



  def 							SCREAMING_SNAKE_CASE__	(  self			:		Union[str, Any]  ,						_UpperCAmelCase			:		Dict  ,						_UpperCAmelCase			:		Dict  ,						**_UpperCAmelCase			:		Optional[Any]       ):






   """simple docstring"""






   UpperCAmelCase__								=			0
   UpperCAmelCase__								=			parquet_writer_kwargs.pop("""path_or_buf"""  ,						_A       )
   UpperCAmelCase__								=			self.dataset.features.arrow_schema

   UpperCAmelCase__								=			pq.ParquetWriter(_A  ,						schema=_A  ,						**_A       )

   for offset in logging.tqdm(
       range(0  ,						len(self.dataset       )  ,						_A       )  ,						unit="""ba"""  ,						disable=not logging.is_progress_bar_enabled()  ,						desc="""Creating parquet from Arrow format"""  ,						):
    UpperCAmelCase__								=			query_table(
        table=self.dataset._data  ,						key=slice(_A  ,						offset + batch_size       )  ,						indices=self.dataset._indices if self.dataset._indices is not None else None  ,						)
    writer.write_table(_A       )
    written += batch.nbytes
   writer.close()
   return written







from __future__ import annotations







def  _SCREAMING_SNAKE_CASE				(   a  )      ->      int:
					if not nums:
										return 0
					__A	:						Optional[int]       =				nums[0]
					__A	:						str       =				0
					for num in nums[1:]:
										__A    ,				__A	:						Tuple       =				(
										    max_excluding + num,
										    max(a					,						a  ),
										)
					return max(a					,						a  )


if __name__ == "__main__":
				import doctest

				doctest.testmod()




