


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    (				lowerCAmelCase_				:Dict		,    lowerCAmelCase_				:Union[str, Any]				)->Optional[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       _lowerCAmelCase    (				lowerCAmelCase_				:List[Any]		,    lowerCAmelCase_				:List[Any]		,    lowerCAmelCase_				:int				)->List[Any]:
   '''simple docstring'''






   snake_case_            =       tmp_path / "cache"
   snake_case_            =       {"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_            =       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    (				lowerCAmelCase_				:List[str]		,    lowerCAmelCase_				:Dict		,    lowerCAmelCase_				:Dict				)->List[Any]:
   '''simple docstring'''






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






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






   snake_case_            =       {"col_2": "int64", "col_3": "float64", "col_1": "string"}
   snake_case_            =       features.copy()
   snake_case_            =       (
       Features({feature: Value(lowerCAmelCase_				) for feature, dtype in features.items()}				) if features is not None else None
   )
   snake_case_            =       tmp_path / "cache"
   snake_case_            =       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    (				lowerCAmelCase_				:Any		,    lowerCAmelCase_				:Optional[Any]		,    lowerCAmelCase_				:Any				)->Optional[Any]:
   '''simple docstring'''






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






   if issubclass(lowerCAmelCase_		,    lowerCAmelCase_				):
      snake_case_            =       jsonl_path
   elif issubclass(lowerCAmelCase_		,    lowerCAmelCase_				):
      snake_case_            =       [jsonl_path]
   snake_case_            =       tmp_path / "cache"
   snake_case_            =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
   snake_case_            =       JsonDatasetReader(lowerCAmelCase_		,    cache_dir=lowerCAmelCase_				).read()
   _check_json_dataset(lowerCAmelCase_		,    lowerCAmelCase_				)



def       _lowerCAmelCase    (				lowerCAmelCase_				:Union[str, Any]		,    lowerCAmelCase_				:int		,    lowerCAmelCase_				:Union[str, Any]=("train",)				)->Union[str, Any]:
   '''simple docstring'''






   assert isinstance(lowerCAmelCase_		,    lowerCAmelCase_				)
   for split in splits:
      snake_case_            =       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    (				lowerCAmelCase_				:Dict		,    lowerCAmelCase_				:str		,    lowerCAmelCase_				:str				)->Optional[Any]:
   '''simple docstring'''






   snake_case_            =       tmp_path / "cache"
   snake_case_            =       {"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_            =       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    (				lowerCAmelCase_				:Optional[Any]		,    lowerCAmelCase_				:Tuple		,    lowerCAmelCase_				:List[str]				)->Tuple:
   '''simple docstring'''






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






   if split:
      snake_case_            =       {split: jsonl_path}
   else:
      snake_case_            =       "train"
      snake_case_            =       {"train": jsonl_path, "test": jsonl_path}
   snake_case_            =       tmp_path / "cache"
   snake_case_            =       {"col_1": "string", "col_2": "int64", "col_3": "float64"}
   snake_case_            =       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    (				lowerCAmelCase_				:Union[str, Any]				)->List[str]:
   '''simple docstring'''






   return json.load(lowerCAmelCase_				)



def       _lowerCAmelCase    (				lowerCAmelCase_				:List[Any]				)->int:
   '''simple docstring'''






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



class      __lowerCAmelCase  :






  """simple docstring"""





  @pytest.mark.parametrize("lines, load_json_function"     ,					[(True, load_json_lines), (False, load_json)]	)
  def        lowerCAmelCase__	(  self							:							Union[str, Any]     ,					_lowerCAmelCase							:							str     ,					_lowerCAmelCase							:							Optional[int]     ,					_lowerCAmelCase							:							Dict	) ->			List[str]:




     """simple docstring"""
     with io.BytesIO() as buffer:
        JsonDatasetWriter(_lowerCAmelCase     ,					_lowerCAmelCase     ,					lines=_lowerCAmelCase	).write()
        buffer.seek(0	)
        snake_case_            =       load_json_function(_lowerCAmelCase	)
     assert isinstance(_lowerCAmelCase     ,					_lowerCAmelCase	)
     assert isinstance(exported_content[0]     ,					_lowerCAmelCase	)
     assert len(_lowerCAmelCase	) == 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        lowerCAmelCase__	(  self							:							List[str]     ,					_lowerCAmelCase							:							str     ,					_lowerCAmelCase							:							Any     ,					_lowerCAmelCase							:							Any     ,					_lowerCAmelCase							:							Dict     ,					_lowerCAmelCase							:							Union[str, Any]	) ->			List[str]:




     """simple docstring"""
     with io.BytesIO() as buffer:
        JsonDatasetWriter(_lowerCAmelCase     ,					_lowerCAmelCase     ,					lines=_lowerCAmelCase     ,					orient=_lowerCAmelCase	).write()
        buffer.seek(0	)
        snake_case_            =       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]	) == 1_0
     else:
        assert len(_lowerCAmelCase	) == 1_0



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




     """simple docstring"""
     with io.BytesIO() as buffer:
        JsonDatasetWriter(_lowerCAmelCase     ,					_lowerCAmelCase     ,					lines=_lowerCAmelCase     ,					num_proc=2	).write()
        buffer.seek(0	)
        snake_case_            =       load_json_function(_lowerCAmelCase	)
     assert isinstance(_lowerCAmelCase     ,					_lowerCAmelCase	)
     assert isinstance(exported_content[0]     ,					_lowerCAmelCase	)
     assert len(_lowerCAmelCase	) == 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        lowerCAmelCase__	(  self							:							str     ,					_lowerCAmelCase							:							List[Any]     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							Any     ,					_lowerCAmelCase							:							List[Any]	) ->			List[Any]:




     """simple docstring"""
     with io.BytesIO() as buffer:
        JsonDatasetWriter(_lowerCAmelCase     ,					_lowerCAmelCase     ,					lines=_lowerCAmelCase     ,					orient=_lowerCAmelCase     ,					num_proc=2	).write()
        buffer.seek(0	)
        snake_case_            =       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]	) == 1_0
     else:
        assert len(_lowerCAmelCase	) == 1_0



  def        lowerCAmelCase__	(  self							:							List[str]     ,					_lowerCAmelCase							:							int	) ->			int:




     """simple docstring"""
     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        lowerCAmelCase__	(  self							:							Any     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							Tuple     ,					_lowerCAmelCase							:							List[Any]     ,					_lowerCAmelCase							:							Union[str, Any]     ,					_lowerCAmelCase							:							Dict	) ->			Any:




     """simple docstring"""
     snake_case_            =       tmp_path_factory.mktemp("data"	) / F'''test.json.{extension}'''
     snake_case_            =       str(shared_datadir / F'''test_file.json.{extension}'''	)
     JsonDatasetWriter(_lowerCAmelCase     ,					_lowerCAmelCase     ,					compression=_lowerCAmelCase	).write()

     with fsspec.open(_lowerCAmelCase     ,					"rb"     ,					compression="infer"	) as f:
        snake_case_            =       f.read()
     with fsspec.open(_lowerCAmelCase     ,					"rb"     ,					compression="infer"	) as f:
        snake_case_            =       f.read()
     assert exported_content == original_content








def       _lowerCAmelCase    (				lowerCAmelCase_				:int		,    lowerCAmelCase_				:int				)->int:
   '''simple docstring'''






   return int((input_a, input_a).count(0				) == 0				)



def       _lowerCAmelCase    (				)->None:
   '''simple docstring'''






   assert and_gate(0		,    0				) == 0
   assert and_gate(0		,    1				) == 0
   assert and_gate(1		,    0				) == 0
   assert and_gate(1		,    1				) == 1


if __name__ == "__main__":
   test_and_gate()
   print(and_gate(1, 0))
   print(and_gate(0, 0))
   print(and_gate(0, 1))
   print(and_gate(1, 1))




