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					(	UpperCamelCase     :				int			,       UpperCamelCase     :				Tuple							)   ->    int:
		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 	lowerCamelCase					(	UpperCamelCase     :				List[Any]			,       UpperCamelCase     :				Tuple			,       UpperCamelCase     :				int							)   ->    Dict:
		_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(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 	lowerCamelCase					(	UpperCamelCase     :				Tuple			,       UpperCamelCase     :				str			,       UpperCamelCase     :				Optional[int]							)   ->    List[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(UpperCamelCase							) for feature, dtype in features.items()}							) if features is not None else None
		)
		_lowerCamelCase				   =  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 	lowerCamelCase					(	UpperCamelCase     :				List[str]			,       UpperCamelCase     :				List[Any]			,       UpperCamelCase     :				str							)   ->    Optional[int]:
		_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(UpperCamelCase							) for feature, dtype in features.items()}							) if features is not None else None
		)
		_lowerCamelCase				   =  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 	lowerCamelCase					(	UpperCamelCase     :				List[str]			,       UpperCamelCase     :				str							)   ->    Optional[Any]:
		# 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(UpperCamelCase							) for feature, dtype in features.items()}							) if features is not None else None
		)
		_lowerCamelCase				   =  tmp_path / 'cache'
		_lowerCamelCase				   =  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 	lowerCamelCase					(	UpperCamelCase     :				Optional[int]			,       UpperCamelCase     :				Dict			,       UpperCamelCase     :				Tuple							)   ->    Union[str, Any]:
		_lowerCamelCase				   =  tmp_path / 'cache'
		_lowerCamelCase				   =  {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
		_lowerCamelCase				   =  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 	lowerCamelCase					(	UpperCamelCase     :				Tuple			,       UpperCamelCase     :				Any			,       UpperCamelCase     :				Any							)   ->    Dict:
		if issubclass(UpperCamelCase			,       UpperCamelCase							):
				_lowerCamelCase				   =  jsonl_path
		elif issubclass(UpperCamelCase			,       UpperCamelCase							):
				_lowerCamelCase				   =  [jsonl_path]
		_lowerCamelCase				   =  tmp_path / 'cache'
		_lowerCamelCase				   =  {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
		_lowerCamelCase				   =  JsonDatasetReader(UpperCamelCase			,       cache_dir=UpperCamelCase							).read()
		_check_json_dataset(UpperCamelCase			,       UpperCamelCase							)


def 	lowerCamelCase					(	UpperCamelCase     :				Dict			,       UpperCamelCase     :				Union[str, Any]			,       UpperCamelCase     :				str=("train",)							)   ->    int:
		assert isinstance(UpperCamelCase			,       UpperCamelCase							)
		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 	lowerCamelCase					(	UpperCamelCase     :				Optional[int]			,       UpperCamelCase     :				Optional[Any]			,       UpperCamelCase     :				Optional[int]							)   ->    int:
		_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=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 	lowerCamelCase					(	UpperCamelCase     :				Dict			,       UpperCamelCase     :				Union[str, Any]			,       UpperCamelCase     :				List[Any]							)   ->    int:
		_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(UpperCamelCase							) for feature, dtype in features.items()}							) if features is not None else None
		)
		_lowerCamelCase				   =  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 	lowerCamelCase					(	UpperCamelCase     :				str			,       UpperCamelCase     :				Union[str, Any]			,       UpperCamelCase     :				str							)   ->    Union[str, Any]:
		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(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 	lowerCamelCase					(	UpperCamelCase     :				str							)   ->    Tuple:
		return json.load(UpperCamelCase							)


def 	lowerCamelCase					(	UpperCamelCase     :				List[Any]							)   ->    List[Any]:
		return [json.loads(UpperCamelCase							) for line in buffer]





class     lowerCAmelCase__     :


		'''simple docstring'''






		@pytest.mark.parametrize('lines, load_json_function'     ,  [(True, load_json_lines), (False, load_json)]		)
		def   _snake_case       (       self     :     List[Any]     ,  snake_case__     :     Any     ,  snake_case__     :     Tuple     ,  snake_case__     :     int		)      ->	Union[str, Any]:
				with io.BytesIO() as buffer:
						JsonDatasetWriter(snake_case__     ,  snake_case__     ,  lines=snake_case__		).write()
						buffer.seek(0		)
						_lowerCamelCase				   =  load_json_function(snake_case__		)
				assert isinstance(snake_case__     ,  snake_case__		)
				assert isinstance(exported_content[0]     ,  snake_case__		)
				assert len(snake_case__		) == 1_0
		@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   _snake_case       (       self     :     str     ,  snake_case__     :     Union[str, Any]     ,  snake_case__     :     str     ,  snake_case__     :     Dict     ,  snake_case__     :     int     ,  snake_case__     :     Optional[Any]		)      ->	Union[str, Any]:
				with io.BytesIO() as buffer:
						JsonDatasetWriter(snake_case__     ,  snake_case__     ,  lines=snake_case__     ,  orient=snake_case__		).write()
						buffer.seek(0		)
						_lowerCamelCase				   =  load_json(snake_case__		)
				assert isinstance(snake_case__     ,  snake_case__		)
				if keys:
						if container is dict:
								assert exported_content.keys() == keys
						else:
								assert exported_content[0].keys() == keys
				else:
						assert not hasattr(snake_case__     ,  'keys'		) and not hasattr(exported_content[0]     ,  'keys'		)
				if len_at:
						assert len(exported_content[len_at]		) == 1_0
				else:
						assert len(snake_case__		) == 1_0
		@pytest.mark.parametrize('lines, load_json_function'     ,  [(True, load_json_lines), (False, load_json)]		)
		def   _snake_case       (       self     :     str     ,  snake_case__     :     str     ,  snake_case__     :     Dict     ,  snake_case__     :     int		)      ->	List[Any]:
				with io.BytesIO() as buffer:
						JsonDatasetWriter(snake_case__     ,  snake_case__     ,  lines=snake_case__     ,  num_proc=2		).write()
						buffer.seek(0		)
						_lowerCamelCase				   =  load_json_function(snake_case__		)
				assert isinstance(snake_case__     ,  snake_case__		)
				assert isinstance(exported_content[0]     ,  snake_case__		)
				assert len(snake_case__		) == 1_0
		@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   _snake_case       (       self     :     Optional[int]     ,  snake_case__     :     Optional[int]     ,  snake_case__     :     Optional[int]     ,  snake_case__     :     List[Any]     ,  snake_case__     :     List[str]     ,  snake_case__     :     Union[str, Any]		)      ->	Dict:
				with io.BytesIO() as buffer:
						JsonDatasetWriter(snake_case__     ,  snake_case__     ,  lines=snake_case__     ,  orient=snake_case__     ,  num_proc=2		).write()
						buffer.seek(0		)
						_lowerCamelCase				   =  load_json(snake_case__		)
				assert isinstance(snake_case__     ,  snake_case__		)
				if keys:
						if container is dict:
								assert exported_content.keys() == keys
						else:
								assert exported_content[0].keys() == keys
				else:
						assert not hasattr(snake_case__     ,  'keys'		) and not hasattr(exported_content[0]     ,  'keys'		)
				if len_at:
						assert len(exported_content[len_at]		) == 1_0
				else:
						assert len(snake_case__		) == 1_0
		def   _snake_case       (       self     :     str     ,  snake_case__     :     int		)      ->	Tuple:
				with pytest.raises(snake_case__		):
						with io.BytesIO() as buffer:
								JsonDatasetWriter(snake_case__     ,  snake_case__     ,  num_proc=0		)



		@pytest.mark.parametrize('compression, extension'     ,  [('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')]		)
		def   _snake_case       (       self     :     List[Any]     ,  snake_case__     :     Any     ,  snake_case__     :     int     ,  snake_case__     :     Dict     ,  snake_case__     :     Dict     ,  snake_case__     :     List[Any]		)      ->	List[Any]:
				_lowerCamelCase				   =  tmp_path_factory.mktemp('data'		) / f"""test.json.{extension}"""
				_lowerCamelCase				   =  str(shared_datadir / f"""test_file.json.{extension}"""		)
				JsonDatasetWriter(snake_case__     ,  snake_case__     ,  compression=snake_case__		).write()

				with fsspec.open(snake_case__     ,  'rb'     ,  compression='infer'		) as f:
						_lowerCamelCase				   =  f.read()
				with fsspec.open(snake_case__     ,  'rb'     ,  compression='infer'		) as f:
						_lowerCamelCase				   =  f.read()
				assert exported_content == original_content
def 	lowerCamelCase					(	UpperCamelCase     :				int							)   ->    bool:
		_lowerCamelCase				   =  (1 + 24 * n) ** 0.5
		return ((1 + root) / 6) % 1 == 0


def 	lowerCamelCase					(	UpperCamelCase     :				int = 50_00							)   ->    int:
		_lowerCamelCase				   =  [(i * (3 * i - 1)) // 2 for i in range(1			,       UpperCamelCase							)]
		for i, pentagonal_i in enumerate(UpperCamelCase							):
				for j in range(UpperCamelCase			,       len(UpperCamelCase							)							):
						_lowerCamelCase				   =  pentagonal_nums[j]
						_lowerCamelCase				   =  pentagonal_i + pentagonal_j
						_lowerCamelCase				   =  pentagonal_j - pentagonal_i
						if is_pentagonal(UpperCamelCase							) and is_pentagonal(UpperCamelCase							):
								return b

		return -1


if __name__ == "__main__":
				print(F'''{solution() = }''')