






'''simple docstring'''







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 		_UpperCAmelCase   (							__A							:   Optional[int]      ):  # picklable for multiprocessing
				return x.sum()





def 		_UpperCAmelCase   (							__A							:   Optional[Any]      ):  # picklable for multiprocessing
				return i + 1





@dataclass
class 	SCREAMING_SNAKE_CASE					:
			snake_case__  =				42
			snake_case__  =				42





class 	SCREAMING_SNAKE_CASE					(					_UpperCAmelCase       ):






			def 			SCREAMING_SNAKE_CASE      ( self      :			Tuple					)			->			int:
							a_		:  Tuple											=      {}
							a_		:  Any											=      []
							a_		:  Any											=      1
							a_		:  List[str]											=      [1, 2]
							a_		:  Dict											=      {"""a""": 1, """b""": 2}
							a_		:  Tuple											=      {"""a""": [1, 2], """b""": [3, 4]}
							a_		:  Optional[Any]											=      {"""a""": {"""1""": 1}, """b""": 2}
							a_		:  List[str]											=      {"""a""": 1, """b""": 2, """c""": 3, """d""": 4}
							a_		:  List[str]											=      {}
							a_		:  List[Any]											=      []
							a_		:  Optional[int]											=      2
							a_		:  List[Any]											=      [2, 3]
							a_		:  List[str]											=      {"""a""": 2, """b""": 3}
							a_		:  Tuple											=      {"""a""": [2, 3], """b""": [4, 5]}
							a_		:  Optional[Any]											=      {"""a""": {"""1""": 2}, """b""": 3}
							a_		:  Optional[int]											=      {"""a""": 2, """b""": 3, """c""": 4, """d""": 5}
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__					)  ,						lowercase__					)

							a_		:  Optional[Any]											=      2
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)
							self.assertEqual(map_nested(lowercase__  ,						lowercase__  ,						num_proc=lowercase__					)  ,						lowercase__					)

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






			def 			SCREAMING_SNAKE_CASE      ( self      :			int					)			->			int:
							a_		:  List[str]											=      {"""a""": 1, """b""": 2}
							a_		:  Dict											=      {"""a""": 3, """b""": 4}
							a_		:  Dict											=      {"""a""": 5, """b""": 6}
							a_		:  List[Any]											=      sorted([('''a''', (1, 3, 5)), ('''b''', (2, 4, 6))]					)
							self.assertEqual(sorted(zip_dict(lowercase__  ,						lowercase__  ,						lowercase__					)					)  ,						lowercase__					)







			def 			SCREAMING_SNAKE_CASE      ( self      :			Dict					)			->			List[str]:





							class 	SCREAMING_SNAKE_CASE					:
										snake_case__  =				"""bar"""





							a_		:  Dict											=      Foo()
							self.assertEqual(foo.my_attr  ,						'''bar'''					)
							with temporary_assignment(lowercase__  ,						'''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 		_UpperCAmelCase   (							__A							:   Tuple						,		__A							:   int						,		__A							:   Optional[Any]      ):





				with patch('''datasets.utils.py_utils._single_map_nested'''      ) as mock_single_map_nested, patch(
				    '''datasets.parallel.parallel.Pool'''      ) as mock_multiprocessing_pool:
								a_		:  Optional[Any]											=      {f'{i}': i for i in range(SCREAMING_SNAKE_CASE__      )}
								a_		:  Union[str, Any]											=      map_nested(lambda __A      : x + 10						,		SCREAMING_SNAKE_CASE__						,		num_proc=SCREAMING_SNAKE_CASE__						,		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					(					_UpperCAmelCase       ):






			@require_tf
			def 			SCREAMING_SNAKE_CASE      ( self      :			Optional[int]					)			->			int:
							import tensorflow as tf
							from tensorflow.keras import layers

							a_		:  Tuple											=      layers.Dense(2					)

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

							with temp_seed(42  ,						set_tensorflow=lowercase__					):
											a_		:  Tuple											=      gen_random_output()
							with temp_seed(42  ,						set_tensorflow=lowercase__					):
											a_		:  str											=      gen_random_output()
							a_		:  Optional[int]											=      gen_random_output()

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






			@require_torch
			def 			SCREAMING_SNAKE_CASE      ( self      :			Any					)			->			int:
							import torch

							def gen_random_output():
											a_		:  Any											=      torch.nn.Linear(3  ,						2					)
											a_		:  Optional[int]											=      torch.rand(1  ,						3					)
											return model(lowercase__					).detach().numpy()

							with temp_seed(42  ,						set_pytorch=lowercase__					):
											a_		:  Optional[Any]											=      gen_random_output()
							with temp_seed(42  ,						set_pytorch=lowercase__					):
											a_		:  Optional[Any]											=      gen_random_output()
							a_		:  Tuple											=      gen_random_output()

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







			def 			SCREAMING_SNAKE_CASE      ( self      :			Union[str, Any]					)			->			Union[str, Any]:
							def gen_random_output():
											return np.random.rand(1  ,						3					)

							with temp_seed(42					):
											a_		:  List[Any]											=      gen_random_output()
							with temp_seed(42					):
											a_		:  Union[str, Any]											=      gen_random_output()
							a_		:  Optional[Any]											=      gen_random_output()

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





@pytest.mark.parametrize('''input_data'''						,		[{}]      )
def 		_UpperCAmelCase   (							__A							:   str      ):
				a_		:  Union[str, Any]											=      NestedDataStructure(SCREAMING_SNAKE_CASE__      ).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 		_UpperCAmelCase   (							__A							:   Tuple						,		__A							:   List[Any]      ):
				a_		:  Dict											=      NestedDataStructure(SCREAMING_SNAKE_CASE__      ).flatten()
				assert output == expected_output





def 		_UpperCAmelCase   (							):
				a_		:  Union[str, Any]											=      A(x=1						,		y='''foobar'''      )
				a_		:  List[Any]											=      {"""x""": 1, """y""": """foobar"""}
				assert asdict(SCREAMING_SNAKE_CASE__      ) == expected_output

				a_		:  Tuple											=      {"""a""": {"""b""": A(x=10						,		y='''foo'''      )}, """c""": [A(x=20						,		y='''bar'''      )]}
				a_		:  Union[str, Any]											=      {"""a""": {"""b""": {"""x""": 10, """y""": """foo"""}}, """c""": [{"""x""": 20, """y""": """bar"""}]}
				assert asdict(SCREAMING_SNAKE_CASE__      ) == expected_output

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





def 		_UpperCAmelCase   (							__A							:   str      ):
				return text.split()





def 		_UpperCAmelCase   (							__A							:   Dict      ):
				yield (time.time(), content)
				time.sleep(2      )
				yield (time.time(), content)





def 		_UpperCAmelCase   (							):





				with Pool(2      ) as pool:
								a_		:  Optional[int]											=      list(iflatmap_unordered(SCREAMING_SNAKE_CASE__						,		_split_text						,		kwargs_iterable=[{'''text''': '''hello there'''}] * 10      )      )
								assert out.count('''hello'''      ) == 10
								assert out.count('''there'''      ) == 10
								assert len(SCREAMING_SNAKE_CASE__      ) == 20

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

				# check that we get items as fast as possible
				with Pool(2      ) as pool:
								a_		:  Optional[int]											=      []
								for yield_time, content in iflatmap_unordered(
								    SCREAMING_SNAKE_CASE__						,		_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(SCREAMING_SNAKE_CASE__      )
								assert out.count('''a'''      ) == 2
								assert out.count('''b'''      ) == 2
								assert len(SCREAMING_SNAKE_CASE__      ) == 4












'''simple docstring'''







import sys

__lowerCAmelCase       		=      (
    '73167176531330624919225119674426574742355349194934'
    '96983520312774506326239578318016984801869478851843'
    '85861560789112949495459501737958331952853208805511'
    '12540698747158523863050715693290963295227443043557'
    '66896648950445244523161731856403098711121722383113'
    '62229893423380308135336276614282806444486645238749'
    '30358907296290491560440772390713810515859307960866'
    '70172427121883998797908792274921901699720888093776'
    '65727333001053367881220235421809751254540594752243'
    '52584907711670556013604839586446706324415722155397'
    '53697817977846174064955149290862569321978468622482'
    '83972241375657056057490261407972968652414535100474'
    '82166370484403199890008895243450658541227588666881'
    '16427171479924442928230863465674813919123162824586'
    '17866458359124566529476545682848912883142607690042'
    '24219022671055626321111109370544217506941658960408'
    '07198403850962455444362981230987879927244284909188'
    '84580156166097919133875499200524063689912560717606'
    '05886116467109405077541002256983155200055935729725'
    '71636269561882670428252483600823257530420752963450'
)





def 		_UpperCAmelCase   (							__A							:   str      ):
				a_		:  Tuple											=      1
				for digit in s:
								product *= int(__A      )
				return product





def 		_UpperCAmelCase   (							__A							:   str = N      ):
				a_		:  Dict											=      -sys.maxsize - 1
				a_		:  Optional[int]											=      n[:13]
				a_		:  str											=      13
				while cur_index < len(__A      ) - 13:
								if int(n[cur_index]      ) >= int(substr[0]      ):
												a_		:  Tuple											=      substr[1:] + n[cur_index]
												cur_index += 1
								else:
												a_		:  Dict											=      max(__A						,		str_eval(__A      )      )
												a_		:  List[str]											=      n[cur_index : cur_index + 13]
												cur_index += 13
				return largest_product


if __name__ == "__main__":
		print(F"""{solution() = }""")




