



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      lowerCAmelCase__		(   lowerCamelCase_		:		Optional[int]	,lowerCamelCase_		:		List[str]):





				'''simple docstring'''

				assert isinstance(a__	,a__)
				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__		(   lowerCamelCase_		:		Any	,lowerCamelCase_		:		str	,lowerCamelCase_		:		Union[str, Any]):





				'''simple docstring'''

				lowerCAmelCase__					:    str												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    Optional[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():
								lowerCAmelCase__					:    str												=					JsonDatasetReader(a__	,cache_dir=a__	,keep_in_memory=a__).read()
				_check_json_dataset(a__	,a__)






@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__		(   lowerCamelCase_		:		str	,lowerCamelCase_		:		List[str]	,lowerCamelCase_		:		Union[str, Any]):





				'''simple docstring'''

				lowerCAmelCase__					:    Optional[Any]												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    str												=					{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
				lowerCAmelCase__					:    List[str]												=					features.copy() if features else default_expected_features
				lowerCAmelCase__					:    int												=					(
				    Features({feature: Value(a__) for feature, dtype in features.items()}) if features is not None else None
				)
				lowerCAmelCase__					:    Union[str, Any]												=					JsonDatasetReader(a__	,features=a__	,cache_dir=a__).read()
				_check_json_dataset(a__	,a__)






@pytest.mark.parametrize(
    '''features'''	,[
        None,
        {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''},
    ]	,)
def      lowerCAmelCase__		(   lowerCamelCase_		:		List[Any]	,lowerCamelCase_		:		List[Any]	,lowerCamelCase_		:		Tuple):





				'''simple docstring'''

				lowerCAmelCase__					:    Any												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    Union[str, Any]												=					{'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''}
				lowerCAmelCase__					:    Union[str, Any]												=					features.copy() if features else default_expected_features
				lowerCAmelCase__					:    Optional[Any]												=					(
				    Features({feature: Value(a__) for feature, dtype in features.items()}) if features is not None else None
				)
				lowerCAmelCase__					:    Any												=					JsonDatasetReader(a__	,features=a__	,cache_dir=a__).read()
				assert isinstance(a__	,a__)
				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      lowerCAmelCase__		(   lowerCamelCase_		:		Optional[int]	,lowerCamelCase_		:		Optional[Any]):





				'''simple docstring'''

				lowerCAmelCase__					:    str												=					{'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''}
				lowerCAmelCase__					:    Optional[Any]												=					features.copy()
				lowerCAmelCase__					:    Tuple												=					(
				    Features({feature: Value(a__) for feature, dtype in features.items()}) if features is not None else None
				)
				lowerCAmelCase__					:    List[str]												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    str												=					JsonDatasetReader(a__	,features=a__	,cache_dir=a__).read()
				assert isinstance(a__	,a__)
				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      lowerCAmelCase__		(   lowerCamelCase_		:		List[Any]	,lowerCamelCase_		:		Optional[int]	,lowerCamelCase_		:		Union[str, Any]):





				'''simple docstring'''

				lowerCAmelCase__					:    Dict												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    int												=					{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
				lowerCAmelCase__					:    List[Any]												=					JsonDatasetReader(a__	,cache_dir=a__	,split=a__).read()
				_check_json_dataset(a__	,a__)
				assert dataset.split == split if split else "train"






@pytest.mark.parametrize('''path_type'''	,[str, list])
def      lowerCAmelCase__		(   lowerCamelCase_		:		List[str]	,lowerCamelCase_		:		List[Any]	,lowerCamelCase_		:		str):





				'''simple docstring'''

				if issubclass(a__	,a__):
								lowerCAmelCase__					:    List[str]												=					jsonl_path
				elif issubclass(a__	,a__):
								lowerCAmelCase__					:    Optional[int]												=					[jsonl_path]
				lowerCAmelCase__					:    List[str]												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    int												=					{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
				lowerCAmelCase__					:    str												=					JsonDatasetReader(a__	,cache_dir=a__).read()
				_check_json_dataset(a__	,a__)






def      lowerCAmelCase__		(   lowerCamelCase_		:		Any	,lowerCamelCase_		:		Optional[Any]	,lowerCamelCase_		:		str=("train",)):





				'''simple docstring'''

				assert isinstance(a__	,a__)
				for split in splits:
								lowerCAmelCase__					:    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__		(   lowerCamelCase_		:		Union[str, Any]	,lowerCamelCase_		:		Tuple	,lowerCamelCase_		:		Union[str, Any]):





				'''simple docstring'''

				lowerCAmelCase__					:    Any												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    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():
								lowerCAmelCase__					:    Any												=					JsonDatasetReader({'''train''': jsonl_path}	,cache_dir=a__	,keep_in_memory=a__).read()
				_check_json_datasetdict(a__	,a__)






@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__		(   lowerCamelCase_		:		int	,lowerCamelCase_		:		Union[str, Any]	,lowerCamelCase_		:		str):





				'''simple docstring'''

				lowerCAmelCase__					:    Union[str, Any]												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    Any												=					{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
				lowerCAmelCase__					:    Any												=					features.copy() if features else default_expected_features
				lowerCAmelCase__					:    str												=					(
				    Features({feature: Value(a__) for feature, dtype in features.items()}) if features is not None else None
				)
				lowerCAmelCase__					:    Tuple												=					JsonDatasetReader({'''train''': jsonl_path}	,features=a__	,cache_dir=a__).read()
				_check_json_datasetdict(a__	,a__)






@pytest.mark.parametrize('''split'''	,[None, NamedSplit('''train'''), '''train''', '''test'''])
def      lowerCAmelCase__		(   lowerCamelCase_		:		Optional[Any]	,lowerCamelCase_		:		Optional[int]	,lowerCamelCase_		:		int):





				'''simple docstring'''

				if split:
								lowerCAmelCase__					:    List[Any]												=					{split: jsonl_path}
				else:
								lowerCAmelCase__					:    Tuple												=					'''train'''
								lowerCAmelCase__					:    List[Any]												=					{'''train''': jsonl_path, '''test''': jsonl_path}
				lowerCAmelCase__					:    Union[str, Any]												=					tmp_path / '''cache'''
				lowerCAmelCase__					:    Any												=					{'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
				lowerCAmelCase__					:    Tuple												=					JsonDatasetReader(a__	,cache_dir=a__).read()
				_check_json_datasetdict(a__	,a__	,splits=list(path.keys()))
				assert all(dataset[split].split == split for split in path.keys())






def      lowerCAmelCase__		(   lowerCamelCase_		:		Any):





				'''simple docstring'''

				return json.load(a__)






def      lowerCAmelCase__		(   lowerCamelCase_		:		Tuple):





				'''simple docstring'''

				return [json.loads(a__) for line in buffer]



class 	lowerCamelCase__      :




		'''simple docstring'''



		@pytest.mark.parametrize('''lines, load_json_function'''		,[(True, load_json_lines), (False, load_json)]			)
		def 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Union[str, Any]:

						"""simple docstring"""






						with io.BytesIO() as buffer:
										JsonDatasetWriter(__lowerCAmelCase		,__lowerCAmelCase		,lines=__lowerCAmelCase			).write()
										buffer.seek(0			)
										lowerCAmelCase__					:    Optional[Any]												=					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 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Optional[int]:

						"""simple docstring"""






						with io.BytesIO() as buffer:
										JsonDatasetWriter(__lowerCAmelCase		,__lowerCAmelCase		,lines=__lowerCAmelCase		,orient=__lowerCAmelCase			).write()
										buffer.seek(0			)
										lowerCAmelCase__					:    Optional[int]												=					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 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      Tuple:

						"""simple docstring"""






						with io.BytesIO() as buffer:
										JsonDatasetWriter(__lowerCAmelCase		,__lowerCAmelCase		,lines=__lowerCAmelCase		,num_proc=2			).write()
										buffer.seek(0			)
										lowerCAmelCase__					:    Optional[int]												=					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 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      List[Any]:

						"""simple docstring"""






						with io.BytesIO() as buffer:
										JsonDatasetWriter(__lowerCAmelCase		,__lowerCAmelCase		,lines=__lowerCAmelCase		,orient=__lowerCAmelCase		,num_proc=2			).write()
										buffer.seek(0			)
										lowerCAmelCase__					:    Any												=					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 		lowerCAmelCase__				(self		,__lowerCamelCase			)					->      int:

						"""simple docstring"""






						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 		lowerCAmelCase__				(self		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase		,__lowerCamelCase			)					->      List[str]:

						"""simple docstring"""






						lowerCAmelCase__					:    str												=					tmp_path_factory.mktemp('''data'''			) / f"""test.json.{extension}"""
						lowerCAmelCase__					:    Dict												=					str(shared_datadir / f"""test_file.json.{extension}"""			)
						JsonDatasetWriter(__lowerCAmelCase		,__lowerCAmelCase		,compression=__lowerCAmelCase			).write()

						with fsspec.open(__lowerCAmelCase		,'''rb'''		,compression='''infer'''			) as f:
										lowerCAmelCase__					:    int												=					f.read()
						with fsspec.open(__lowerCAmelCase		,'''rb'''		,compression='''infer'''			) as f:
										lowerCAmelCase__					:    Union[str, Any]												=					f.read()
						assert exported_content == original_content






def      lowerCAmelCase__		(   lowerCamelCase_		:		str = "The quick brown fox jumps over the lazy dog"	,):





				'''simple docstring'''

				lowerCAmelCase__					:    Any												=					set()

				# Replace all the whitespace in our sentence
				lowerCAmelCase__					:    List[Any]												=					input_str.replace(''' '''	,'''''')
				for alpha in input_str:
								if "a" <= alpha.lower() <= "z":
												frequency.add(alpha.lower())
				return len(lowerCamelCase_) == 26






def      lowerCAmelCase__		(   lowerCamelCase_		:		str = "The quick brown fox jumps over the lazy dog"	,):





				'''simple docstring'''

				lowerCAmelCase__					:    List[str]												=					[False] * 26
				for char in input_str:
								if char.islower():
												lowerCAmelCase__					:    Union[str, Any]												=					True
								elif char.isupper():
												lowerCAmelCase__					:    str												=					True
				return all(lowerCamelCase_)






def      lowerCAmelCase__		(   lowerCamelCase_		:		str = "The quick brown fox jumps over the lazy dog"	,):





				'''simple docstring'''

				return len({char for char in input_str.lower() if char.isalpha()}) == 26






def      lowerCAmelCase__		(   ):





				'''simple docstring'''

				from timeit import timeit

				lowerCAmelCase__					:    Optional[Any]												=					'''from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest'''
				print(timeit('''is_pangram()'''	,setup=lowerCamelCase_))
				print(timeit('''is_pangram_faster()'''	,setup=lowerCamelCase_))
				print(timeit('''is_pangram_fastest()'''	,setup=lowerCamelCase_))
				# 5.348480500048026, 2.6477354579837993, 1.8470395830227062
				# 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
							import doctest

							doctest.testmod()
							benchmark()

