


'''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 			a      (			__a   )  ->	Optional[int]:



					'''simple docstring'''






					UpperCamelCase__   :str												= np.inf

					def set_batch_size(__a   ) -> None:
										nonlocal batch_size
										if isinstance(__a ,					__a   ):
															UpperCamelCase__   :List[Any]												= min(__a ,					config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS   )
										elif isinstance(__a ,					__a   ):
															UpperCamelCase__   :int												= min(__a ,					config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS   )
										elif isinstance(__a ,					__a   ) and feature.dtype == "binary":
															UpperCamelCase__   :int												= min(__a ,					config.PARQUET_ROW_GROUP_SIZE_FOR_BINARY_DATASETS   )

					_visit(__a ,					__a   )

					return None if batch_size is np.inf else batch_size
class    lowercase	(							A__					):

		"""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_    ,   )
							UpperCamelCase__   :Any												= path_or_paths if isinstance(UpperCamelCase_    ,   UpperCamelCase_							) else {self.split: path_or_paths}
							UpperCamelCase__   :List[Any]												= _PACKAGED_DATASETS_MODULES['''parquet'''][1]
							UpperCamelCase__   :Union[str, Any]												= Parquet(
							    cache_dir=UpperCamelCase_    ,   data_files=UpperCamelCase_    ,   features=UpperCamelCase_    ,   hash=UpperCamelCase_    ,   **UpperCamelCase_    ,   )




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							if self.streaming:
												UpperCamelCase__   :List[Any]												= self.builder.as_streaming_dataset(split=self.split							)
							# Build regular (map-style) dataset
							else:
												UpperCamelCase__   :Any												= None
												UpperCamelCase__   :List[Any]												= None
												UpperCamelCase__   :List[str]												= None
												UpperCamelCase__   :str												= None

												self.builder.download_and_prepare(
												    download_config=UpperCamelCase_    ,   download_mode=UpperCamelCase_    ,   verification_mode=UpperCamelCase_    ,   base_path=UpperCamelCase_    ,   num_proc=self.num_proc    ,   )
												UpperCamelCase__   :List[Any]												= 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_    ,   ):

							'''simple docstring'''


							UpperCamelCase__   :int												= dataset
							UpperCamelCase__   :Optional[int]												= path_or_buf
							UpperCamelCase__   :Optional[Any]												= batch_size or get_writer_batch_size(dataset.features							)
							UpperCamelCase__   :int												= parquet_writer_kwargs




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							UpperCamelCase__   :int												= 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__   :Any												= self._write(file_obj=UpperCamelCase_    ,   batch_size=UpperCamelCase_    ,   **self.parquet_writer_kwargs							)
							else:
												UpperCamelCase__   :Dict												= self._write(file_obj=self.path_or_buf    ,   batch_size=UpperCamelCase_    ,   **self.parquet_writer_kwargs							)
							return written




		def     lowerCAmelCase__				(		self    ,   UpperCamelCase_    ,   UpperCamelCase_    ,   **UpperCamelCase_							):

							'''simple docstring'''


							UpperCamelCase__   :Tuple												= 0
							UpperCamelCase__   :Any												= parquet_writer_kwargs.pop('''path_or_buf'''    ,   UpperCamelCase_							)
							UpperCamelCase__   :Dict												= self.dataset.features.arrow_schema

							UpperCamelCase__   :List[Any]												= pq.ParquetWriter(UpperCamelCase_    ,   schema=UpperCamelCase_    ,   **UpperCamelCase_							)

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



'''simple docstring'''
from typing import Callable, Optional

from .. import Features
from ..packaged_modules.generator.generator import Generator
from .abc import AbstractDatasetInputStream
class    lowercase	(							A__					):

		"""simple docstring"""

		def __init__(		self    ,   UpperCamelCase_    ,   UpperCamelCase_ = None    ,   UpperCamelCase_ = None    ,   UpperCamelCase_ = False    ,   UpperCamelCase_ = False    ,   UpperCamelCase_ = None    ,   UpperCamelCase_ = None    ,   **UpperCamelCase_    ,   ):

							'''simple docstring'''


							super().__init__(
							    features=UpperCamelCase_    ,   cache_dir=UpperCamelCase_    ,   keep_in_memory=UpperCamelCase_    ,   streaming=UpperCamelCase_    ,   num_proc=UpperCamelCase_    ,   **UpperCamelCase_    ,   )
							UpperCamelCase__   :Any												= Generator(
							    cache_dir=UpperCamelCase_    ,   features=UpperCamelCase_    ,   generator=UpperCamelCase_    ,   gen_kwargs=UpperCamelCase_    ,   **UpperCamelCase_    ,   )




		def     lowerCAmelCase__				(		self							):

							'''simple docstring'''


							if self.streaming:
												UpperCamelCase__   :Optional[Any]												= self.builder.as_streaming_dataset(split='''train'''							)
							# Build regular (map-style) dataset
							else:
												UpperCamelCase__   :Optional[int]												= None
												UpperCamelCase__   :int												= None
												UpperCamelCase__   :Any												= None
												UpperCamelCase__   :Any												= None

												self.builder.download_and_prepare(
												    download_config=UpperCamelCase_    ,   download_mode=UpperCamelCase_    ,   verification_mode=UpperCamelCase_    ,   base_path=UpperCamelCase_    ,   num_proc=self.num_proc    ,   )
												UpperCamelCase__   :List[Any]												= self.builder.as_dataset(
												    split='''train'''    ,   verification_mode=UpperCamelCase_    ,   in_memory=self.keep_in_memory							)
							return dataset