






import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase      )			->     Tuple:  # picklable for multiprocessing
     return i + 1







@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def 		SCREAMING_SNAKE_CASE							( )			->     Tuple:
     with parallel_backend("spark"      ):
          assert ParallelBackendConfig.backend_name == "spark"

     UpperCamelCase__      :       int	    =  [1, 2, 3]
     with pytest.raises(__lowerCAmelCase      ):
          with parallel_backend("unsupported backend"      ):
               map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=2      )

     with pytest.raises(__lowerCAmelCase      ):
          with parallel_backend("unsupported backend"      ):
               map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=-1      )







@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("num_proc" ,   [2, -1]      )
def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase      )			->     Dict:
     UpperCamelCase__      :       str	    =  [1, 2]
     UpperCamelCase__      :       Union[str, Any]	    =  {"a": 1, "b": 2}
     UpperCamelCase__      :       Dict	    =  {"a": [1, 2], "b": [3, 4]}
     UpperCamelCase__      :       Dict	    =  {"a": {"1": 1}, "b": 2}
     UpperCamelCase__      :       List[Any]	    =  {"a": 1, "b": 2, "c": 3, "d": 4}
     UpperCamelCase__      :       Tuple	    =  [2, 3]
     UpperCamelCase__      :       Optional[int]	    =  {"a": 2, "b": 3}
     UpperCamelCase__      :       Optional[Any]	    =  {"a": [2, 3], "b": [4, 5]}
     UpperCamelCase__      :       Any	    =  {"a": {"1": 2}, "b": 3}
     UpperCamelCase__      :       Tuple	    =  {"a": 2, "b": 3, "c": 4, "d": 5}

     with parallel_backend("spark"      ):
          assert map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=__lowerCAmelCase      ) == expected_map_nested_sa
          assert map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=__lowerCAmelCase      ) == expected_map_nested_sa
          assert map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=__lowerCAmelCase      ) == expected_map_nested_sa
          assert map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=__lowerCAmelCase      ) == expected_map_nested_sa
          assert map_nested(__lowerCAmelCase ,   __lowerCAmelCase ,   num_proc=__lowerCAmelCase      ) == expected_map_nested_sa







from __future__ import annotations







def 		SCREAMING_SNAKE_CASE							( __lowerCAmelCase ,   __lowerCAmelCase = None ,   __lowerCAmelCase = None      )			->     None:
     if start is None:
          UpperCamelCase__      :       Union[str, Any]	    =  0

     if end is None:
          UpperCamelCase__      :       List[Any]	    =  len(__lowerCAmelCase      ) - 1

     if start >= end:
          return

     UpperCamelCase__      :       Union[str, Any]	    =  (start + end) // 2

     slowsort(__lowerCAmelCase ,   __lowerCAmelCase ,   __lowerCAmelCase      )
     slowsort(__lowerCAmelCase ,   mid + 1 ,   __lowerCAmelCase      )

     if sequence[end] < sequence[mid]:
          UpperCamelCase__    ,					UpperCamelCase__      :       Optional[int]	    =  sequence[mid], sequence[end]

     slowsort(__lowerCAmelCase ,   __lowerCAmelCase ,   end - 1      )


if __name__ == "__main__":
 from doctest import testmod

 testmod()