



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







			'''simple docstring'''
			assert isinstance(_UpperCAmelCase							,     _UpperCAmelCase					)
			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			(  _UpperCAmelCase       :     Any							,     _UpperCAmelCase       :     Tuple							,     _UpperCAmelCase       :     Optional[Any]					)       ->				Any:







			'''simple docstring'''
			_UpperCAmelCase	 =     tmp_path / 'cache'
			_UpperCAmelCase	 =     {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
			with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
						_UpperCAmelCase	 =     JsonDatasetReader(_UpperCAmelCase							,     cache_dir=_UpperCAmelCase							,     keep_in_memory=_UpperCAmelCase					).read()
			_check_json_dataset(_UpperCAmelCase							,     _UpperCAmelCase					)





@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			(  _UpperCAmelCase       :     Dict							,     _UpperCAmelCase       :     Union[str, Any]							,     _UpperCAmelCase       :     Tuple					)       ->				Dict:







			'''simple docstring'''
			_UpperCAmelCase	 =     tmp_path / 'cache'
			_UpperCAmelCase	 =     {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
			_UpperCAmelCase	 =     features.copy() if features else default_expected_features
			_UpperCAmelCase	 =     (
			    Features({feature: Value(_UpperCAmelCase					) for feature, dtype in features.items()}					) if features is not None else None
			)
			_UpperCAmelCase	 =     JsonDatasetReader(_UpperCAmelCase							,     features=_UpperCAmelCase							,     cache_dir=_UpperCAmelCase					).read()
			_check_json_dataset(_UpperCAmelCase							,     _UpperCAmelCase					)





@pytest.mark.parametrize(
    'features'							,     [
        None,
        {'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'},
    ]							,     )
def 							A			(  _UpperCAmelCase       :     str							,     _UpperCAmelCase       :     Optional[Any]							,     _UpperCAmelCase       :     Union[str, Any]					)       ->				Union[str, Any]:







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







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







			'''simple docstring'''
			_UpperCAmelCase	 =     tmp_path / 'cache'
			_UpperCAmelCase	 =     {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
			_UpperCAmelCase	 =     JsonDatasetReader(_UpperCAmelCase							,     cache_dir=_UpperCAmelCase							,     split=_UpperCAmelCase					).read()
			_check_json_dataset(_UpperCAmelCase							,     _UpperCAmelCase					)
			assert dataset.split == split if split else "train"





@pytest.mark.parametrize('path_type'							,     [str, list]					)
def 							A			(  _UpperCAmelCase       :     List[str]							,     _UpperCAmelCase       :     str							,     _UpperCAmelCase       :     Tuple					)       ->				Tuple:







			'''simple docstring'''
			if issubclass(_UpperCAmelCase							,     _UpperCAmelCase					):
						_UpperCAmelCase	 =     jsonl_path
			elif issubclass(_UpperCAmelCase							,     _UpperCAmelCase					):
						_UpperCAmelCase	 =     [jsonl_path]
			_UpperCAmelCase	 =     tmp_path / 'cache'
			_UpperCAmelCase	 =     {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
			_UpperCAmelCase	 =     JsonDatasetReader(_UpperCAmelCase							,     cache_dir=_UpperCAmelCase					).read()
			_check_json_dataset(_UpperCAmelCase							,     _UpperCAmelCase					)





def 							A			(  _UpperCAmelCase       :     Any							,     _UpperCAmelCase       :     Any							,     _UpperCAmelCase       :     int=("train",)					)       ->				Tuple:







			'''simple docstring'''
			assert isinstance(_UpperCAmelCase							,     _UpperCAmelCase					)
			for split in splits:
						_UpperCAmelCase	 =     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			(  _UpperCAmelCase       :     str							,     _UpperCAmelCase       :     Tuple							,     _UpperCAmelCase       :     Union[str, Any]					)       ->				List[Any]:







			'''simple docstring'''
			_UpperCAmelCase	 =     tmp_path / 'cache'
			_UpperCAmelCase	 =     {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
			with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
						_UpperCAmelCase	 =     JsonDatasetReader({'train': jsonl_path}							,     cache_dir=_UpperCAmelCase							,     keep_in_memory=_UpperCAmelCase					).read()
			_check_json_datasetdict(_UpperCAmelCase							,     _UpperCAmelCase					)





@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			(  _UpperCAmelCase       :     Tuple							,     _UpperCAmelCase       :     List[Any]							,     _UpperCAmelCase       :     Any					)       ->				Optional[Any]:







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





@pytest.mark.parametrize('split'							,     [None, NamedSplit('train'					), 'train', 'test']					)
def 							A			(  _UpperCAmelCase       :     Optional[int]							,     _UpperCAmelCase       :     List[str]							,     _UpperCAmelCase       :     List[Any]					)       ->				Tuple:







			'''simple docstring'''
			if split:
						_UpperCAmelCase	 =     {split: jsonl_path}
			else:
						_UpperCAmelCase	 =     'train'
						_UpperCAmelCase	 =     {'train': jsonl_path, 'test': jsonl_path}
			_UpperCAmelCase	 =     tmp_path / 'cache'
			_UpperCAmelCase	 =     {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
			_UpperCAmelCase	 =     JsonDatasetReader(_UpperCAmelCase							,     cache_dir=_UpperCAmelCase					).read()
			_check_json_datasetdict(_UpperCAmelCase							,     _UpperCAmelCase							,     splits=list(path.keys()					)					)
			assert all(dataset[split].split == split for split in path.keys()					)





def 							A			(  _UpperCAmelCase       :     Optional[int]					)       ->				int:







			'''simple docstring'''
			return json.load(_UpperCAmelCase					)





def 							A			(  _UpperCAmelCase       :     Dict					)       ->				Tuple:







			'''simple docstring'''
			return [json.loads(_UpperCAmelCase					) for line in buffer]







class     __lowerCAmelCase					:



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



						"""simple docstring"""






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



						"""simple docstring"""






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



			@pytest.mark.parametrize('lines, load_json_function'				,	[(True, load_json_lines), (False, load_json)])
			def 					_lowerCamelCase (		self     :						Union[str, Any]				,	A     :						Tuple				,	A     :						Optional[int]				,	A     :						Any)     ->					List[Any]:



						"""simple docstring"""






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



						"""simple docstring"""






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



			def 					_lowerCamelCase (		self     :						int				,	A     :						Optional[int])     ->					Any:



						"""simple docstring"""






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



			@pytest.mark.parametrize('compression, extension'				,	[('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')])
			def 					_lowerCamelCase (		self     :						Optional[Any]				,	A     :						Union[str, Any]				,	A     :						Tuple				,	A     :						Union[str, Any]				,	A     :						str				,	A     :						Dict)     ->					Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase	 =     tmp_path_factory.mktemp('data') / F"test.json.{extension}"
						_UpperCAmelCase	 =     str(shared_datadir / F"test_file.json.{extension}")
						JsonDatasetWriter(A				,	A				,	compression=A).write()

						with fsspec.open(A				,	'rb'				,	compression='infer') as f:
									_UpperCAmelCase	 =     f.read()
						with fsspec.open(A				,	'rb'				,	compression='infer') as f:
									_UpperCAmelCase	 =     f.read()
						assert exported_content == original_content





from collections import Counter
from timeit import timeit





def 							A			(  _UpperCAmelCase       :     str = ""							,     )       ->				bool:







			'''simple docstring'''
			return sum(c % 2 for c in Counter(input_str.replace(' '							,     ''					).lower()					).values()					) < 2





def 							A			(  _UpperCAmelCase       :     str = ""					)       ->				bool:







			'''simple docstring'''
			if len(_UpperCAmelCase					) == 0:
						return True
			_UpperCAmelCase	 =     input_str.replace(' '							,     ''					).lower()
			# character_freq_dict: Stores the frequency of every character in the input string
			_UpperCAmelCase	 =     {}

			for character in lower_case_input_str:
						_UpperCAmelCase	 =     character_freq_dict.get(_UpperCAmelCase							,     0					) + 1
			_UpperCAmelCase	 =     0

			for character_count in character_freq_dict.values():
						if character_count % 2:
									odd_char += 1
			if odd_char > 1:
						return False
			return True





def 							A			(  _UpperCAmelCase       :     str = ""					)       ->				None:







			'''simple docstring'''
			print('\nFor string = '							,     _UpperCAmelCase							,     ':'					)
			print(
			    '> can_string_be_rearranged_as_palindrome_counter()'							,     '\tans ='							,     can_string_be_rearranged_as_palindrome_counter(_UpperCAmelCase					)							,     '\ttime ='							,     timeit(
			        'z.can_string_be_rearranged_as_palindrome_counter(z.check_str)'							,     setup='import __main__ as z'							,     )							,     'seconds'							,     )
			print(
			    '> can_string_be_rearranged_as_palindrome()'							,     '\tans ='							,     can_string_be_rearranged_as_palindrome(_UpperCAmelCase					)							,     '\ttime ='							,     timeit(
			        'z.can_string_be_rearranged_as_palindrome(z.check_str)'							,     setup='import __main__ as z'							,     )							,     'seconds'							,     )


if __name__ == "__main__":
							UpperCAmelCase__     = input(
							    "Enter string to determine if it can be rearranged as a palindrome or not: "
							).strip()
							benchmark(check_str)
							UpperCAmelCase__     = can_string_be_rearranged_as_palindrome_counter(check_str)
							print(f"""{check_str} can {"" if status else "not "}be rearranged as a palindrome""")
