import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict




@pytest.mark.parametrize(
    "files"   , [
        ["full:README.md", "dataset_infos.json"],
        ["empty:README.md", "dataset_infos.json"],
        ["dataset_infos.json"],
        ["full:README.md"],
    ]   , )
def       _snake_case	(   lowerCAmelCase							:   Tuple   , lowerCAmelCase							:   Dict	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Tuple				      =    tmp_path_factory.mktemp("dset_infos_dir"	)
		if "full:README.md" in files:
				with open(dataset_infos_dir / "README.md"   , "w"	) as f:
						f.write("---\ndataset_info:\n  dataset_size: 42\n---"	)
		if "empty:README.md" in files:
				with open(dataset_infos_dir / "README.md"   , "w"	) as f:
						f.write(""	)
    # we want to support dataset_infos.json for backward compatibility
		if "dataset_infos.json" in files:
				with open(dataset_infos_dir / "dataset_infos.json"   , "w"	) as f:
						f.write("{\"default\": {\"dataset_size\": 42}}"	)
		SCREAMING_SNAKE_CASE_  :		str				      =    DatasetInfosDict.from_directory(__A	)
		assert dataset_infos
		assert dataset_infos["default"].dataset_size == 4_2




@pytest.mark.parametrize(
    "dataset_info"   , [
        DatasetInfo(),
        DatasetInfo(
            description="foo"   , features=Features({"a": Value("int32"	)}	)   , builder_name="builder"   , config_name="config"   , version="1.0.0"   , splits=[{"name": "train"}]   , download_size=4_2   , ),
    ]   , )
def       _snake_case	(   lowerCAmelCase							:   Optional[int]   , lowerCAmelCase							:   DatasetInfo	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Any				      =    str(__A	)
		dataset_info.write_to_directory(__A	)
		SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    DatasetInfo.from_directory(__A	)
		assert dataset_info == reloaded
		assert os.path.exists(os.path.join(__A   , "dataset_info.json"	)	)




def       _snake_case	(   ):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		List[str]				      =    DatasetInfo(
		    description="foo"   , citation="bar"   , homepage="https://foo.bar"   , license="CC0"   , features=Features({"a": Value("int32"	)}	)   , post_processed={}   , supervised_keys=()   , task_templates=[]   , builder_name="builder"   , config_name="config"   , version="1.0.0"   , splits=[{"name": "train", "num_examples": 4_2}]   , download_checksums={}   , download_size=1_3_3_7   , post_processing_size=4_4_2   , dataset_size=1_2_3_4   , size_in_bytes=1_3_3_7 + 4_4_2 + 1_2_3_4   , )
		SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    dataset_info._to_yaml_dict()
		assert sorted(__A	) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML	)
		for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
				assert key in dataset_info_yaml_dict
				assert isinstance(dataset_info_yaml_dict[key]   , (list, dict, int, str)	)
		SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    yaml.safe_dump(__A	)
		SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    yaml.safe_load(__A	)
		assert dataset_info_yaml_dict == reloaded




def       _snake_case	(   ):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		List[str]				      =    DatasetInfo()
		SCREAMING_SNAKE_CASE_  :		int				      =    dataset_info._to_yaml_dict()
		assert dataset_info_yaml_dict == {}







@pytest.mark.parametrize(
    "dataset_infos_dict"   , [
        DatasetInfosDict(),
        DatasetInfosDict({"default": DatasetInfo()}	),
        DatasetInfosDict({"my_config_name": DatasetInfo()}	),
        DatasetInfosDict(
            {
                "default": DatasetInfo(
                    description="foo"   , features=Features({"a": Value("int32"	)}	)   , builder_name="builder"   , config_name="config"   , version="1.0.0"   , splits=[{"name": "train"}]   , download_size=4_2   , )
            }	),
        DatasetInfosDict(
            {
                "v1": DatasetInfo(dataset_size=4_2	),
                "v2": DatasetInfo(dataset_size=1_3_3_7	),
            }	),
    ]   , )
def       _snake_case	(   lowerCAmelCase							:   Optional[Any]   , lowerCAmelCase							:   DatasetInfosDict	):
		"""simple docstring"""







		SCREAMING_SNAKE_CASE_  :		Tuple				      =    str(__A	)
		dataset_infos_dict.write_to_directory(__A	)
		SCREAMING_SNAKE_CASE_  :		Tuple				      =    DatasetInfosDict.from_directory(__A	)

		# the config_name of the dataset_infos_dict take over the attribute
		for config_name, dataset_info in dataset_infos_dict.items():
				SCREAMING_SNAKE_CASE_  :		Tuple				      =    config_name
				# the yaml representation doesn't include fields like description or citation
				# so we just test that we can recover what we can from the yaml
				SCREAMING_SNAKE_CASE_  :		str				      =    DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict()	)
		assert dataset_infos_dict == reloaded

		if dataset_infos_dict:
				assert os.path.exists(os.path.join(__A   , "README.md"	)	)






