
'''simple docstring'''







import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


_lowercase			:  List[Any]   							=	logging.get_logger(__name__)






class 			lowerCAmelCase__       :






					"""simple docstring"""




					lowerCAmelCase_					       =					None







@experimental
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			Tuple				,				__SCREAMING_SNAKE_CASE :			List[str]				,				__SCREAMING_SNAKE_CASE :			int				,				__SCREAMING_SNAKE_CASE :			Dict				,				__SCREAMING_SNAKE_CASE :			str				,				__SCREAMING_SNAKE_CASE :			Union[str, Any]				,				__SCREAMING_SNAKE_CASE :			int							):



						"""simple docstring"""





						if ParallelBackendConfig.backend_name is None:
												return _map_with_multiprocessing_pool(
												    _UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase							)

						return _map_with_joblib(_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase				,				_UpperCAmelCase							)
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			Optional[Any]				,				__SCREAMING_SNAKE_CASE :			List[str]				,				__SCREAMING_SNAKE_CASE :			Dict				,				__SCREAMING_SNAKE_CASE :			Tuple				,				__SCREAMING_SNAKE_CASE :			Tuple				,				__SCREAMING_SNAKE_CASE :			int				,				__SCREAMING_SNAKE_CASE :			Tuple							):



						"""simple docstring"""





						lowercase_     :					List[str]													=	num_proc if num_proc <= len(_UpperCAmelCase							) else len(_UpperCAmelCase							)
						lowercase_     :					Union[str, Any]													=	[]  # We organize the splits ourselve (contiguous splits)
						for index in range(_UpperCAmelCase							):
												lowercase_     :					str													=	len(_UpperCAmelCase							) // num_proc
												lowercase_     :					Optional[Any]													=	len(_UpperCAmelCase							) % num_proc
												lowercase_     :					List[Any]													=	div * index + min(_UpperCAmelCase				,				_UpperCAmelCase							)
												lowercase_     :					List[str]													=	start + div + (1 if index < mod else 0)
												split_kwds.append((function, iterable[start:end], types, index, disable_tqdm, desc)							)

						if len(_UpperCAmelCase							) != sum(len(i[1]							) for i in split_kwds							):
												raise ValueError(
												    F'''Error dividing inputs iterable among processes. '''
												    F'''Total number of objects {len(_UpperCAmelCase							)}, '''
												    F'''length: {sum(len(i[1]							) for i in split_kwds							)}'''							)

						logger.info(
						    F'''Spawning {num_proc} processes for {len(_UpperCAmelCase							)} objects in slices of {[len(i[1]							) for i in split_kwds]}'''							)
						lowercase_     :					Optional[Any]													=	None, None
						if not disable_tqdm:
												lowercase_     :					Dict													=	(RLock(),), tqdm.set_lock
						with Pool(_UpperCAmelCase				,				initargs=_UpperCAmelCase				,				initializer=_UpperCAmelCase							) as pool:
												lowercase_     :					Optional[int]													=	pool.map(_UpperCAmelCase				,				_UpperCAmelCase							)
						logger.info(F'''Finished {num_proc} processes'''							)
						lowercase_     :					Any													=	[obj for proc_res in mapped for obj in proc_res]
						logger.info(F'''Unpacked {len(_UpperCAmelCase							)} objects'''							)

						return mapped
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			str				,				__SCREAMING_SNAKE_CASE :			Dict				,				__SCREAMING_SNAKE_CASE :			List[Any]				,				__SCREAMING_SNAKE_CASE :			Optional[Any]				,				__SCREAMING_SNAKE_CASE :			Any				,				__SCREAMING_SNAKE_CASE :			Optional[Any]				,				__SCREAMING_SNAKE_CASE :			Tuple							):



						"""simple docstring"""





						import joblib

						with joblib.parallel_backend(ParallelBackendConfig.backend_name				,				n_jobs=_UpperCAmelCase							):
												return joblib.Parallel()(
												    joblib.delayed(_UpperCAmelCase							)((function, obj, types, None, True, None)							) for obj in iterable							)




@experimental
@contextlib.contextmanager
def  snake_case_		(    __SCREAMING_SNAKE_CASE :			Optional[Any]							):



						"""simple docstring"""





						lowercase_     :					Optional[Any]													=	backend_name

						if backend_name == "spark":
												from joblibspark import register_spark

												register_spark()

												# TODO: call create_cache_and_write_probe if "download" in steps
												# TODO: raise NotImplementedError when Dataset.map etc is called

						try:
												yield
						finally:
												lowercase_     :					Tuple													=	None





'''simple docstring'''







from __future__ import annotations






class 			lowerCAmelCase__       :






					def __init__(       self    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											lowercase_     :					Any													=	key






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					List[str]													=	key or self.__key or 1

											# make sure key is an appropriate size
											key %= 2_55

											return [chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		) for ch in content]






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					Optional[int]													=	key or self.__key or 1

											# make sure key is an appropriate size
											key %= 2_55

											return [chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		) for ch in content]






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					List[Any]													=	key or self.__key or 1

											# make sure key can be any size
											while key > 2_55:
																	key -= 2_55

											# This will be returned
											lowercase_     :					str													=	''''''

											for ch in content:
																	ans += chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		)

											return ans






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											lowercase_     :					Any													=	key or self.__key or 1

											# make sure key can be any size
											while key > 2_55:
																	key -= 2_55

											# This will be returned
											lowercase_     :					Dict													=	''''''

											for ch in content:
																	ans += chr(ord(__SCREAMING_SNAKE_CASE		) ^ key		)

											return ans






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE = 0		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											try:
																	with open(__SCREAMING_SNAKE_CASE		) as fin, open('''encrypt.out'''    ,							'''w+'''		) as fout:
																							# actual encrypt-process
																							for line in fin:
																													fout.write(self.encrypt_string(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)		)

											except OSError:
																	return False

											return True






					def      _snake_case   (       self    ,							__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		):


											"""simple docstring"""





											assert isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		) and isinstance(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)

											try:
																	with open(__SCREAMING_SNAKE_CASE		) as fin, open('''decrypt.out'''    ,							'''w+'''		) as fout:
																							# actual encrypt-process
																							for line in fin:
																													fout.write(self.decrypt_string(__SCREAMING_SNAKE_CASE    ,							__SCREAMING_SNAKE_CASE		)		)

											except OSError:
																	return False

											return True


# Tests
# crypt = XORCipher()
# key = 67

# # test encrypt
# print(crypt.encrypt("hallo welt",key))
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))

# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))

# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))

# if (crypt.encrypt_file("test.txt",key)):
#       print("encrypt successful")
# else:
#       print("encrypt unsuccessful")

# if (crypt.decrypt_file("encrypt.out",key)):
#       print("decrypt successful")
# else:
#       print("decrypt unsuccessful")



