




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__  (     lowerCamelCase,			lowerCamelCase   ):
  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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  lowercase    :Optional[Any]      	=						tmp_path / "cache"
  lowercase    :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():
    lowercase    :Optional[Any]      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  lowercase    :Tuple      	=						tmp_path / "cache"
  lowercase    :Optional[Any]      	=						{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowercase    :Tuple      	=						features.copy() if features else default_expected_features
  lowercase    :str      	=						(
      Features({feature: Value(lowerCamelCase   ) for feature, dtype in features.items()}   ) if features is not None else None
  )
  lowercase    :List[str]      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  lowercase    :Optional[int]      	=						tmp_path / "cache"
  lowercase    :Tuple      	=						{"col_3": "float64", "col_1": "string", "col_2": "int64"}
  lowercase    :Tuple      	=						features.copy() if features else default_expected_features
  lowercase    :Optional[int]      	=						(
      Features({feature: Value(lowerCamelCase   ) for feature, dtype in features.items()}   ) if features is not None else None
  )
  lowercase    :str      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase   ):
  # jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"}
  lowercase    :Dict      	=						{"col_2": "int64", "col_3": "float64", "col_1": "string"}
  lowercase    :Optional[Any]      	=						features.copy()
  lowercase    :str      	=						(
      Features({feature: Value(lowerCamelCase   ) for feature, dtype in features.items()}   ) if features is not None else None
  )
  lowercase    :int      	=						tmp_path / "cache"
  lowercase    :List[str]      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  lowercase    :List[Any]      	=						tmp_path / "cache"
  lowercase    :Optional[int]      	=						{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowercase    :str      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  if issubclass(lowerCamelCase,			lowerCamelCase   ):
    lowercase    :Union[str, Any]      	=						jsonl_path
  elif issubclass(lowerCamelCase,			lowerCamelCase   ):
    lowercase    :int      	=						[jsonl_path]
  lowercase    :List[Any]      	=						tmp_path / "cache"
  lowercase    :Optional[int]      	=						{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowercase    :Dict      	=						JsonDatasetReader(lowerCamelCase,			cache_dir=lowerCamelCase   ).read()
  _check_json_dataset(lowerCamelCase,			lowerCamelCase   )



def       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase=("train",)   ):
  assert isinstance(lowerCamelCase,			lowerCamelCase   )
  for split in splits:
    lowercase    :List[Any]      	=						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__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  lowercase    :List[str]      	=						tmp_path / "cache"
  lowercase    :Optional[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():
    lowercase    :Tuple      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  lowercase    :List[Any]      	=						tmp_path / "cache"
  lowercase    :str      	=						{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowercase    :Optional[Any]      	=						features.copy() if features else default_expected_features
  lowercase    :Union[str, Any]      	=						(
      Features({feature: Value(lowerCamelCase   ) for feature, dtype in features.items()}   ) if features is not None else None
  )
  lowercase    :List[str]      	=						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       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase,			lowerCamelCase   ):
  if split:
    lowercase    :str      	=						{split: jsonl_path}
  else:
    lowercase    :Dict      	=						"train"
    lowercase    :Any      	=						{"train": jsonl_path, "test": jsonl_path}
  lowercase    :Tuple      	=						tmp_path / "cache"
  lowercase    :List[str]      	=						{"col_1": "string", "col_2": "int64", "col_3": "float64"}
  lowercase    :Dict      	=						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       UpperCAmelCase__  (     lowerCamelCase   ):
  return json.load(lowerCamelCase   )



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







class    __lowerCAmelCase       :






       @pytest.mark.parametrize("lines, load_json_function"       ,					[(True, load_json_lines), (False, load_json)]				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Tuple       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					List[Any]       ,					_lowerCAmelCase:					Optional[int]				):
         with io.BytesIO() as buffer:
           JsonDatasetWriter(_lowerCAmelCase       ,					_lowerCAmelCase       ,					lines=_lowerCAmelCase				).write()
           buffer.seek(0				)
           lowercase    :Optional[Any]      	=						load_json_function(_lowerCAmelCase				)
         assert isinstance(_lowerCAmelCase       ,					_lowerCAmelCase				)
         assert isinstance(exported_content[0]       ,					_lowerCAmelCase				)
         assert len(_lowerCAmelCase				) == 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]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					str				):
         with io.BytesIO() as buffer:
           JsonDatasetWriter(_lowerCAmelCase       ,					_lowerCAmelCase       ,					lines=_lowerCAmelCase       ,					orient=_lowerCAmelCase				).write()
           buffer.seek(0				)
           lowercase    :Union[str, Any]      	=						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]				) == 10
         else:
           assert len(_lowerCAmelCase				) == 10






       @pytest.mark.parametrize("lines, load_json_function"       ,					[(True, load_json_lines), (False, load_json)]				)
       def 					SCREAMING_SNAKE_CASE    (   self:					Dict       ,					_lowerCAmelCase:					Union[str, Any]       ,					_lowerCAmelCase:					List[str]       ,					_lowerCAmelCase:					List[Any]				):
         with io.BytesIO() as buffer:
           JsonDatasetWriter(_lowerCAmelCase       ,					_lowerCAmelCase       ,					lines=_lowerCAmelCase       ,					num_proc=2				).write()
           buffer.seek(0				)
           lowercase    :List[str]      	=						load_json_function(_lowerCAmelCase				)
         assert isinstance(_lowerCAmelCase       ,					_lowerCAmelCase				)
         assert isinstance(exported_content[0]       ,					_lowerCAmelCase				)
         assert len(_lowerCAmelCase				) == 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:					Tuple       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Optional[int]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					int       ,					_lowerCAmelCase:					Any				):
         with io.BytesIO() as buffer:
           JsonDatasetWriter(_lowerCAmelCase       ,					_lowerCAmelCase       ,					lines=_lowerCAmelCase       ,					orient=_lowerCAmelCase       ,					num_proc=2				).write()
           buffer.seek(0				)
           lowercase    :List[str]      	=						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]				) == 10
         else:
           assert len(_lowerCAmelCase				) == 10






       def 					SCREAMING_SNAKE_CASE    (   self:					List[str]       ,					_lowerCAmelCase:					int				):
         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 					SCREAMING_SNAKE_CASE    (   self:					Any       ,					_lowerCAmelCase:					str       ,					_lowerCAmelCase:					Optional[Any]       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Any       ,					_lowerCAmelCase:					Any				):
         lowercase    :Tuple      	=						tmp_path_factory.mktemp("data"				) / F"test.json.{extension}"
         lowercase    :Optional[Any]      	=						str(shared_datadir / F"test_file.json.{extension}"				)
         JsonDatasetWriter(_lowerCAmelCase       ,					_lowerCAmelCase       ,					compression=_lowerCAmelCase				).write()

         with fsspec.open(_lowerCAmelCase       ,					"rb"       ,					compression="infer"				) as f:
           lowercase    :List[Any]      	=						f.read()
         with fsspec.open(_lowerCAmelCase       ,					"rb"       ,					compression="infer"				) as f:
           lowercase    :Optional[Any]      	=						f.read()
         assert exported_content == original_content









def       UpperCAmelCase__  (     lowerCamelCase,			lowerCamelCase   ):
  if discount_rate < 0:
    raise ValueError("Discount rate cannot be negative"   )
  if not cash_flows:
    raise ValueError("Cash flows list cannot be empty"   )
  lowercase    :Dict      	=						sum(
      cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(lowerCamelCase   )   )
  return round(lowerCamelCase,			ndigits=2   )


if __name__ == "__main__":
     import doctest

     doctest.testmod()



