





import time
from dataclasses import dataclass
from multiprocessing import Pool
from unittest import TestCase
from unittest.mock import patch

import multiprocess
import numpy as np
import pytest

from datasets.utils.py_utils import (
    NestedDataStructure,
    asdict,
    iflatmap_unordered,
    map_nested,
    temp_seed,
    temporary_assignment,
    zip_dict,
)

from .utils import require_tf, require_torch





def 					lowerCAmelCase_			(	__a		)					->		Tuple:  # picklable for multiprocessing







  """simple docstring"""





  return x.sum()





def 					lowerCAmelCase_			(	__a		)					->		str:  # picklable for multiprocessing







  """simple docstring"""





  return i + 1



@dataclass
class        _SCREAMING_SNAKE_CASE						:







 '''simple docstring'''
 lowercase_		=      42
 lowercase_		=      42



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->int:



   '''simple docstring'''



   lowerCamelCase__:				Union[str, Any]        ={}
   lowerCamelCase__:				str        =[]
   lowerCamelCase__:				List[Any]        =1
   lowerCamelCase__:				List[str]        =[1, 2]
   lowerCamelCase__:				List[str]        ={'a': 1, 'b': 2}
   lowerCamelCase__:				int        ={'a': [1, 2], 'b': [3, 4]}
   lowerCamelCase__:				Dict        ={'a': {'1': 1}, 'b': 2}
   lowerCamelCase__:				Union[str, Any]        ={'a': 1, 'b': 2, 'c': 3, 'd': 4}
   lowerCamelCase__:				List[str]        ={}
   lowerCamelCase__:				Dict        =[]
   lowerCamelCase__:				List[Any]        =2
   lowerCamelCase__:				Tuple        =[2, 3]
   lowerCamelCase__:				List[str]        ={'a': 2, 'b': 3}
   lowerCamelCase__:				Optional[int]        ={'a': [2, 3], 'b': [4, 5]}
   lowerCamelCase__:				Optional[Any]        ={'a': {'1': 2}, 'b': 3}
   lowerCamelCase__:				List[Any]        ={'a': 2, 'b': 3, 'c': 4, 'd': 5}
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_)	,			UpperCAmelCase_)

   lowerCamelCase__:				Union[str, Any]        =2
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)

   lowerCamelCase__:				Tuple        ={'a': np.eye(2), 'b': np.zeros(3), 'c': np.ones(2)}
   lowerCamelCase__:				Any        ={'a': 2, 'b': 0, 'c': 2}
   lowerCamelCase__:				List[Any]        ={
       'a': np.eye(2).astype(UpperCAmelCase_),
       'b': np.zeros(3).astype(UpperCAmelCase_),
       'c': np.ones(2).astype(UpperCAmelCase_),
   }
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			map_numpy=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(
       {k: v.tolist() for k, v in map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			map_numpy=UpperCAmelCase_).items()}	,			{k: v.tolist() for k, v in expected_map_nested_sna_int.items()}	,			)
   self.assertEqual(map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			map_numpy=UpperCAmelCase_	,			num_proc=UpperCAmelCase_)	,			UpperCAmelCase_)
   self.assertEqual(
       {k: v.tolist() for k, v in map_nested(UpperCAmelCase_	,			UpperCAmelCase_	,			map_numpy=UpperCAmelCase_	,			num_proc=UpperCAmelCase_).items()}	,			{k: v.tolist() for k, v in expected_map_nested_sna_int.items()}	,			)
   with self.assertRaises(UpperCAmelCase_):  # can't pickle a local lambda
     map_nested(lambda UpperCAmelCase_: x + 1	,			UpperCAmelCase_	,			num_proc=UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_ (self    :       Optional[int])	->Optional[int]:



   '''simple docstring'''



   lowerCamelCase__:				Tuple        ={'a': 1, 'b': 2}
   lowerCamelCase__:				Tuple        ={'a': 3, 'b': 4}
   lowerCamelCase__:				Union[str, Any]        ={'a': 5, 'b': 6}
   lowerCamelCase__:				Optional[Any]        =sorted([("a", (1, 3, 5)), ("b", (2, 4, 6))])
   self.assertEqual(sorted(zip_dict(UpperCAmelCase_	,			UpperCAmelCase_	,			UpperCAmelCase_))	,			UpperCAmelCase_)







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[str])	->Union[str, Any]:



   '''simple docstring'''



   class        _SCREAMING_SNAKE_CASE						:







    '''simple docstring'''
    lowercase_		=      "bar"



   lowerCamelCase__:				Any        =Foo()
   self.assertEqual(foo.my_attr	,			"bar")
   with temporary_assignment(UpperCAmelCase_	,			"my_attr"	,			"BAR"):
     self.assertEqual(foo.my_attr	,			"BAR")
   self.assertEqual(foo.my_attr	,			"bar")





