





import io
import json

import fsspec
import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.json import JsonDatasetReader, JsonDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases





def    __UpperCamelCase      (    a,     a) ->str:
					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    __UpperCamelCase      (    a,     a,     a) ->str:
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE,     keep_in_memory=_SCREAMING_SNAKE_CASE).read()
					_check_json_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    __UpperCamelCase      (    a,     a,     a) ->List[str]:
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					lowerCamelCase__       =       features.copy() if features else default_expected_features
					lowerCamelCase__       =       (
					    Features({feature: Value(_SCREAMING_SNAKE_CASE) for feature, dtype in features.items()}) if features is not None else None
					)
					lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     features=_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE).read()
					_check_json_dataset(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)





@pytest.mark.parametrize(
    "features",     [
        None,
        {"col_3": "float64", "col_1": "string", "col_2": "int64"},
    ],     )
def    __UpperCamelCase      (    a,     a,     a) ->List[Any]:
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_3": "float64", "col_1": "string", "col_2": "int64"}
					lowerCamelCase__       =       features.copy() if features else default_expected_features
					lowerCamelCase__       =       (
					    Features({feature: Value(_SCREAMING_SNAKE_CASE) for feature, dtype in features.items()}) if features is not None else None
					)
					lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     features=_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE).read()
					assert isinstance(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)
					assert dataset.num_rows == 2
					assert dataset.num_columns == 3
					assert dataset.column_names == ["col_3", "col_1", "col_2"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype





def    __UpperCamelCase      (    a,     a) ->Tuple:
					# jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"}
					lowerCamelCase__       =       {"col_2": "int64", "col_3": "float64", "col_1": "string"}
					lowerCamelCase__       =       features.copy()
					lowerCamelCase__       =       (
					    Features({feature: Value(_SCREAMING_SNAKE_CASE) for feature, dtype in features.items()}) if features is not None else None
					)
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     features=_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE).read()
					assert isinstance(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)
					assert dataset.num_rows == 2
					assert dataset.num_columns == 3
					assert dataset.column_names == ["col_2", "col_3", "col_1"]
					for feature, expected_dtype in expected_features.items():
										assert dataset.features[feature].dtype == expected_dtype





@pytest.mark.parametrize("split",     [None, NamedSplit("train"), "train", "test"])
def    __UpperCamelCase      (    a,     a,     a) ->Optional[int]:
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE,     split=_SCREAMING_SNAKE_CASE).read()
					_check_json_dataset(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)
					assert dataset.split == split if split else "train"





@pytest.mark.parametrize("path_type",     [str, list])
def    __UpperCamelCase      (    a,     a,     a) ->List[str]:
					if issubclass(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE):
										lowerCamelCase__       =       jsonl_path
					elif issubclass(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE):
										lowerCamelCase__       =       [jsonl_path]
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE).read()
					_check_json_dataset(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)





def    __UpperCamelCase      (    a,     a,     a=("train",)) ->int:
					assert isinstance(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)
					for split in splits:
										lowerCamelCase__       =       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    __UpperCamelCase      (    a,     a,     a) ->Optional[Any]:
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
										lowerCamelCase__       =       JsonDatasetReader({"train": jsonl_path},     cache_dir=_SCREAMING_SNAKE_CASE,     keep_in_memory=_SCREAMING_SNAKE_CASE).read()
					_check_json_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    __UpperCamelCase      (    a,     a,     a) ->Any:
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					lowerCamelCase__       =       features.copy() if features else default_expected_features
					lowerCamelCase__       =       (
					    Features({feature: Value(_SCREAMING_SNAKE_CASE) for feature, dtype in features.items()}) if features is not None else None
					)
					lowerCamelCase__       =       JsonDatasetReader({"train": jsonl_path},     features=_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE).read()
					_check_json_datasetdict(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE)





@pytest.mark.parametrize("split",     [None, NamedSplit("train"), "train", "test"])
def    __UpperCamelCase      (    a,     a,     a) ->Tuple:
					if split:
										lowerCamelCase__       =       {split: jsonl_path}
					else:
										lowerCamelCase__       =       "train"
										lowerCamelCase__       =       {"train": jsonl_path, "test": jsonl_path}
					lowerCamelCase__       =       tmp_path / "cache"
					lowerCamelCase__       =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
					lowerCamelCase__       =       JsonDatasetReader(_SCREAMING_SNAKE_CASE,     cache_dir=_SCREAMING_SNAKE_CASE).read()
					_check_json_datasetdict(_SCREAMING_SNAKE_CASE,     _SCREAMING_SNAKE_CASE,     splits=list(path.keys()))
					assert all(dataset[split].split == split for split in path.keys())





def    __UpperCamelCase      (    a) ->str:
					return json.load(_SCREAMING_SNAKE_CASE)





def    __UpperCamelCase      (    a) ->List[Any]:
					return [json.loads(_SCREAMING_SNAKE_CASE) for line in buffer]







