

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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__=("train",)      ):

     """simple docstring"""






     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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     __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        __lowercase    (  UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      ):

     """simple docstring"""






     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        __lowercase    (  UpperCAmelCase__      ):

     """simple docstring"""






     return json.load(UpperCAmelCase__      )


def        __lowercase    (  UpperCAmelCase__      ):

     """simple docstring"""






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





class     snake_case_			:


       """simple docstring"""





       @pytest.mark.parametrize('lines, load_json_function'					,						[(True, load_json_lines), (False, load_json)]							)
       def        A__  ( self					,						_A					,						_A					,						_A							):
            with io.BytesIO() as buffer:
                 JsonDatasetWriter(_A					,						_A					,						lines=_A							).write()
                 buffer.seek(0							)
                 __lowerCAmelCase					=							load_json_function(_A							)
            assert isinstance(_A					,						_A							)
            assert isinstance(exported_content[0]					,						_A							)
            assert len(_A							) == 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        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A							):
            with io.BytesIO() as buffer:
                 JsonDatasetWriter(_A					,						_A					,						lines=_A					,						orient=_A							).write()
                 buffer.seek(0							)
                 __lowerCAmelCase					=							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]							) == 1_0
            else:
                 assert len(_A							) == 1_0




       @pytest.mark.parametrize('lines, load_json_function'					,						[(True, load_json_lines), (False, load_json)]							)
       def        A__  ( self					,						_A					,						_A					,						_A							):
            with io.BytesIO() as buffer:
                 JsonDatasetWriter(_A					,						_A					,						lines=_A					,						num_proc=2							).write()
                 buffer.seek(0							)
                 __lowerCAmelCase					=							load_json_function(_A							)
            assert isinstance(_A					,						_A							)
            assert isinstance(exported_content[0]					,						_A							)
            assert len(_A							) == 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        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A							):
            with io.BytesIO() as buffer:
                 JsonDatasetWriter(_A					,						_A					,						lines=_A					,						orient=_A					,						num_proc=2							).write()
                 buffer.seek(0							)
                 __lowerCAmelCase					=							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]							) == 1_0
            else:
                 assert len(_A							) == 1_0




       def        A__  ( self					,						_A							):
            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        A__  ( self					,						_A					,						_A					,						_A					,						_A					,						_A							):
            __lowerCAmelCase					=							tmp_path_factory.mktemp('data'							) / F"""test.json.{extension}"""
            __lowerCAmelCase					=							str(shared_datadir / F"""test_file.json.{extension}"""							)
            JsonDatasetWriter(_A					,						_A					,						compression=_A							).write()

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









import random


def        __lowercase    (  UpperCAmelCase__      ):

					"""simple docstring"""






					__lowerCAmelCase					=							num - 1
					__lowerCAmelCase					=							0

					while s % 2 == 0:
										__lowerCAmelCase					=							s // 2
										t += 1

					for _ in range(5      ):
										__lowerCAmelCase					=							random.randrange(2		,					num - 1      )
										__lowerCAmelCase					=							pow(UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__      )
										if v != 1:
															__lowerCAmelCase					=							0
															while v != (num - 1):
																				if i == t - 1:
																									return False
																				else:
																									__lowerCAmelCase					=							i + 1
																									__lowerCAmelCase					=							(v**2) % num
					return True


def        __lowercase    (  UpperCAmelCase__      ):

					"""simple docstring"""






					if num < 2:
										return False

					__lowerCAmelCase					=							[
					    2,
					    3,
					    5,
					    7,
					    11,
					    13,
					    17,
					    19,
					    23,
					    29,
					    31,
					    37,
					    41,
					    43,
					    47,
					    53,
					    59,
					    61,
					    67,
					    71,
					    73,
					    79,
					    83,
					    89,
					    97,
					    101,
					    103,
					    107,
					    109,
					    113,
					    127,
					    131,
					    137,
					    139,
					    149,
					    151,
					    157,
					    163,
					    167,
					    173,
					    179,
					    181,
					    191,
					    193,
					    197,
					    199,
					    211,
					    223,
					    227,
					    229,
					    233,
					    239,
					    241,
					    251,
					    257,
					    263,
					    269,
					    271,
					    277,
					    281,
					    283,
					    293,
					    307,
					    311,
					    313,
					    317,
					    331,
					    337,
					    347,
					    349,
					    353,
					    359,
					    367,
					    373,
					    379,
					    383,
					    389,
					    397,
					    401,
					    409,
					    419,
					    421,
					    431,
					    433,
					    439,
					    443,
					    449,
					    457,
					    461,
					    463,
					    467,
					    479,
					    487,
					    491,
					    499,
					    503,
					    509,
					    521,
					    523,
					    541,
					    547,
					    557,
					    563,
					    569,
					    571,
					    577,
					    587,
					    593,
					    599,
					    601,
					    607,
					    613,
					    617,
					    619,
					    631,
					    641,
					    643,
					    647,
					    653,
					    659,
					    661,
					    673,
					    677,
					    683,
					    691,
					    701,
					    709,
					    719,
					    727,
					    733,
					    739,
					    743,
					    751,
					    757,
					    761,
					    769,
					    773,
					    787,
					    797,
					    809,
					    811,
					    821,
					    823,
					    827,
					    829,
					    839,
					    853,
					    857,
					    859,
					    863,
					    877,
					    881,
					    883,
					    887,
					    907,
					    911,
					    919,
					    929,
					    937,
					    941,
					    947,
					    953,
					    967,
					    971,
					    977,
					    983,
					    991,
					    997,
					]

					if num in low_primes:
										return True

					for prime in low_primes:
										if (num % prime) == 0:
															return False

					return rabin_miller(UpperCAmelCase__      )


def        __lowercase    (  UpperCAmelCase__ = 1_024      ):

					"""simple docstring"""






					while True:
										__lowerCAmelCase					=							random.randrange(2 ** (keysize - 1)		,					2 ** (keysize)      )
										if is_prime_low_num(UpperCAmelCase__      ):
															return num


if __name__ == "__main__":
		lowerCamelCase      				=     generate_large_prime()
		print(('''Prime number:''', num))
		print(('''is_prime_low_num:''', is_prime_low_num(num)))






