





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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     str:
  assert isinstance(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)
  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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     str:
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	,		keep_in_memory=_SCREAMING_SNAKE_CASE	).read()
  _check_json_dataset(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)




@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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     List[str]:
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  lowercase__			    =						features.copy() if features else default_expected_features
  lowercase__			    =						(
      Features({feature: Value(_SCREAMING_SNAKE_CASE	) for feature, dtype in features.items()}	) if features is not None else None
  )
  lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		features=_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	).read()
  _check_json_dataset(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)




@pytest.mark.parametrize(
    'features'	,		[
        None,
        {'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'},
    ]	,		)
def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     List[Any]:
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'}
  lowercase__			    =						features.copy() if features else default_expected_features
  lowercase__			    =						(
      Features({feature: Value(_SCREAMING_SNAKE_CASE	) for feature, dtype in features.items()}	) if features is not None else None
  )
  lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		features=_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	).read()
  assert isinstance(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)
  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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     Tuple:
  # jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"}
  lowercase__			    =						{'col_2': 'int64', 'col_3': 'float64', 'col_1': 'string'}
  lowercase__			    =						features.copy()
  lowercase__			    =						(
      Features({feature: Value(_SCREAMING_SNAKE_CASE	) for feature, dtype in features.items()}	) if features is not None else None
  )
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		features=_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	).read()
  assert isinstance(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)
  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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     Optional[int]:
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	,		split=_SCREAMING_SNAKE_CASE	).read()
  _check_json_dataset(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)
  assert dataset.split == split if split else "train"




@pytest.mark.parametrize('path_type'	,		[str, list]	)
def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     List[str]:
  if issubclass(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	):
    lowercase__			    =						jsonl_path
  elif issubclass(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	):
    lowercase__			    =						[jsonl_path]
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	).read()
  _check_json_dataset(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)




def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE=("train",)	)       ->     int:
  assert isinstance(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)
  for split in splits:
    lowercase__			    =						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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     Optional[Any]:
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
    lowercase__			    =						JsonDatasetReader({'train': jsonl_path}	,		cache_dir=_SCREAMING_SNAKE_CASE	,		keep_in_memory=_SCREAMING_SNAKE_CASE	).read()
  _check_json_datasetdict(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)




@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     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     Any:
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  lowercase__			    =						features.copy() if features else default_expected_features
  lowercase__			    =						(
      Features({feature: Value(_SCREAMING_SNAKE_CASE	) for feature, dtype in features.items()}	) if features is not None else None
  )
  lowercase__			    =						JsonDatasetReader({'train': jsonl_path}	,		features=_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	).read()
  _check_json_datasetdict(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)




@pytest.mark.parametrize('split'	,		[None, NamedSplit('train'	), 'train', 'test']	)
def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	)       ->     Tuple:
  if split:
    lowercase__			    =						{split: jsonl_path}
  else:
    lowercase__			    =						'train'
    lowercase__			    =						{'train': jsonl_path, 'test': jsonl_path}
  lowercase__			    =						tmp_path / 'cache'
  lowercase__			    =						{'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'}
  lowercase__			    =						JsonDatasetReader(_SCREAMING_SNAKE_CASE	,		cache_dir=_SCREAMING_SNAKE_CASE	).read()
  _check_json_datasetdict(_SCREAMING_SNAKE_CASE	,		_SCREAMING_SNAKE_CASE	,		splits=list(path.keys()	)	)
  assert all(dataset[split].split == split for split in path.keys()	)




def     __UpperCamelCase						(_SCREAMING_SNAKE_CASE	)       ->     str:
  return json.load(_SCREAMING_SNAKE_CASE	)




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


class        SCREAMING_SNAKE_CASE     :


   @pytest.mark.parametrize('lines, load_json_function'    ,  [(True, load_json_lines), (False, load_json)] )
   def  SCREAMING_SNAKE_CASE_							(					self    :       Any    ,  a    :       int    ,  a    :       Optional[int]    ,  a    :       Tuple )->     Optional[int]:





     """simple docstring"""







     with io.BytesIO() as buffer:
       JsonDatasetWriter(a    ,  a    ,  lines=a ).write()
       buffer.seek(0 )
       lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       int    ,  a    :       Optional[int]    ,  a    :       str    ,  a    :       Dict    ,  a    :       Any    ,  a    :       Dict )->     List[str]:





     """simple docstring"""







     with io.BytesIO() as buffer:
       JsonDatasetWriter(a    ,  a    ,  lines=a    ,  orient=a ).write()
       buffer.seek(0 )
       lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       Tuple    ,  a    :       List[Any]    ,  a    :       List[Any]    ,  a    :       str )->     Any:





     """simple docstring"""







     with io.BytesIO() as buffer:
       JsonDatasetWriter(a    ,  a    ,  lines=a    ,  num_proc=2 ).write()
       buffer.seek(0 )
       lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       Optional[Any]    ,  a    :       Tuple    ,  a    :       Dict    ,  a    :       Any    ,  a    :       str    ,  a    :       str )->     List[str]:





     """simple docstring"""







     with io.BytesIO() as buffer:
       JsonDatasetWriter(a    ,  a    ,  lines=a    ,  orient=a    ,  num_proc=2 ).write()
       buffer.seek(0 )
       lowercase__			    =						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  SCREAMING_SNAKE_CASE_							(					self    :       int    ,  a    :       int )->     List[str]:





     """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  SCREAMING_SNAKE_CASE_							(					self    :       str    ,  a    :       List[str]    ,  a    :       int    ,  a    :       List[str]    ,  a    :       Dict    ,  a    :       Union[str, Any] )->     Dict:





     """simple docstring"""







     lowercase__			    =						tmp_path_factory.mktemp('data' ) / f"""test.json.{extension}"""
     lowercase__			    =						str(shared_datadir / f"""test_file.json.{extension}""" )
     JsonDatasetWriter(a    ,  a    ,  compression=a ).write()

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









class        SCREAMING_SNAKE_CASE     :  # Public class to implement a graph


   def __init__(					self    :       int    ,  a    :       int    ,  a    :       int    ,  a    :       list[list[bool]] )->     None:





     """simple docstring"""







     lowercase__			    =						row
     lowercase__			    =						col
     lowercase__			    =						graph


   def  SCREAMING_SNAKE_CASE_							(					self    :       Dict    ,  a    :       int    ,  a    :       int    ,  a    :       list[list[bool]] )->     bool:





     """simple docstring"""







     return (
         0 <= i < self.ROW
         and 0 <= j < self.COL
         and not visited[i][j]
         and self.graph[i][j]
     )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str]    ,  a    :       int    ,  a    :       int    ,  a    :       list[list[bool]] )->     None:





     """simple docstring"""







     lowercase__			    =						[-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
     lowercase__			    =						[-1, 0, 1, -1, 1, -1, 0, 1]
     lowercase__			    =						True  # Make those cells visited
     for k in range(8 ):
       if self.is_safe(i + row_nbr[k]    ,  j + col_nbr[k]    ,  a ):
         self.diffs(i + row_nbr[k]    ,  j + col_nbr[k]    ,  a )


   def  SCREAMING_SNAKE_CASE_							(					self    :       List[str] )->     int:  # And finally, count all islands.





     """simple docstring"""







     lowercase__			    =						[[False for j in range(self.COL )] for i in range(self.ROW )]
     lowercase__			    =						0
     for i in range(self.ROW ):
       for j in range(self.COL ):
         if visited[i][j] is False and self.graph[i][j] == 1:
           self.diffs(a    ,  a    ,  a )
           count += 1
     return count


