






"""simple docstring"""



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      _lowerCAmelCase		(							UpperCAmelCase__       :					Optional[Any],						UpperCAmelCase__       :					int      )		->Any:
						assert isinstance(__lowerCAmelCase,						__lowerCAmelCase      )
						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      _lowerCAmelCase		(							UpperCAmelCase__       :					Dict,						UpperCAmelCase__       :					int,						UpperCAmelCase__       :					Tuple      )		->Any:
						A__						:  List[Any]		  =  tmp_path / """cache"""
						A__						:  str		  =  {"""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__						:  Optional[int]		  =  ParquetDatasetReader(__lowerCAmelCase,						cache_dir=__lowerCAmelCase,						keep_in_memory=__lowerCAmelCase      ).read()
						_check_parquet_dataset(__lowerCAmelCase,						__lowerCAmelCase      )




@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      _lowerCAmelCase		(							UpperCAmelCase__       :					Tuple,						UpperCAmelCase__       :					List[str],						UpperCAmelCase__       :					List[Any]      )		->List[Any]:
						A__						:  int		  =  tmp_path / """cache"""
						A__						:  List[str]		  =  {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
						A__						:  int		  =  features.copy() if features else default_expected_features
						A__						:  List[str]		  =  (
						    Features({feature: Value(__lowerCAmelCase      ) for feature, dtype in features.items()}      ) if features is not None else None
						)
						A__						:  List[Any]		  =  ParquetDatasetReader(__lowerCAmelCase,						features=__lowerCAmelCase,						cache_dir=__lowerCAmelCase      ).read()
						_check_parquet_dataset(__lowerCAmelCase,						__lowerCAmelCase      )




@pytest.mark.parametrize("""split""",						[None, NamedSplit("""train"""      ), """train""", """test"""]      )
def      _lowerCAmelCase		(							UpperCAmelCase__       :					Optional[int],						UpperCAmelCase__       :					List[str],						UpperCAmelCase__       :					Any      )		->str:
						A__						:  Union[str, Any]		  =  tmp_path / """cache"""
						A__						:  Optional[int]		  =  {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
						A__						:  Optional[int]		  =  ParquetDatasetReader(__lowerCAmelCase,						cache_dir=__lowerCAmelCase,						split=__lowerCAmelCase      ).read()
						_check_parquet_dataset(__lowerCAmelCase,						__lowerCAmelCase      )
						assert dataset.split == split if split else "train"




@pytest.mark.parametrize("""path_type""",						[str, list]      )
def      _lowerCAmelCase		(							UpperCAmelCase__       :					str,						UpperCAmelCase__       :					Optional[int],						UpperCAmelCase__       :					Union[str, Any]      )		->Tuple:
						if issubclass(__lowerCAmelCase,						__lowerCAmelCase      ):
												A__						:  Dict		  =  parquet_path
						elif issubclass(__lowerCAmelCase,						__lowerCAmelCase      ):
												A__						:  int		  =  [parquet_path]
						A__						:  List[str]		  =  tmp_path / """cache"""
						A__						:  List[Any]		  =  {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
						A__						:  str		  =  ParquetDatasetReader(__lowerCAmelCase,						cache_dir=__lowerCAmelCase      ).read()
						_check_parquet_dataset(__lowerCAmelCase,						__lowerCAmelCase      )




def      _lowerCAmelCase		(							UpperCAmelCase__       :					Any,						UpperCAmelCase__       :					List[str],						UpperCAmelCase__       :					Union[str, Any]=("train",)      )		->Dict:
						assert isinstance(__lowerCAmelCase,						__lowerCAmelCase      )
						for split in splits:
												A__						:  List[Any]		  =  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      _lowerCAmelCase		(							UpperCAmelCase__       :					Any,						UpperCAmelCase__       :					Dict,						UpperCAmelCase__       :					Tuple      )		->str:
						A__						:  List[Any]		  =  tmp_path / """cache"""
						A__						:  int		  =  {"""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__						:  str		  =  ParquetDatasetReader(
												    {"""train""": parquet_path},						cache_dir=__lowerCAmelCase,						keep_in_memory=__lowerCAmelCase      ).read()
						_check_parquet_datasetdict(__lowerCAmelCase,						__lowerCAmelCase      )




@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      _lowerCAmelCase		(							UpperCAmelCase__       :					str,						UpperCAmelCase__       :					Tuple,						UpperCAmelCase__       :					Dict      )		->Union[str, Any]:
						A__						:  Dict		  =  tmp_path / """cache"""
						A__						:  List[str]		  =  {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
						A__						:  List[Any]		  =  features.copy() if features else default_expected_features
						A__						:  Tuple		  =  (
						    Features({feature: Value(__lowerCAmelCase      ) for feature, dtype in features.items()}      ) if features is not None else None
						)
						A__						:  int		  =  ParquetDatasetReader({"""train""": parquet_path},						features=__lowerCAmelCase,						cache_dir=__lowerCAmelCase      ).read()
						_check_parquet_datasetdict(__lowerCAmelCase,						__lowerCAmelCase      )




@pytest.mark.parametrize("""split""",						[None, NamedSplit("""train"""      ), """train""", """test"""]      )
def      _lowerCAmelCase		(							UpperCAmelCase__       :					int,						UpperCAmelCase__       :					str,						UpperCAmelCase__       :					Tuple      )		->List[str]:
						if split:
												A__						:  Optional[Any]		  =  {split: parquet_path}
						else:
												A__						:  Union[str, Any]		  =  """train"""
												A__						:  str		  =  {"""train""": parquet_path, """test""": parquet_path}
						A__						:  Any		  =  tmp_path / """cache"""
						A__						:  str		  =  {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
						A__						:  Optional[Any]		  =  ParquetDatasetReader(__lowerCAmelCase,						cache_dir=__lowerCAmelCase      ).read()
						_check_parquet_datasetdict(__lowerCAmelCase,						__lowerCAmelCase,						splits=list(path.keys()      )      )
						assert all(dataset[split].split == split for split in path.keys()      )




def      _lowerCAmelCase		(							UpperCAmelCase__       :					List[Any],						UpperCAmelCase__       :					str      )		->int:
						A__						:  List[str]		  =  ParquetDatasetWriter(__lowerCAmelCase,						tmp_path / """foo.parquet"""      )
						assert writer.write() > 0
						A__						:  Tuple		  =  pq.ParquetFile(tmp_path / """foo.parquet"""      )
						A__						:  List[str]		  =  pf.read()
						assert dataset.data.table == output_table




def      _lowerCAmelCase		(							UpperCAmelCase__       :					List[Any],						UpperCAmelCase__       :					int      )		->Any:
						A__						:  Tuple		  =  str(shared_datadir / """test_image_rgb.jpg"""      )
						A__						:  List[str]		  =  {"""image""": [image_path]}
						A__						:  str		  =  Features({"""image""": Image()}      )
						A__						:  List[Any]		  =  Dataset.from_dict(__lowerCAmelCase,						features=__lowerCAmelCase      )
						A__						:  str		  =  ParquetDatasetWriter(__lowerCAmelCase,						tmp_path / """foo.parquet"""      )
						assert writer.write() > 0

						A__						:  int		  =  Dataset.from_parquet(str(tmp_path / """foo.parquet"""      )      )
						assert dataset.features == reloaded_dataset.features

						A__						:  str		  =  ParquetDatasetReader(str(tmp_path / """foo.parquet"""      ),						streaming=__lowerCAmelCase      ).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      _lowerCAmelCase		(							UpperCAmelCase__       :					str,						UpperCAmelCase__       :					str      )		->Optional[Any]:
						assert get_writer_batch_size(__lowerCAmelCase      ) == expected








"""simple docstring"""



from timeit import timeit




def      _lowerCAmelCase		(							UpperCAmelCase__       :					int      )		->int:
						if number < 0:
												raise ValueError("""the value of input must not be negative"""      )
						A__						:  Optional[int]		  =  0
						while number:
												number &= number - 1
												result += 1
						return result




def      _lowerCAmelCase		(							UpperCAmelCase__       :					int      )		->int:
						if number < 0:
												raise ValueError("""the value of input must not be negative"""      )
						A__						:  Tuple		  =  0
						while number:
												if number % 2 == 1:
																		result += 1
												number >>= 1
						return result




def      _lowerCAmelCase		(							)		->None:

						def do_benchmark(UpperCAmelCase__       :					int      ) -> None:
												A__						:  Optional[int]		  =  """import __main__ as z"""
												print(f'Benchmark when {number = }:'      )
												print(f'{get_set_bits_count_using_modulo_operator(UpperCAmelCase__      ) = }'      )
												A__						:  Any		  =  timeit("""z.get_set_bits_count_using_modulo_operator(25)""",						setup=UpperCAmelCase__      )
												print(f'timeit() runs in {timing} seconds'      )
												print(f'{get_set_bits_count_using_brian_kernighans_algorithm(UpperCAmelCase__      ) = }'      )
												A__						:  List[str]		  =  timeit(
												    """z.get_set_bits_count_using_brian_kernighans_algorithm(25)""",						setup=UpperCAmelCase__,						)
												print(f'timeit() runs in {timing} seconds'      )

						for number in (2_5, 3_7, 5_8, 0):
												do_benchmark(UpperCAmelCase__      )
												print()


if __name__ == "__main__":
						import doctest

						doctest.testmod()
						benchmark()