@pytest.mark.parametrize(
    "iterable_length, num_proc, expected_num_proc"     ,   [
        (1, None, 1),
        (1, 1, 1),
        (2, None, 1),
        (2, 1, 1),
        (2, 2, 1),
        (2, 3, 1),
        (3, 2, 1),
        (16, 16, 16),
        (16, 17, 16),
        (17, 16, 16),
    ]     ,   )
def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a		)					->		Any:







  """simple docstring"""





  with patch("datasets.utils.py_utils._single_map_nested"		) as mock_single_map_nested, patch(
      "datasets.parallel.parallel.Pool"		) as mock_multiprocessing_pool:
    lowerCamelCase__:				Tuple        ={F"""{i}""": i for i in range(lowercase__		)}
    lowerCamelCase__:				Dict        =map_nested(lambda __a		: x + 10     ,   lowercase__     ,   num_proc=lowercase__     ,   parallel_min_length=16		)
    if expected_num_proc == 1:
      assert mock_single_map_nested.called
      assert not mock_multiprocessing_pool.called
    else:
      assert not mock_single_map_nested.called
      assert mock_multiprocessing_pool.called
      assert mock_multiprocessing_pool.call_args[0][0] == expected_num_proc



class        _SCREAMING_SNAKE_CASE						(     __SCREAMING_SNAKE_CASE   ):







 '''simple docstring'''

 @require_tf
 def 							SCREAMING_SNAKE_CASE_ (self    :       Any)	->Optional[int]:



   '''simple docstring'''



   import tensorflow as tf
   from tensorflow.keras import layers

   lowerCamelCase__:				List[Any]        =layers.Dense(2)

   def gen_random_output():
     lowerCamelCase__:				List[Any]        =tf.random.uniform((1, 3))
     return model(UpperCAmelCase_).numpy()

   with temp_seed(42	,			set_tensorflow=UpperCAmelCase_):
     lowerCamelCase__:				Union[str, Any]        =gen_random_output()
   with temp_seed(42	,			set_tensorflow=UpperCAmelCase_):
     lowerCamelCase__:				Optional[Any]        =gen_random_output()
   lowerCamelCase__:				Union[str, Any]        =gen_random_output()

   np.testing.assert_equal(UpperCAmelCase_	,			UpperCAmelCase_)
   self.assertGreater(np.abs(outa - outa).sum()	,			0)

 @require_torch
 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->List[Any]:



   '''simple docstring'''



   import torch

   def gen_random_output():
     lowerCamelCase__:				Any        =torch.nn.Linear(3	,			2)
     lowerCamelCase__:				str        =torch.rand(1	,			3)
     return model(UpperCAmelCase_).detach().numpy()

   with temp_seed(42	,			set_pytorch=UpperCAmelCase_):
     lowerCamelCase__:				Any        =gen_random_output()
   with temp_seed(42	,			set_pytorch=UpperCAmelCase_):
     lowerCamelCase__:				List[str]        =gen_random_output()
   lowerCamelCase__:				Union[str, Any]        =gen_random_output()

   np.testing.assert_equal(UpperCAmelCase_	,			UpperCAmelCase_)
   self.assertGreater(np.abs(outa - outa).sum()	,			0)







 def 							SCREAMING_SNAKE_CASE_ (self    :       List[Any])	->str:



   '''simple docstring'''



   def gen_random_output():
     return np.random.rand(1	,			3)

   with temp_seed(42):
     lowerCamelCase__:				List[str]        =gen_random_output()
   with temp_seed(42):
     lowerCamelCase__:				List[Any]        =gen_random_output()
   lowerCamelCase__:				Dict        =gen_random_output()

   np.testing.assert_equal(UpperCAmelCase_	,			UpperCAmelCase_)
   self.assertGreater(np.abs(outa - outa).sum()	,			0)





@pytest.mark.parametrize("input_data"     ,   [{}]		)
def 					lowerCAmelCase_			(	__a		)					->		Any:







  """simple docstring"""





  lowerCamelCase__:				Dict        =NestedDataStructure(lowercase__		).data
  assert output_data == input_data





@pytest.mark.parametrize(
    "data, expected_output"     ,   [
        ({}, []),
        ([], []),
        ("foo", ["foo"]),
        (["foo", "bar"], ["foo", "bar"]),
        ([["foo", "bar"]], ["foo", "bar"]),
        ([[["foo"], ["bar"]]], ["foo", "bar"]),
        ([[["foo"], "bar"]], ["foo", "bar"]),
        ({"a": 1, "b": 2}, [1, 2]),
        ({"a": [1, 2], "b": [3, 4]}, [1, 2, 3, 4]),
        ({"a": [[1, 2]], "b": [[3, 4]]}, [1, 2, 3, 4]),
        ({"a": [[1, 2]], "b": [3, 4]}, [1, 2, 3, 4]),
        ({"a": [[[1], [2]]], "b": [[[3], [4]]]}, [1, 2, 3, 4]),
        ({"a": [[[1], [2]]], "b": [[3, 4]]}, [1, 2, 3, 4]),
        ({"a": [[[1], [2]]], "b": [3, 4]}, [1, 2, 3, 4]),
        ({"a": [[[1], [2]]], "b": [3, [4]]}, [1, 2, 3, 4]),
        ({"a": {"1": 1}, "b": 2}, [1, 2]),
        ({"a": {"1": [1]}, "b": 2}, [1, 2]),
        ({"a": {"1": [1]}, "b": [2]}, [1, 2]),
    ]     ,   )
