





'''simple docstring'''






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 					_a						(       _lowerCamelCase    ,	_lowerCamelCase ) ->				Any:



			"""simple docstring"""


			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 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				Optional[Any]:



			"""simple docstring"""


			__snake_case					:      str							      =     tmp_path / """cache"""
			__snake_case					:      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():
						__snake_case					:      Tuple							      =     JsonDatasetReader(_lowerCamelCase    ,	cache_dir=_lowerCamelCase    ,	keep_in_memory=_lowerCamelCase ).read()
			_check_json_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 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				int:



			"""simple docstring"""


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







@pytest.mark.parametrize(
    """features"""    ,	[
        None,
        {"""col_3""": """float64""", """col_1""": """string""", """col_2""": """int64"""},
    ]    ,	)
def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				Dict:



			"""simple docstring"""


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



			"""simple docstring"""


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



			"""simple docstring"""


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







@pytest.mark.parametrize("""path_type"""    ,	[str, list] )
def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				Dict:



			"""simple docstring"""


			if issubclass(_lowerCamelCase    ,	_lowerCamelCase ):
						__snake_case					:      Optional[int]							      =     jsonl_path
			elif issubclass(_lowerCamelCase    ,	_lowerCamelCase ):
						__snake_case					:      Any							      =     [jsonl_path]
			__snake_case					:      Optional[int]							      =     tmp_path / """cache"""
			__snake_case					:      Tuple							      =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
			__snake_case					:      Tuple							      =     JsonDatasetReader(_lowerCamelCase    ,	cache_dir=_lowerCamelCase ).read()
			_check_json_dataset(_lowerCamelCase    ,	_lowerCamelCase )







