

"""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    (   UpperCAmelCase_   :    List[str]  )							->      Optional[int]:

	'''simple docstring'''





	__snake_case      :							List[Any]						=			np.inf

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

	_visit(_UpperCAmelCase      ,							_UpperCAmelCase  )

	return None if batch_size is np.inf else batch_size

class 		UpperCamelCase      (   SCREAMING_SNAKE_CASE__						):



				def __init__(self    :				Union[str, Any]     ,  _A    :				NestedDataStructureLike[PathLike]     ,  _A    :				Optional[NamedSplit] = None     ,  _A    :				Optional[Features] = None     ,  _A    :				str = None     ,  _A    :				bool = False     ,  _A    :				bool = False     ,  _A    :				Optional[int] = None     ,  **_A    :				int     ,  )   ->     List[Any]:
					super().__init__(
					    _A     ,  split=_A     ,  features=_A     ,  cache_dir=_A     ,  keep_in_memory=_A     ,  streaming=_A     ,  num_proc=_A     ,  **_A     ,  )
					__snake_case      :							str						=			path_or_paths if isinstance(_A     ,  _A) else {self.split: path_or_paths}
					__snake_case      :							Tuple						=			_PACKAGED_DATASETS_MODULES['parquet'][1]
					__snake_case      :							Optional[int]						=			Parquet(
					    cache_dir=_A     ,  data_files=_A     ,  features=_A     ,  hash=_A     ,  **_A     ,  )






				def  _lowercase					(self    :				Optional[int])   ->     Union[str, Any]:
					# Build iterable dataset
					if self.streaming:
						__snake_case      :							Union[str, Any]						=			self.builder.as_streaming_dataset(split=self.split)
					# Build regular (map-style) dataset
					else:
						__snake_case      :							Tuple						=			None
						__snake_case      :							Union[str, Any]						=			None
						__snake_case      :							List[Any]						=			None
						__snake_case      :							int						=			None

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







class 		UpperCamelCase      :



				def __init__(self    :				Dict     ,  _A    :				Dataset     ,  _A    :				Union[PathLike, BinaryIO]     ,  _A    :				Optional[int] = None     ,  **_A    :				Optional[Any]     ,  )   ->     Optional[Any]:
					__snake_case      :							List[Any]						=			dataset
					__snake_case      :							int						=			path_or_buf
					__snake_case      :							Optional[Any]						=			batch_size or get_writer_batch_size(dataset.features)
					__snake_case      :							Optional[Any]						=			parquet_writer_kwargs



				def  _lowercase					(self    :				Union[str, Any])   ->     int:
					__snake_case      :							Union[str, Any]						=			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:
							__snake_case      :							int						=			self._write(file_obj=_A     ,  batch_size=_A     ,  **self.parquet_writer_kwargs)
					else:
						__snake_case      :							List[Any]						=			self._write(file_obj=self.path_or_buf     ,  batch_size=_A     ,  **self.parquet_writer_kwargs)
					return written






				def  _lowercase					(self    :				int     ,  _A    :				BinaryIO     ,  _A    :				int     ,  **_A    :				Union[str, Any])   ->     int:
					__snake_case      :							Optional[Any]						=			0
					__snake_case      :							int						=			parquet_writer_kwargs.pop('path_or_buf'     ,  _A)
					__snake_case      :							List[str]						=			self.dataset.features.arrow_schema

					__snake_case      :							int						=			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'     ,  ):
						__snake_case      :							Tuple						=			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






"""simple docstring"""







def   __UpperCAmelCase    (   UpperCAmelCase_   :    int      ,							UpperCAmelCase_   :    int  )							->      int:

	'''simple docstring'''





	while a != 0:
		__snake_case		,    __snake_case      :							Union[str, Any]						=			b % a, a
	return b

def   __UpperCAmelCase    (   UpperCAmelCase_   :    int      ,							UpperCAmelCase_   :    int  )							->      int:

	'''simple docstring'''





	if gcd(UpperCAmelCase_      ,							UpperCAmelCase_  ) != 1:
		__snake_case      :							Union[str, Any]						=			F"mod inverse of {a!r} and {m!r} does not exist"
		raise ValueError(UpperCAmelCase_  )
	__snake_case		,    __snake_case		,    __snake_case      :							List[str]						=			1, 0, a
	__snake_case		,    __snake_case		,    __snake_case      :							Dict						=			0, 1, m
	while va != 0:
		__snake_case      :							List[str]						=			ua // va
		__snake_case		,    __snake_case		,    __snake_case		,    __snake_case		,    __snake_case		,    __snake_case      :							List[str]						=			(ua - q * va), (ua - q * va), (ua - q * va), va, va, va
	return ua % m





