

'''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 					lowerCamelCase_     (							UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     str	)						->				str:




	'''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 					lowerCamelCase_     (							UpperCAmelCase_							:     int							,       UpperCAmelCase_							:     Optional[Any]							,       UpperCAmelCase_							:     Tuple	)						->				Dict:




	'''simple docstring'''




	_UpperCamelCase   :		Any			=						tmp_path / 'cache'
	_UpperCamelCase   :		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():
		_UpperCamelCase   :		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 					lowerCamelCase_     (							UpperCAmelCase_							:     str							,       UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     List[str]	)						->				Any:




	'''simple docstring'''




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




	'''simple docstring'''




	_UpperCamelCase   :		List[str]			=						tmp_path / 'cache'
	_UpperCamelCase   :		int			=						{'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'}
	_UpperCamelCase   :		Optional[Any]			=						features.copy() if features else default_expected_features
	_UpperCamelCase   :		Tuple			=						(
	    Features({feature: Value(lowerCAmelCase__	) for feature, dtype in features.items()}	) if features is not None else None
	)
	_UpperCamelCase   :		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_3", "col_1", "col_2"]
	for feature, expected_dtype in expected_features.items():
		assert dataset.features[feature].dtype == expected_dtype






def 					lowerCamelCase_     (							UpperCAmelCase_							:     List[Any]							,       UpperCAmelCase_							:     List[str]	)						->				int:




	'''simple docstring'''




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




	'''simple docstring'''




	_UpperCamelCase   :		str			=						tmp_path / 'cache'
	_UpperCamelCase   :		Optional[int]			=						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
	_UpperCamelCase   :		Union[str, Any]			=						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 					lowerCamelCase_     (							UpperCAmelCase_							:     Optional[Any]							,       UpperCAmelCase_							:     Optional[Any]							,       UpperCAmelCase_							:     Optional[Any]	)						->				int:




	'''simple docstring'''




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






