





import dataclasses
import re
from dataclasses import dataclass
from functools import total_ordering
from typing import Optional, Union


_UpperCAmelCase	:     List[str]		       = re.compile(R"""^(?P<major>\d+)""" R"""\.(?P<minor>\d+)""" R"""\.(?P<patch>\d+)$""")







@total_ordering
@dataclass
class 				lowercase       :
				__SCREAMING_SNAKE_CASE      :				str
				__SCREAMING_SNAKE_CASE      :				Optional[str]     =    None
				__SCREAMING_SNAKE_CASE      :				Optional[Union[str, int]]     =    None
				__SCREAMING_SNAKE_CASE      :				Optional[Union[str, int]]     =    None
				__SCREAMING_SNAKE_CASE      :				Optional[Union[str, int]]     =    None




				def 					a							(      self ):
											snake_case_           =					_str_to_version_tuple(self.version_str )




				def __repr__(      self ):
											return F'''{self.tuple[0]}.{self.tuple[1]}.{self.tuple[2]}'''




				@property
				def 					a							(      self ):
											return self.major, self.minor, self.patch




				def 					a							(      self   ,     snake_case ):
											if isinstance(lowercase__   ,     lowercase__ ):
																		return Version(lowercase__ )
											elif isinstance(lowercase__   ,     lowercase__ ):
																		return other
											raise TypeError(F'''{other} (type {type(lowercase__ )}) cannot be compared to version.''' )




				def __eq__(      self   ,     snake_case ):
											try:
																		snake_case_           =					self._validate_operand(lowercase__ )
											except (TypeError, ValueError):
																		return False
											else:
																		return self.tuple == other.tuple




				def __lt__(      self   ,     snake_case ):
											snake_case_           =					self._validate_operand(lowercase__ )
											return self.tuple < other.tuple




				def __hash__(      self ):
											return hash(_version_tuple_to_str(self.tuple ) )




				@classmethod
				def 					a							(      cls   ,     snake_case ):
											snake_case_           =					{f.name for f in dataclasses.fields(cls )}
											return cls(**{k: v for k, v in dic.items() if k in field_names} )




				def 					a							(      self ):
											return self.version_str





def       __lowerCamelCase			(				UpperCamelCase__   ):







							'''simple docstring'''






							snake_case_           =					_VERSION_REG.match(SCREAMING_SNAKE_CASE__   )
							if not res:
														raise ValueError(F'''Invalid version \'{version_str}\'. Format should be x.y.z with {{x,y,z}} being digits.'''   )
							return tuple(int(SCREAMING_SNAKE_CASE__   ) for v in [res.group('major'   ), res.group('minor'   ), res.group('patch'   )]   )





def       __lowerCamelCase			(				UpperCamelCase__   ):







							'''simple docstring'''






							return ".".join(str(SCREAMING_SNAKE_CASE__   ) for v in version_tuple   )







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       __lowerCamelCase			(				UpperCamelCase__   ):







							'''simple docstring'''






							snake_case_           =					np.inf

							def set_batch_size(UpperCamelCase__   ) -> None:
														nonlocal batch_size
														if isinstance(UpperCamelCase__     ,							UpperCamelCase__   ):
																					snake_case_           =					min(UpperCamelCase__     ,							config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS   )
														elif isinstance(UpperCamelCase__     ,							UpperCamelCase__   ):
																					snake_case_           =					min(UpperCamelCase__     ,							config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS   )
														elif isinstance(UpperCamelCase__     ,							UpperCamelCase__   ) and feature.dtype == "binary":
																					snake_case_           =					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 				lowercase       ( lowercase_    ):




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




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

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







class 				lowercase       :




				def __init__(      self   ,     snake_case   ,     snake_case   ,     snake_case = None   ,     **snake_case   ,     ):
											snake_case_           =					dataset
											snake_case_           =					path_or_buf
											snake_case_           =					batch_size or get_writer_batch_size(dataset.features )
											snake_case_           =					parquet_writer_kwargs




				def 					a							(      self ):
											snake_case_           =					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_           =					self._write(file_obj=snake_case   ,     batch_size=snake_case   ,     **self.parquet_writer_kwargs )
											else:
																		snake_case_           =					self._write(file_obj=self.path_or_buf   ,     batch_size=snake_case   ,     **self.parquet_writer_kwargs )
											return written




				def 					a							(      self   ,     snake_case   ,     snake_case   ,     **snake_case ):
											snake_case_           =					0
											snake_case_           =					parquet_writer_kwargs.pop('path_or_buf'   ,     snake_case )
											snake_case_           =					self.dataset.features.arrow_schema

											snake_case_           =					pq.ParquetWriter(snake_case   ,     schema=snake_case   ,     **snake_case )

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