from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


snake_case_ : Any    		=       logging.get_logger(__name__)


snake_case_ : Optional[int]    		=       TypeVar("DatasetType", Dataset, IterableDataset)


def    A						(__A   :    List[DatasetType]   ,				__A   :    Optional[List[float]] = None   ,				__A   :    Optional[int] = None   ,				__A   :    Optional[DatasetInfo] = None   ,				__A   :    Optional[NamedSplit] = None   ,				__A   :    Literal["first_exhausted", "all_exhausted"] = "first_exhausted"   ,				)   ->      DatasetType:







     """simple docstring"""


     from .arrow_dataset import Dataset
     from .iterable_dataset import IterableDataset

     if not datasets:
          raise ValueError('''Unable to interleave an empty list of datasets.'''       )
     for i, dataset in enumerate(__A       ):
          if not isinstance(__A   ,				(Dataset, IterableDataset)       ):
               if isinstance(__A   ,				(DatasetDict, IterableDatasetDict)       ):
                    if not dataset:
                         raise ValueError(
                             F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """
                             '''is an empty dataset dictionary.'''       )
                    raise ValueError(
                        F"""Dataset at position {i} has at least one split: {list(__A       )}\n"""
                        F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__A       )       )}']"""       )
               raise ValueError(
                   F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__A       ).__name__}."""       )
          if i == 0:
               UpperCAmelCase_     ,       UpperCAmelCase_          =							(
                   (Dataset, IterableDataset) if isinstance(__A   ,				__A       ) else (IterableDataset, Dataset)
               )
          elif not isinstance(__A   ,				__A       ):
               raise ValueError(
                   F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."""       )
     if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
          raise ValueError(F"""{stopping_strategy} is not supported. Please enter a valid stopping_strategy."""       )
     if dataset_type is Dataset:
          return _interleave_map_style_datasets(
              __A   ,				__A   ,				__A   ,				info=__A   ,				split=__A   ,				stopping_strategy=__A       )
     else:
          return _interleave_iterable_datasets(
              __A   ,				__A   ,				__A   ,				info=__A   ,				split=__A   ,				stopping_strategy=__A       )


def    A						(__A   :    List[DatasetType]   ,				__A   :    Optional[DatasetInfo] = None   ,				__A   :    Optional[NamedSplit] = None   ,				__A   :    int = 0   ,				)   ->      DatasetType:







     """simple docstring"""


     if not dsets:
          raise ValueError('''Unable to concatenate an empty list of datasets.'''       )
     for i, dataset in enumerate(__A       ):
          if not isinstance(__A   ,				(Dataset, IterableDataset)       ):
               if isinstance(__A   ,				(DatasetDict, IterableDatasetDict)       ):
                    if not dataset:
                         raise ValueError(
                             F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} """
                             '''is an empty dataset dictionary.'''       )
                    raise ValueError(
                        F"""Dataset at position {i} has at least one split: {list(__A       )}\n"""
                        F"""Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__A       )       )}']"""       )
               raise ValueError(
                   F"""Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__A       ).__name__}."""       )
          if i == 0:
               UpperCAmelCase_     ,       UpperCAmelCase_          =							(
                   (Dataset, IterableDataset) if isinstance(__A   ,				__A       ) else (IterableDataset, Dataset)
               )
          elif not isinstance(__A   ,				__A       ):
               raise ValueError(
                   F"""Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."""       )
     if dataset_type is Dataset:
          return _concatenate_map_style_datasets(__A   ,				info=__A   ,				split=__A   ,				axis=__A       )
     else:
          return _concatenate_iterable_datasets(__A   ,				info=__A   ,				split=__A   ,				axis=__A       )