def 					lowerCamelCase_     (							UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Any							,       UpperCAmelCase_							:     Any=("train",)	)						->				int:




	'''simple docstring'''




	assert isinstance(lowerCAmelCase__							,       lowerCAmelCase__	)
	for split in splits:
		_UpperCamelCase   :		List[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_     (							UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     List[str]	)						->				str:




	'''simple docstring'''




	_UpperCamelCase   :		Dict			=						tmp_path / 'cache'
	_UpperCamelCase   :		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():
		_UpperCamelCase   :		Union[str, 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 					lowerCamelCase_     (							UpperCAmelCase_							:     Optional[Any]							,       UpperCAmelCase_							:     Tuple							,       UpperCAmelCase_							:     List[str]	)						->				Dict:




	'''simple docstring'''




	_UpperCamelCase   :		Dict			=						tmp_path / 'cache'
	_UpperCamelCase   :		List[Any]			=						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
	_UpperCamelCase   :		Dict			=						features.copy() if features else default_expected_features
	_UpperCamelCase   :		Any			=						(
	    Features({feature: Value(lowerCAmelCase__	) for feature, dtype in features.items()}	) if features is not None else None
	)
	_UpperCamelCase   :		str			=						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 					lowerCamelCase_     (							UpperCAmelCase_							:     Dict							,       UpperCAmelCase_							:     Tuple							,       UpperCAmelCase_							:     str	)						->				str:




	'''simple docstring'''




	if split:
		_UpperCamelCase   :		str			=						{split: jsonl_path}
	else:
		_UpperCamelCase   :		List[str]			=						'train'
		_UpperCamelCase   :		Optional[int]			=						{'train': jsonl_path, 'test': jsonl_path}
	_UpperCamelCase   :		Optional[int]			=						tmp_path / 'cache'
	_UpperCamelCase   :		Optional[Any]			=						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
	_UpperCamelCase   :		Tuple			=						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 					lowerCamelCase_     (							UpperCAmelCase_							:     Union[str, Any]	)						->				Union[str, Any]:




	'''simple docstring'''




	return json.load(lowerCAmelCase__	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     int	)						->				Optional[int]:




	'''simple docstring'''




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







class 	lowercase    :


		"""simple docstring"""




		@pytest.mark.parametrize('lines, load_json_function'	,       [(True, load_json_lines), (False, load_json)])
		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(UpperCamelCase__	,       UpperCamelCase__	,       lines=UpperCamelCase__).write()
				buffer.seek(0)
				_UpperCamelCase   :		Any			=						load_json_function(UpperCamelCase__)
			assert isinstance(UpperCamelCase__	,       UpperCamelCase__)
			assert isinstance(exported_content[0]	,       UpperCamelCase__)
			assert len(UpperCamelCase__) == 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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(UpperCamelCase__	,       UpperCamelCase__	,       lines=UpperCamelCase__	,       orient=UpperCamelCase__).write()
				buffer.seek(0)
				_UpperCamelCase   :		Optional[int]			=						load_json(UpperCamelCase__)
			assert isinstance(UpperCamelCase__	,       UpperCamelCase__)
			if keys:
				if container is dict:
					assert exported_content.keys() == keys
				else:
					assert exported_content[0].keys() == keys
			else:
				assert not hasattr(UpperCamelCase__	,       'keys') and not hasattr(exported_content[0]	,       'keys')
			if len_at:
				assert len(exported_content[len_at]) == 10
			else:
				assert len(UpperCamelCase__) == 10


		@pytest.mark.parametrize('lines, load_json_function'	,       [(True, load_json_lines), (False, load_json)])
		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(UpperCamelCase__	,       UpperCamelCase__	,       lines=UpperCamelCase__	,       num_proc=2).write()
				buffer.seek(0)
				_UpperCamelCase   :		Any			=						load_json_function(UpperCamelCase__)
			assert isinstance(UpperCamelCase__	,       UpperCamelCase__)
			assert isinstance(exported_content[0]	,       UpperCamelCase__)
			assert len(UpperCamelCase__) == 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 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(UpperCamelCase__	,       UpperCamelCase__	,       lines=UpperCamelCase__	,       orient=UpperCamelCase__	,       num_proc=2).write()
				buffer.seek(0)
				_UpperCamelCase   :		Union[str, Any]			=						load_json(UpperCamelCase__)
			assert isinstance(UpperCamelCase__	,       UpperCamelCase__)
			if keys:
				if container is dict:
					assert exported_content.keys() == keys
				else:
					assert exported_content[0].keys() == keys
			else:
				assert not hasattr(UpperCamelCase__	,       'keys') and not hasattr(exported_content[0]	,       'keys')
			if len_at:
				assert len(exported_content[len_at]) == 10
			else:
				assert len(UpperCamelCase__) == 10


		def 		A__					(				self	,       __snake_case):
			with pytest.raises(UpperCamelCase__):
				with io.BytesIO() as buffer:
					JsonDatasetWriter(UpperCamelCase__	,       UpperCamelCase__	,       num_proc=0)






		@pytest.mark.parametrize('compression, extension'	,       [('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')])
		def 		A__					(				self	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case	,       __snake_case):
			_UpperCamelCase   :		Union[str, Any]			=						tmp_path_factory.mktemp('data') / f'''test.json.{extension}'''
			_UpperCamelCase   :		Tuple			=						str(shared_datadir / f'''test_file.json.{extension}''')
			JsonDatasetWriter(UpperCamelCase__	,       UpperCamelCase__	,       compression=UpperCamelCase__).write()

			with fsspec.open(UpperCamelCase__	,       'rb'	,       compression='infer') as f:
				_UpperCamelCase   :		int			=						f.read()
			with fsspec.open(UpperCamelCase__	,       'rb'	,       compression='infer') as f:
				_UpperCamelCase   :		List[str]			=						f.read()
			assert exported_content == original_content







from __future__ import annotations

import matplotlib.pyplot as plt  # type: ignore
import numpy

# initial triangle of Koch snowflake
lowerCAmelCase__								=		numpy.array([0, 0])
lowerCAmelCase__								=		numpy.array([0.5, 0.8_66_02_54])
lowerCAmelCase__								=		numpy.array([1, 0])
lowerCAmelCase__								=		[VECTOR_1, VECTOR_2, VECTOR_3, VECTOR_1]






def 					lowerCamelCase_     (							UpperCAmelCase_							:     list[numpy.ndarray]							,       UpperCAmelCase_							:     int	)						->				list[numpy.ndarray]:




	'''simple docstring'''




	_UpperCamelCase   :		Tuple			=						initial_vectors
	for _ in range(UpperCAmelCase_	):
		_UpperCamelCase   :		str			=						iteration_step(UpperCAmelCase_	)
	return vectors






def 					lowerCamelCase_     (							UpperCAmelCase_							:     list[numpy.ndarray]	)						->				list[numpy.ndarray]:




	'''simple docstring'''




	_UpperCamelCase   :		int			=						[]
	for i, start_vector in enumerate(vectors[:-1]	):
		_UpperCamelCase   :		Union[str, Any]			=						vectors[i + 1]
		new_vectors.append(UpperCAmelCase_	)
		_UpperCamelCase   :		Tuple			=						end_vector - start_vector
		new_vectors.append(start_vector + difference_vector / 3	)
		new_vectors.append(
		    start_vector + difference_vector / 3 + rotate(difference_vector / 3							,       6_0	)	)
		new_vectors.append(start_vector + difference_vector * 2 / 3	)
	new_vectors.append(vectors[-1]	)
	return new_vectors






def 					lowerCamelCase_     (							UpperCAmelCase_							:     numpy.ndarray							,       UpperCAmelCase_							:     float	)						->				numpy.ndarray:




	'''simple docstring'''




	_UpperCamelCase   :		str			=						numpy.radians(UpperCAmelCase_	)
	_UpperCamelCase	,		_UpperCamelCase   :		Optional[Any]			=						numpy.cos(UpperCAmelCase_	), numpy.sin(UpperCAmelCase_	)
	_UpperCamelCase   :		Any			=						numpy.array(((c, -s), (s, c))	)
	return numpy.dot(UpperCAmelCase_							,       UpperCAmelCase_	)






def 					lowerCamelCase_     (							UpperCAmelCase_							:     list[numpy.ndarray]	)						->				None:




	'''simple docstring'''




	_UpperCamelCase   :		str			=						plt.gca()
	axes.set_aspect('equal'	)

	# matplotlib.pyplot.plot takes a list of all x-coordinates and a list of all
	# y-coordinates as inputs, which are constructed from the vector-list using
	# zip()
	_UpperCamelCase	,		_UpperCamelCase   :		Dict			=						zip(*UpperCAmelCase_	)
	plt.plot(UpperCAmelCase_							,       UpperCAmelCase_	)
	plt.show()


if __name__ == "__main__":
							import doctest

							doctest.testmod()

							lowerCAmelCase__								=		iterate(INITIAL_VECTORS, 5)
							plot(processed_vectors)