def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase=("train",) ) ->				Tuple:



			"""simple docstring"""


			assert isinstance(_lowerCamelCase    ,	_lowerCamelCase )
			for split in splits:
						__snake_case					:      Optional[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						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				Union[str, Any]:



			"""simple docstring"""


			__snake_case					:      List[Any]							      =     tmp_path / """cache"""
			__snake_case					:      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():
						__snake_case					:      Optional[Any]							      =     JsonDatasetReader({"""train""": jsonl_path}    ,	cache_dir=_lowerCamelCase    ,	keep_in_memory=_lowerCamelCase ).read()
			_check_json_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 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				Union[str, Any]:



			"""simple docstring"""


			__snake_case					:      int							      =     tmp_path / """cache"""
			__snake_case					:      int							      =     {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
			__snake_case					:      int							      =     features.copy() if features else default_expected_features
			__snake_case					:      Any							      =     (
			    Features({feature: Value(_lowerCamelCase ) for feature, dtype in features.items()} ) if features is not None else None
			)
			__snake_case					:      int							      =     JsonDatasetReader({"""train""": jsonl_path}    ,	features=_lowerCamelCase    ,	cache_dir=_lowerCamelCase ).read()
			_check_json_datasetdict(_lowerCamelCase    ,	_lowerCamelCase )







@pytest.mark.parametrize("""split"""    ,	[None, NamedSplit("""train""" ), """train""", """test"""] )
def 					_a						(       _lowerCamelCase    ,	_lowerCamelCase    ,	_lowerCamelCase ) ->				List[str]:



			"""simple docstring"""


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







def 					_a						(       _lowerCamelCase ) ->				List[Any]:



			"""simple docstring"""


			return json.load(_lowerCamelCase )







def 					_a						(       _lowerCamelCase ) ->				Dict:



			"""simple docstring"""


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




class  _A :

		@pytest.mark.parametrize("""lines, load_json_function"""  ,      [(True, load_json_lines), (False, load_json)]			)
		def 	lowercase__     (   self					:   Optional[Any]  ,      __magic_name__					:   List[Any]  ,      __magic_name__					:   List[str]  ,      __magic_name__					:   Optional[int]			)					->    Tuple:


					"""simple docstring"""

					with io.BytesIO() as buffer:
								JsonDatasetWriter(__magic_name__  ,      __magic_name__  ,      lines=__magic_name__			).write()
								buffer.seek(0			)
								__snake_case					:      Any							      =     load_json_function(__magic_name__			)
					assert isinstance(__magic_name__  ,      __magic_name__			)
					assert isinstance(exported_content[0]  ,      __magic_name__			)
					assert len(__magic_name__			) == 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 	lowercase__     (   self					:   Optional[Any]  ,      __magic_name__					:   List[Any]  ,      __magic_name__					:   Dict  ,      __magic_name__					:   Union[str, Any]  ,      __magic_name__					:   Any  ,      __magic_name__					:   Tuple			)					->    Tuple:


					"""simple docstring"""

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

		@pytest.mark.parametrize("""lines, load_json_function"""  ,      [(True, load_json_lines), (False, load_json)]			)
		def 	lowercase__     (   self					:   Union[str, Any]  ,      __magic_name__					:   List[Any]  ,      __magic_name__					:   Tuple  ,      __magic_name__					:   Dict			)					->    Tuple:


					"""simple docstring"""

					with io.BytesIO() as buffer:
								JsonDatasetWriter(__magic_name__  ,      __magic_name__  ,      lines=__magic_name__  ,      num_proc=2			).write()
								buffer.seek(0			)
								__snake_case					:      int							      =     load_json_function(__magic_name__			)
					assert isinstance(__magic_name__  ,      __magic_name__			)
					assert isinstance(exported_content[0]  ,      __magic_name__			)
					assert len(__magic_name__			) == 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 	lowercase__     (   self					:   Optional[int]  ,      __magic_name__					:   List[Any]  ,      __magic_name__					:   List[Any]  ,      __magic_name__					:   str  ,      __magic_name__					:   Optional[int]  ,      __magic_name__					:   Optional[int]			)					->    Optional[int]:


					"""simple docstring"""

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

		def 	lowercase__     (   self					:   List[Any]  ,      __magic_name__					:   List[str]			)					->    str:


					"""simple docstring"""

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

		@pytest.mark.parametrize("""compression, extension"""  ,      [("""gzip""", """gz"""), ("""bz2""", """bz2"""), ("""xz""", """xz""")]			)
		def 	lowercase__     (   self					:   Union[str, Any]  ,      __magic_name__					:   Optional[Any]  ,      __magic_name__					:   Optional[int]  ,      __magic_name__					:   Optional[Any]  ,      __magic_name__					:   Optional[Any]  ,      __magic_name__					:   Union[str, Any]			)					->    Tuple:


					"""simple docstring"""

					__snake_case					:      Optional[Any]							      =     tmp_path_factory.mktemp("""data"""			) / f'''test.json.{extension}'''
					__snake_case					:      Union[str, Any]							      =     str(shared_datadir / f'''test_file.json.{extension}'''			)
					JsonDatasetWriter(__magic_name__  ,      __magic_name__  ,      compression=__magic_name__			).write()

					with fsspec.open(__magic_name__  ,      """rb"""  ,      compression="""infer"""			) as f:
								__snake_case					:      List[Any]							      =     f.read()
					with fsspec.open(__magic_name__  ,      """rb"""  ,      compression="""infer"""			) as f:
								__snake_case					:      Optional[Any]							      =     f.read()
					assert exported_content == original_content









'''simple docstring'''






def 					_a						(       _lowerCamelCase ) ->				int:



			"""simple docstring"""


			if not isinstance(_lowerCamelCase    ,	_lowerCamelCase ):
						raise TypeError("""only integers accepted as input""" )
			else:
						__snake_case					:      List[Any]							      =     str(abs(_lowerCamelCase ) )
						__snake_case					:      Union[str, Any]							      =     [list(_lowerCamelCase ) for char in range(len(_lowerCamelCase ) )]
						for index in range(len(_lowerCamelCase ) ):
									num_transpositions[index].pop(_lowerCamelCase )
						return max(
						    int("""""".join(list(_lowerCamelCase ) ) ) for transposition in num_transpositions )


if __name__ == "__main__":
	__import__("doctest").testmod()


