






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 			A__							(      _a  :					List[str]		,  _a  :					Optional[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 			A__							(      _a  :					Dict		,  _a  :					int		,  _a  :					Dict	):





 '''simple docstring'''







 snake_case__  :    Dict							       =tmp_path / """cache"""
 snake_case__  :    Tuple							       ={"""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__  :    Union[str, Any]							       =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 			A__							(      _a  :					int		,  _a  :					Optional[int]		,  _a  :					Union[str, Any]	):





 '''simple docstring'''







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





 '''simple docstring'''







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





 '''simple docstring'''







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





 '''simple docstring'''







 snake_case__  :    int							       =tmp_path / """cache"""
 snake_case__  :    Optional[int]							       ={"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
 snake_case__  :    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 			A__							(      _a  :					List[str]		,  _a  :					str		,  _a  :					Tuple	):





 '''simple docstring'''







 if issubclass(_a		,  _a	):
  snake_case__  :    Optional[int]							       =jsonl_path
 elif issubclass(_a		,  _a	):
  snake_case__  :    Dict							       =[jsonl_path]
 snake_case__  :    Union[str, Any]							       =tmp_path / """cache"""
 snake_case__  :    Optional[Any]							       ={"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
 snake_case__  :    str							       =JsonDatasetReader(_a		,  cache_dir=_a	).read()
 _check_json_dataset(_a		,  _a	)







def 			A__							(      _a  :					Dict		,  _a  :					List[Any]		,  _a  :					int=("train",)	):





 '''simple docstring'''







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





 '''simple docstring'''







 snake_case__  :    str							       =tmp_path / """cache"""
 snake_case__  :    List[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():
  snake_case__  :    List[str]							       =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 			A__							(      _a  :					List[str]		,  _a  :					int		,  _a  :					str	):





 '''simple docstring'''







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





 '''simple docstring'''







 if split:
  snake_case__  :    Optional[Any]							       ={split: jsonl_path}
 else:
  snake_case__  :    Dict							       ="""train"""
  snake_case__  :    Union[str, Any]							       ={"""train""": jsonl_path, """test""": jsonl_path}
 snake_case__  :    List[str]							       =tmp_path / """cache"""
 snake_case__  :    Tuple							       ={"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""}
 snake_case__  :    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 			A__							(      _a  :					str	):





 '''simple docstring'''







 return json.load(_a	)







def 			A__							(      _a  :					List[str]	):





 '''simple docstring'''







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







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

       with fsspec.open(a				,			"""rb"""				,			compression="""infer"""   ) as f:
        snake_case__  :    Dict							       =f.read()
       with fsspec.open(a				,			"""rb"""				,			compression="""infer"""   ) as f:
        snake_case__  :    Optional[int]							       =f.read()
       assert exported_content == original_content













from string import ascii_uppercase

__lowerCamelCase	:		List[Any]										=   {str(ord(c) - 55): c for c in ascii_uppercase}







def 			A__							(      _a  :					int		,  _a  :					int	):





 '''simple docstring'''







 if isinstance(_a		,  _a	):
  raise TypeError("""int() can't convert non-string with explicit base"""	)
 if num < 0:
  raise ValueError("""parameter must be positive int"""	)
 if isinstance(_a		,  _a	):
  raise TypeError("""'str' object cannot be interpreted as an integer"""	)
 if isinstance(_a		,  _a	):
  raise TypeError("""'float' object cannot be interpreted as an integer"""	)
 if base in (0, 1):
  raise ValueError("""base must be >= 2"""	)
 if base > 36:
  raise ValueError("""base must be <= 36"""	)
 snake_case__  :    Union[str, Any]							       =""""""
 snake_case__  :    List[str]							       =0
 snake_case__  :    List[Any]							       =0
 while div != 1:
  snake_case__       ,   snake_case__  :    Union[str, Any]							       =divmod(_a		,  _a	)
  if base >= 11 and 9 < mod < 36:
   snake_case__  :    Dict							       =ALPHABET_VALUES[str(_a	)]
  else:
   snake_case__  :    Optional[Any]							       =str(_a	)
  new_value += actual_value
  snake_case__  :    Dict							       =num // base
  snake_case__  :    Tuple							       =div
  if div == 0:
   return str(new_value[::-1]	)
  elif div == 1:
   new_value += str(_a	)
   return str(new_value[::-1]	)

 return new_value[::-1]


if __name__ == "__main__":
       import doctest

       doctest.testmod()

       for base in range(2, 37):
              for num in range(10_00):
                     assert int(decimal_to_any(num, base), base) == num, (
                         num,
                         base,
                         decimal_to_any(num, base),
                         int(decimal_to_any(num, base), base),
                     )





