






"""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 							snake_case (  A__      ,A__		):
			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 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					int     	=   tmp_path / "cache"
			UpperCAmelCase_	:					Union[str, 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():
						UpperCAmelCase_	:					Tuple     	=   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 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					Union[str, Any]     	=   tmp_path / "cache"
			UpperCAmelCase_	:					List[str]     	=   {"col_1": "string", "col_2": "int64", "col_3": "float64"}
			UpperCAmelCase_	:					int     	=   features.copy() if features else default_expected_features
			UpperCAmelCase_	:					Dict     	=   (
			    Features({feature: Value(A__		) for feature, dtype in features.items()}		) if features is not None else None
			)
			UpperCAmelCase_	:					List[str]     	=   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 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					str     	=   tmp_path / "cache"
			UpperCAmelCase_	:					List[Any]     	=   {"col_3": "float64", "col_1": "string", "col_2": "int64"}
			UpperCAmelCase_	:					List[Any]     	=   features.copy() if features else default_expected_features
			UpperCAmelCase_	:					List[str]     	=   (
			    Features({feature: Value(A__		) for feature, dtype in features.items()}		) if features is not None else None
			)
			UpperCAmelCase_	:					Optional[int]     	=   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 							snake_case (  A__      ,A__		):
			# jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"}
			UpperCAmelCase_	:					Optional[int]     	=   {"col_2": "int64", "col_3": "float64", "col_1": "string"}
			UpperCAmelCase_	:					Tuple     	=   features.copy()
			UpperCAmelCase_	:					Any     	=   (
			    Features({feature: Value(A__		) for feature, dtype in features.items()}		) if features is not None else None
			)
			UpperCAmelCase_	:					Dict     	=   tmp_path / "cache"
			UpperCAmelCase_	:					Tuple     	=   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 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					List[str]     	=   tmp_path / "cache"
			UpperCAmelCase_	:					Union[str, Any]     	=   {"col_1": "string", "col_2": "int64", "col_3": "float64"}
			UpperCAmelCase_	:					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 							snake_case (  A__      ,A__      ,A__		):
			if issubclass(A__      ,A__		):
						UpperCAmelCase_	:					Tuple     	=   jsonl_path
			elif issubclass(A__      ,A__		):
						UpperCAmelCase_	:					int     	=   [jsonl_path]
			UpperCAmelCase_	:					Dict     	=   tmp_path / "cache"
			UpperCAmelCase_	:					str     	=   {"col_1": "string", "col_2": "int64", "col_3": "float64"}
			UpperCAmelCase_	:					List[str]     	=   JsonDatasetReader(A__      ,cache_dir=A__		).read()
			_check_json_dataset(A__      ,A__		)

def 							snake_case (  A__      ,A__      ,A__=("train",)		):
			assert isinstance(A__      ,A__		)
			for split in splits:
						UpperCAmelCase_	:					Union[str, 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 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					List[str]     	=   tmp_path / "cache"
			UpperCAmelCase_	:					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():
						UpperCAmelCase_	:					List[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 							snake_case (  A__      ,A__      ,A__		):
			UpperCAmelCase_	:					Dict     	=   tmp_path / "cache"
			UpperCAmelCase_	:					Union[str, Any]     	=   {"col_1": "string", "col_2": "int64", "col_3": "float64"}
			UpperCAmelCase_	:					str     	=   features.copy() if features else default_expected_features
			UpperCAmelCase_	:					Any     	=   (
			    Features({feature: Value(A__		) for feature, dtype in features.items()}		) if features is not None else None
			)
			UpperCAmelCase_	:					Optional[int]     	=   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 							snake_case (  A__      ,A__      ,A__		):
			if split:
						UpperCAmelCase_	:					Tuple     	=   {split: jsonl_path}
			else:
						UpperCAmelCase_	:					List[Any]     	=   "train"
						UpperCAmelCase_	:					str     	=   {"train": jsonl_path, "test": jsonl_path}
			UpperCAmelCase_	:					str     	=   tmp_path / "cache"
			UpperCAmelCase_	:					int     	=   {"col_1": "string", "col_2": "int64", "col_3": "float64"}
			UpperCAmelCase_	:					Dict     	=   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 							snake_case (  A__		):
			return json.load(A__		)

def 							snake_case (  A__		):
			return [json.loads(A__		) for line in buffer]







class      UpperCamelCase_			:





	@pytest.mark.parametrize("lines, load_json_function"      ,      [(True, load_json_lines), (False, load_json)]		)
	def 	_SCREAMING_SNAKE_CASE ( self   :					Union[str, Any]      ,      lowerCAmelCase_   :					Optional[int]      ,      lowerCAmelCase_   :					Any      ,      lowerCAmelCase_   :					Optional[int]		)		->     str:
				with io.BytesIO() as buffer:
							JsonDatasetWriter(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lines=lowerCAmelCase_		).write()
							buffer.seek(0		)
							UpperCAmelCase_	:					Union[str, 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 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]      ,      lowerCAmelCase_   :					List[Any]      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					List[Any]      ,      lowerCAmelCase_   :					Tuple      ,      lowerCAmelCase_   :					List[str]		)		->     List[str]:
				with io.BytesIO() as buffer:
							JsonDatasetWriter(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lines=lowerCAmelCase_      ,      orient=lowerCAmelCase_		).write()
							buffer.seek(0		)
							UpperCAmelCase_	:					Dict     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					str      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					Tuple		)		->     Union[str, Any]:
				with io.BytesIO() as buffer:
							JsonDatasetWriter(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lines=lowerCAmelCase_      ,      num_proc=2		).write()
							buffer.seek(0		)
							UpperCAmelCase_	:					str     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					Any      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					Union[str, Any]      ,      lowerCAmelCase_   :					List[str]      ,      lowerCAmelCase_   :					Union[str, Any]      ,      lowerCAmelCase_   :					Dict		)		->     Optional[Any]:
				with io.BytesIO() as buffer:
							JsonDatasetWriter(lowerCAmelCase_      ,      lowerCAmelCase_      ,      lines=lowerCAmelCase_      ,      orient=lowerCAmelCase_      ,      num_proc=2		).write()
							buffer.seek(0		)
							UpperCAmelCase_	:					Union[str, 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 	_SCREAMING_SNAKE_CASE ( self   :					List[str]      ,      lowerCAmelCase_   :					Optional[Any]		)		->     List[Any]:
				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 	_SCREAMING_SNAKE_CASE ( self   :					Any      ,      lowerCAmelCase_   :					Dict      ,      lowerCAmelCase_   :					int      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					Optional[int]      ,      lowerCAmelCase_   :					Tuple		)		->     List[str]:
				UpperCAmelCase_	:					Optional[Any]     	=   tmp_path_factory.mktemp("data"		) / f"""test.json.{extension}"""
				UpperCAmelCase_	:					Dict     	=   str(shared_datadir / f"""test_file.json.{extension}"""		)
				JsonDatasetWriter(lowerCAmelCase_      ,      lowerCAmelCase_      ,      compression=lowerCAmelCase_		).write()

				with fsspec.open(lowerCAmelCase_      ,      "rb"      ,      compression="infer"		) as f:
							UpperCAmelCase_	:					Optional[Any]     	=   f.read()
				with fsspec.open(lowerCAmelCase_      ,      "rb"      ,      compression="infer"		) as f:
							UpperCAmelCase_	:					Optional[int]     	=   f.read()
				assert exported_content == original_content




'''simple docstring'''

import colorsys

from PIL import Image  # type: ignore



def snake_case_      (					__snake_case  :							float	,   __snake_case  :							float	,   __snake_case  :							int)			->					float:
   lowerCAmelCase_			=      x
   lowerCAmelCase_			=      y
   for step in range(__snake_case):  # noqa: B007
      lowerCAmelCase_			=      a * a - b * b + x
      lowerCAmelCase_			=      2 * a * b + y
      lowerCAmelCase_			=      a_new

      # divergence happens for all complex number with an absolute value
      # greater than 4
      if a * a + b * b > 4:
         break
   return step / (max_step - 1)



def snake_case_      (					__snake_case  :							float)			->					tuple:
   if distance == 1:
      return (0, 0, 0)
   else:
      return (255, 255, 255)



def snake_case_      (					__snake_case  :							float)			->					tuple:
   if distance == 1:
      return (0, 0, 0)
   else:
      return tuple(round(i * 255) for i in colorsys.hsv_to_rgb(__snake_case	,   1	,   1))



def snake_case_      (					__snake_case  :							int = 800	,   __snake_case  :							int = 600	,   __snake_case  :							float = -0.6	,   __snake_case  :							float = 0	,   __snake_case  :							float = 3.2	,   __snake_case  :							int = 50	,   __snake_case  :							bool = True	,   )			->					Image.Image:
   lowerCAmelCase_			=      Image.new('''RGB'''	,   (image_width, image_height))
   lowerCAmelCase_			=      img.load()

   # loop through the image-coordinates
   for image_x in range(__snake_case):
      for image_y in range(__snake_case):
         # determine the figure-coordinates based on the image-coordinates
         lowerCAmelCase_			=      figure_width / image_width * image_height
         lowerCAmelCase_			=      figure_center_x + (image_x / image_width - 0.5) * figure_width
         lowerCAmelCase_			=      figure_center_y + (image_y / image_height - 0.5) * figure_height

         lowerCAmelCase_			=      get_distance(__snake_case	,   __snake_case	,   __snake_case)

         # color the corresponding pixel based on the selected coloring-function
         if use_distance_color_coding:
            lowerCAmelCase_			=      get_color_coded_rgb(__snake_case)
         else:
            lowerCAmelCase_			=      get_black_and_white_rgb(__snake_case)

   return img


if __name__ == "__main__":
 import doctest

 doctest.testmod()

 # colored version, full figure
 A_     :						List[str]    							=get_image()

 # uncomment for colored version, different section, zoomed in
 # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
 # figure_width = 0.8)

 # uncomment for black and white version, full figure
 # img = get_image(use_distance_color_coding = False)

 # uncomment to save the image
 # img.save("mandelbrot.png")

 img.show()