class SCREAMING_SNAKE_CASE_ :





					"""simple docstring"""


					@pytest.mark.parametrize("lines, load_json_function"	,  [(True, load_json_lines), (False, load_json)]	)
					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):







										with io.BytesIO() as buffer:
															JsonDatasetWriter(_lowerCAmelCase	,  _lowerCAmelCase	,  lines=_lowerCAmelCase	).write()
															buffer.seek(0	)
															lowerCamelCase__       =       load_json_function(_lowerCAmelCase	)
										assert isinstance(_lowerCAmelCase	,  _lowerCAmelCase	)
										assert isinstance(exported_content[0]	,  _lowerCAmelCase	)
										assert len(_lowerCAmelCase	) == 10





					@pytest.mark.parametrize(
					    "orient, container, keys, len_at"	,  [
					        ("records", list, {"tokens", "labels", "answers", "id"}, None),
					        ("split", dict, {"columns", "data"}, "data"),
					        ("index", dict, set("0123456789"	), None),
					        ("columns", dict, {"tokens", "labels", "answers", "id"}, "tokens"),
					        ("values", list, None, None),
					        ("table", dict, {"schema", "data"}, "data"),
					    ]	,  )
					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):







										with io.BytesIO() as buffer:
															JsonDatasetWriter(_lowerCAmelCase	,  _lowerCAmelCase	,  lines=_lowerCAmelCase	,  orient=_lowerCAmelCase	).write()
															buffer.seek(0	)
															lowerCamelCase__       =       load_json(_lowerCAmelCase	)
										assert isinstance(_lowerCAmelCase	,  _lowerCAmelCase	)
										if keys:
															if container is dict:
																				assert exported_content.keys() == keys
															else:
																				assert exported_content[0].keys() == keys
										else:
															assert not hasattr(_lowerCAmelCase	,  "keys"	) and not hasattr(exported_content[0]	,  "keys"	)
										if len_at:
															assert len(exported_content[len_at]	) == 10
										else:
															assert len(_lowerCAmelCase	) == 10





					@pytest.mark.parametrize("lines, load_json_function"	,  [(True, load_json_lines), (False, load_json)]	)
					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):







										with io.BytesIO() as buffer:
															JsonDatasetWriter(_lowerCAmelCase	,  _lowerCAmelCase	,  lines=_lowerCAmelCase	,  num_proc=2	).write()
															buffer.seek(0	)
															lowerCamelCase__       =       load_json_function(_lowerCAmelCase	)
										assert isinstance(_lowerCAmelCase	,  _lowerCAmelCase	)
										assert isinstance(exported_content[0]	,  _lowerCAmelCase	)
										assert len(_lowerCAmelCase	) == 10





					@pytest.mark.parametrize(
					    "orient, container, keys, len_at"	,  [
					        ("records", list, {"tokens", "labels", "answers", "id"}, None),
					        ("split", dict, {"columns", "data"}, "data"),
					        ("index", dict, set("0123456789"	), None),
					        ("columns", dict, {"tokens", "labels", "answers", "id"}, "tokens"),
					        ("values", list, None, None),
					        ("table", dict, {"schema", "data"}, "data"),
					    ]	,  )
					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):







										with io.BytesIO() as buffer:
															JsonDatasetWriter(_lowerCAmelCase	,  _lowerCAmelCase	,  lines=_lowerCAmelCase	,  orient=_lowerCAmelCase	,  num_proc=2	).write()
															buffer.seek(0	)
															lowerCamelCase__       =       load_json(_lowerCAmelCase	)
										assert isinstance(_lowerCAmelCase	,  _lowerCAmelCase	)
										if keys:
															if container is dict:
																				assert exported_content.keys() == keys
															else:
																				assert exported_content[0].keys() == keys
										else:
															assert not hasattr(_lowerCAmelCase	,  "keys"	) and not hasattr(exported_content[0]	,  "keys"	)
										if len_at:
															assert len(exported_content[len_at]	) == 10
										else:
															assert len(_lowerCAmelCase	) == 10





					def 					__magic_name__ ( self	,  _lowerCAmelCase	):







										with pytest.raises(_lowerCAmelCase	):
															with io.BytesIO() as buffer:
																				JsonDatasetWriter(_lowerCAmelCase	,  _lowerCAmelCase	,  num_proc=0	)






					@pytest.mark.parametrize("compression, extension"	,  [("gzip", "gz"), ("bz2", "bz2"), ("xz", "xz")]	)
					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       tmp_path_factory.mktemp("data"	) / F"test.json.{extension}"
										lowerCamelCase__       =       str(shared_datadir / F"test_file.json.{extension}"	)
										JsonDatasetWriter(_lowerCAmelCase	,  _lowerCAmelCase	,  compression=_lowerCAmelCase	).write()

										with fsspec.open(_lowerCAmelCase	,  "rb"	,  compression="infer"	) as f:
															lowerCamelCase__       =       f.read()
										with fsspec.open(_lowerCAmelCase	,  "rb"	,  compression="infer"	) as f:
															lowerCamelCase__       =       f.read()
										assert exported_content == original_content








from typing import TYPE_CHECKING

from ...utils import _LazyModule


A_												=     {"tokenization_byt5": ["ByT5Tokenizer"]}


if TYPE_CHECKING:
						from .tokenization_byta import ByTaTokenizer
else:
						import sys

						A_												=     _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