def 					lowerCAmelCase_			(	__a     ,   __a		)					->		List[str]:







  """simple docstring"""





  lowerCamelCase__:				str        =NestedDataStructure(lowercase__		).flatten()
  assert output == expected_output





def 					lowerCAmelCase_			(	)					->		List[Any]:







  """simple docstring"""





  lowerCamelCase__:				Tuple        =A(x=1     ,   y="foobar"		)
  lowerCamelCase__:				List[str]        ={'x': 1, 'y': 'foobar'}
  assert asdict(lowercase__		) == expected_output

  lowerCamelCase__:				Dict        ={'a': {'b': A(x=10     ,   y="foo"		)}, 'c': [A(x=20     ,   y="bar"		)]}
  lowerCamelCase__:				int        ={'a': {'b': {'x': 10, 'y': 'foo'}}, 'c': [{'x': 20, 'y': 'bar'}]}
  assert asdict(lowercase__		) == expected_output

  with pytest.raises(lowercase__		):
    asdict([1, A(x=10     ,   y="foo"		)]		)





def 					lowerCAmelCase_			(	__a		)					->		List[str]:







  """simple docstring"""





  return text.split()





def 					lowerCAmelCase_			(	__a		)					->		str:







  """simple docstring"""





  yield (time.time(), content)
  time.sleep(2		)
  yield (time.time(), content)






def 					lowerCAmelCase_			(	)					->		str:







  """simple docstring"""





  with Pool(2		) as pool:
    lowerCamelCase__:				List[str]        =list(iflatmap_unordered(lowercase__     ,   _split_text     ,   kwargs_iterable=[{"text": "hello there"}] * 10		)		)
    assert out.count("hello"		) == 10
    assert out.count("there"		) == 10
    assert len(lowercase__		) == 20

  # check multiprocess from pathos (uses dill for pickling)
  with multiprocess.Pool(2		) as pool:
    lowerCamelCase__:				List[str]        =list(iflatmap_unordered(lowercase__     ,   _split_text     ,   kwargs_iterable=[{"text": "hello there"}] * 10		)		)
    assert out.count("hello"		) == 10
    assert out.count("there"		) == 10
    assert len(lowercase__		) == 20

  # check that we get items as fast as possible
  with Pool(2		) as pool:
    lowerCamelCase__:				int        =[]
    for yield_time, content in iflatmap_unordered(
        lowercase__     ,   _aseconds_generator_of_aitems_with_timing     ,   kwargs_iterable=[{"content": "a"}, {"content": "b"}]		):
      assert yield_time < time.time() + 0.1, "we should each item directly after it was yielded"
      out.append(lowercase__		)
    assert out.count("a"		) == 2
    assert out.count("b"		) == 2
    assert len(lowercase__		) == 4








from math import pow





def 					lowerCAmelCase_			(	__a     ,   __a     ,   __a     ,   __a     ,   __a     ,   )					->		tuple[int, int]:







  """simple docstring"""





  if current_sum == needed_sum:
    # If the sum of the powers is equal to needed_sum, then we have a solution.
    solutions_count += 1
    return current_sum, solutions_count

  lowerCamelCase__:				Optional[Any]        =int(pow(__a     ,   __a		)		)
  if current_sum + i_to_n <= needed_sum:
    # If the sum of the powers is less than needed_sum, then continue adding powers.
    current_sum += i_to_n
    lowerCamelCase__  , lowerCamelCase__:				int        =backtrack(
        __a     ,   __a     ,   current_number + 1     ,   __a     ,   __a		)
    current_sum -= i_to_n
  if i_to_n < needed_sum:
    # If the power of i is less than needed_sum, then try with the next power.
    lowerCamelCase__  , lowerCamelCase__:				Dict        =backtrack(
        __a     ,   __a     ,   current_number + 1     ,   __a     ,   __a		)
  return current_sum, solutions_count





def 					lowerCAmelCase_			(	__a     ,   __a		)					->		int:







  """simple docstring"""





  if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):
    raise ValueError(
        "Invalid input\n"
        "needed_sum must be between 1 and 1000, power between 2 and 10."		)

  return backtrack(__a     ,   __a     ,   1     ,   0     ,   0		)[1]  # Return the solutions_count


if __name__ == "__main__":
     import doctest

     doctest.testmod()

