


'''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			(			lowerCAmelCase				:	Union[str, Any]		,		lowerCAmelCase				:	int		):





	"""simple docstring"""







	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 lowerCamelCase			(			lowerCAmelCase				:	int		,		lowerCAmelCase				:	List[Any]		,		lowerCAmelCase				:	Any		):





	"""simple docstring"""







	__magic_name__       :    Tuple     	=     tmp_path / '''cache'''
	__magic_name__       :    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():
		__magic_name__       :    Optional[int]     	=     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 lowerCamelCase			(			lowerCAmelCase				:	Union[str, Any]		,		lowerCAmelCase				:	List[str]		,		lowerCAmelCase				:	List[str]		):





	"""simple docstring"""







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





	"""simple docstring"""







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





	"""simple docstring"""







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





	"""simple docstring"""







	__magic_name__       :    Optional[int]     	=     tmp_path / '''cache'''
	__magic_name__       :    List[str]     	=     {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	__magic_name__       :    Optional[int]     	=     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 lowerCamelCase			(			lowerCAmelCase				:	Dict		,		lowerCAmelCase				:	Tuple		,		lowerCAmelCase				:	int		):





	"""simple docstring"""







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





def lowerCamelCase			(			lowerCAmelCase				:	Optional[int]		,		lowerCAmelCase				:	int		,		lowerCAmelCase				:	Union[str, Any]=("train",)		):





	"""simple docstring"""







	assert isinstance(_a		,		_a		)
	for split in splits:
		__magic_name__       :    str     	=     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				:	Union[str, Any]		,		lowerCAmelCase				:	List[str]		,		lowerCAmelCase				:	Optional[int]		):





	"""simple docstring"""







	__magic_name__       :    List[str]     	=     tmp_path / '''cache'''
	__magic_name__       :    Dict     	=     {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		__magic_name__       :    Optional[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 lowerCamelCase			(			lowerCAmelCase				:	Dict		,		lowerCAmelCase				:	List[Any]		,		lowerCAmelCase				:	Optional[int]		):





	"""simple docstring"""







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





	"""simple docstring"""







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





	"""simple docstring"""







	return json.load(_a		)





def lowerCamelCase			(			lowerCAmelCase				:	Any		):





	"""simple docstring"""







	return [json.loads(_a		) 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		:						List[str]  ,     _A		:						Any  ,     _A		:						List[Any]  ,     _A		:						Optional[int]			)  -> List[str]:
			with io.BytesIO() as buffer:
				JsonDatasetWriter(lowercase_  ,     lowercase_  ,     lines=lowercase_			).write()
				buffer.seek(0			)
				__magic_name__       :    List[str]     	=     load_json_function(lowercase_			)
			assert isinstance(lowercase_  ,     lowercase_			)
			assert isinstance(exported_content[0]  ,     lowercase_			)
			assert len(lowercase_			) == 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    __lowerCAmelCase		(					self		:						str  ,     _A		:						Dict  ,     _A		:						Union[str, Any]  ,     _A		:						Union[str, Any]  ,     _A		:						Optional[int]  ,     _A		:						Dict			)  -> int:
			with io.BytesIO() as buffer:
				JsonDatasetWriter(lowercase_  ,     lowercase_  ,     lines=lowercase_  ,     orient=lowercase_			).write()
				buffer.seek(0			)
				__magic_name__       :    str     	=     load_json(lowercase_			)
			assert isinstance(lowercase_  ,     lowercase_			)
			if keys:
				if container is dict:
					assert exported_content.keys() == keys
				else:
					assert exported_content[0].keys() == keys
			else:
				assert not hasattr(lowercase_  ,     'keys'			) and not hasattr(exported_content[0]  ,     'keys'			)
			if len_at:
				assert len(exported_content[len_at]			) == 10
			else:
				assert len(lowercase_			) == 10







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







		def    __lowerCAmelCase		(					self		:						str  ,     _A		:						Optional[Any]			)  -> int:
			with pytest.raises(lowercase_			):
				with io.BytesIO() as buffer:
					JsonDatasetWriter(lowercase_  ,     lowercase_  ,     num_proc=0			)







		@pytest.mark.parametrize('compression, extension'  ,     [('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')]			)
		def    __lowerCAmelCase		(					self		:						Optional[Any]  ,     _A		:						int  ,     _A		:						List[str]  ,     _A		:						Tuple  ,     _A		:						Union[str, Any]  ,     _A		:						Optional[int]			)  -> int:
			__magic_name__       :    Any     	=     tmp_path_factory.mktemp('data'			) / F'test.json.{extension}'
			__magic_name__       :    Tuple     	=     str(shared_datadir / F'test_file.json.{extension}'			)
			JsonDatasetWriter(lowercase_  ,     lowercase_  ,     compression=lowercase_			).write()

			with fsspec.open(lowercase_  ,     'rb'  ,     compression='infer'			) as f:
				__magic_name__       :    Optional[Any]     	=     f.read()
			with fsspec.open(lowercase_  ,     'rb'  ,     compression='infer'			) as f:
				__magic_name__       :    Optional[Any]     	=     f.read()
			assert exported_content == original_content




"""simple docstring"""

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase__    :  str							    =	{
    '''configuration_x_clip''': [
        '''XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''XCLIPConfig''',
        '''XCLIPTextConfig''',
        '''XCLIPVisionConfig''',
    ],
    '''processing_x_clip''': ['''XCLIPProcessor'''],
}

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowercase__    :  Tuple							    =	[
         '''XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
         '''XCLIPModel''',
         '''XCLIPPreTrainedModel''',
         '''XCLIPTextModel''',
         '''XCLIPVisionModel''',
     ]

if TYPE_CHECKING:
     from .configuration_x_clip import (
         XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
         XCLIPConfig,
         XCLIPTextConfig,
         XCLIPVisionConfig,
     )
     from .processing_x_clip import XCLIPProcessor

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_x_clip import (
              XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
              XCLIPModel,
              XCLIPPreTrainedModel,
              XCLIPTextModel,
              XCLIPVisionModel,
          )

else:
     import sys

     lowercase__    :  Tuple							    =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





