




import pyarrow.parquet as pq
import pytest

from datasets import Audio, Dataset, DatasetDict, Features, NamedSplit, Sequence, Value, config
from datasets.features.image import Image
from datasets.io.parquet import ParquetDatasetReader, ParquetDatasetWriter, get_writer_batch_size

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases
def 					_A	(		SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Optional[int]						):





  """simple docstring"""



  assert isinstance(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  assert dataset.num_rows == 4
  assert dataset.num_columns == 3
  assert dataset.column_names == ["col_1", "col_2", "col_3"]
  for feature, expected_dtype in expected_features.items():
    assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize("keep_in_memory"	,	[False, True]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]						):





  """simple docstring"""



  a__     : Optional[int]  =tmp_path / "cache"
  a__     : Tuple  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    a__     : List[str]  =ParquetDatasetReader(SCREAMING_SNAKE_CASE	,	cache_dir=SCREAMING_SNAKE_CASE	,	keep_in_memory=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
@pytest.mark.parametrize(
    "features"	,	[
        None,
        {"col_1": "string", "col_2": "int64", "col_3": "float64"},
        {"col_1": "string", "col_2": "string", "col_3": "string"},
        {"col_1": "int32", "col_2": "int32", "col_3": "int32"},
        {"col_1": "float32", "col_2": "float32", "col_3": "float32"},
    ]	,	)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Optional[Any]						):





  """simple docstring"""



  a__     : Optional[Any]  =tmp_path / "cache"
  a__     : Union[str, Any]  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  a__     : int  =features.copy() if features else default_expected_features
  a__     : Dict  =(
      Features({feature: Value(SCREAMING_SNAKE_CASE						) for feature, dtype in features.items()}						) if features is not None else None
  )
  a__     : Optional[Any]  =ParquetDatasetReader(SCREAMING_SNAKE_CASE	,	features=SCREAMING_SNAKE_CASE	,	cache_dir=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
@pytest.mark.parametrize("split"	,	[None, NamedSplit("train"						), "train", "test"]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       int	,	SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  a__     : Optional[int]  =tmp_path / "cache"
  a__     : int  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  a__     : int  =ParquetDatasetReader(SCREAMING_SNAKE_CASE	,	cache_dir=SCREAMING_SNAKE_CASE	,	split=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  assert dataset.split == split if split else "train"
@pytest.mark.parametrize("path_type"	,	[str, list]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       List[str]	,	SCREAMING_SNAKE_CASE					:       List[str]	,	SCREAMING_SNAKE_CASE					:       Optional[Any]						):





  """simple docstring"""



  if issubclass(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						):
    a__     : List[Any]  =parquet_path
  elif issubclass(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						):
    a__     : Optional[int]  =[parquet_path]
  a__     : Any  =tmp_path / "cache"
  a__     : List[str]  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  a__     : str  =ParquetDatasetReader(SCREAMING_SNAKE_CASE	,	cache_dir=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_dataset(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       str	,	SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       int=("train",)						):





  """simple docstring"""



  assert isinstance(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
  for split in splits:
    a__     : int  =dataset_dict[split]
    assert dataset.num_rows == 4
    assert dataset.num_columns == 3
    assert dataset.column_names == ["col_1", "col_2", "col_3"]
    for feature, expected_dtype in expected_features.items():
      assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize("keep_in_memory"	,	[False, True]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Union[str, Any]	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Union[str, Any]						):





  """simple docstring"""



  a__     : List[str]  =tmp_path / "cache"
  a__     : List[Any]  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    a__     : Dict  =ParquetDatasetReader(
        {"train": parquet_path}	,	cache_dir=SCREAMING_SNAKE_CASE	,	keep_in_memory=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_datasetdict(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
@pytest.mark.parametrize(
    "features"	,	[
        None,
        {"col_1": "string", "col_2": "int64", "col_3": "float64"},
        {"col_1": "string", "col_2": "string", "col_3": "string"},
        {"col_1": "int32", "col_2": "int32", "col_3": "int32"},
        {"col_1": "float32", "col_2": "float32", "col_3": "float32"},
    ]	,	)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[Any]	,	SCREAMING_SNAKE_CASE					:       Any	,	SCREAMING_SNAKE_CASE					:       Dict						):





  """simple docstring"""



  a__     : Optional[Any]  =tmp_path / "cache"
  a__     : List[str]  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  a__     : Union[str, Any]  =features.copy() if features else default_expected_features
  a__     : List[str]  =(
      Features({feature: Value(SCREAMING_SNAKE_CASE						) for feature, dtype in features.items()}						) if features is not None else None
  )
  a__     : Any  =ParquetDatasetReader({"train": parquet_path}	,	features=SCREAMING_SNAKE_CASE	,	cache_dir=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_datasetdict(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE						)
@pytest.mark.parametrize("split"	,	[None, NamedSplit("train"						), "train", "test"]						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       Optional[int]	,	SCREAMING_SNAKE_CASE					:       List[Any]	,	SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  if split:
    a__     : List[str]  ={split: parquet_path}
  else:
    a__     : Optional[int]  ="train"
    a__     : Any  ={"train": parquet_path, "test": parquet_path}
  a__     : Union[str, Any]  =tmp_path / "cache"
  a__     : Dict  ={"col_1": "string", "col_2": "int64", "col_3": "float64"}
  a__     : Union[str, Any]  =ParquetDatasetReader(SCREAMING_SNAKE_CASE	,	cache_dir=SCREAMING_SNAKE_CASE						).read()
  _check_parquet_datasetdict(SCREAMING_SNAKE_CASE	,	SCREAMING_SNAKE_CASE	,	splits=list(path.keys()						)						)
  assert all(dataset[split].split == split for split in path.keys()						)
def 					_A	(		SCREAMING_SNAKE_CASE					:       List[str]	,	SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  a__     : int  =ParquetDatasetWriter(SCREAMING_SNAKE_CASE	,	tmp_path / "foo.parquet"						)
  assert writer.write() > 0
  a__     : Optional[int]  =pq.ParquetFile(tmp_path / "foo.parquet"						)
  a__     : str  =pf.read()
  assert dataset.data.table == output_table
def 					_A	(		SCREAMING_SNAKE_CASE					:       List[Any]	,	SCREAMING_SNAKE_CASE					:       int						):





  """simple docstring"""



  a__     : Dict  =str(shared_datadir / "test_image_rgb.jpg"						)
  a__     : Tuple  ={"image": [image_path]}
  a__     : Any  =Features({"image": Image()}						)
  a__     : str  =Dataset.from_dict(SCREAMING_SNAKE_CASE	,	features=SCREAMING_SNAKE_CASE						)
  a__     : Dict  =ParquetDatasetWriter(SCREAMING_SNAKE_CASE	,	tmp_path / "foo.parquet"						)
  assert writer.write() > 0

  a__     : Optional[int]  =Dataset.from_parquet(str(tmp_path / "foo.parquet"						)						)
  assert dataset.features == reloaded_dataset.features

  a__     : Optional[int]  =ParquetDatasetReader(str(tmp_path / "foo.parquet"						)	,	streaming=SCREAMING_SNAKE_CASE						).read()
  assert dataset.features == reloaded_iterable_dataset.features





@pytest.mark.parametrize(
    "feature, expected"	,	[
        (Features({"foo": Value("int32"						)}						), None),
        (Features({"image": Image(), "foo": Value("int32"						)}						), config.PARQUET_ROW_GROUP_SIZE_FOR_IMAGE_DATASETS),
        (Features({"nested": Sequence(Audio()						)}						), config.PARQUET_ROW_GROUP_SIZE_FOR_AUDIO_DATASETS),
    ]	,	)
def 					_A	(		SCREAMING_SNAKE_CASE					:       str	,	SCREAMING_SNAKE_CASE					:       Tuple						):





  """simple docstring"""



  assert get_writer_batch_size(SCREAMING_SNAKE_CASE						) == expected













def 					_A	(		SCREAMING_SNAKE_CASE					:       int = 1	,	SCREAMING_SNAKE_CASE					:       int = 1_000						):





  """simple docstring"""



  a__     : Any  =1
  a__     : Any  =0

  for divide_by_number in range(SCREAMING_SNAKE_CASE	,	digit + 1						):
    a__     : list[int]  =[]
    a__     : int  =numerator
    for _ in range(1	,	digit + 1						):
      if now_divide in has_been_divided:
        if longest_list_length < len(SCREAMING_SNAKE_CASE						):
          a__     : Optional[Any]  =len(SCREAMING_SNAKE_CASE						)
          a__     : List[str]  =divide_by_number
      else:
        has_been_divided.append(SCREAMING_SNAKE_CASE						)
        a__     : List[Any]  =now_divide * 10 % divide_by_number

  return the_digit


# Tests
if __name__ == "__main__":
     import doctest

     doctest.testmod()






