






'''simple docstring'''

import contextlib
from multiprocessing import Pool, RLock

from tqdm.auto import tqdm

from ..utils import experimental, logging


SCREAMING_SNAKE_CASE__											=     logging.get_logger(__name__)






class        a_		:
  lowercase  	=   None







@experimental
def 					lowercase__				(   __UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase					)->				str:
      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 					lowercase__				(   __UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase					)->				Tuple:
      UpperCamelCase       						=      num_proc if num_proc <= len(__UpperCamelCase					) else len(__UpperCamelCase					)
      UpperCamelCase       						=      []  # We organize the splits ourselve (contiguous splits)
      for index in range(__UpperCamelCase					):
            UpperCamelCase       						=      len(__UpperCamelCase					) // num_proc
            UpperCamelCase       						=      len(__UpperCamelCase					) % num_proc
            UpperCamelCase       						=      div * index + min(__UpperCamelCase	,		__UpperCamelCase					)
            UpperCamelCase       						=      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]}"					)
      UpperCamelCase						,UpperCamelCase       						=      None, None
      if not disable_tqdm:
            UpperCamelCase						,UpperCamelCase       						=      (RLock(),), tqdm.set_lock
      with Pool(__UpperCamelCase	,		initargs=__UpperCamelCase	,		initializer=__UpperCamelCase					) as pool:
            UpperCamelCase       						=      pool.map(__UpperCamelCase	,		__UpperCamelCase					)
      logger.info(F"Finished {num_proc} processes"					)
      UpperCamelCase       						=      [obj for proc_res in mapped for obj in proc_res]
      logger.info(F"Unpacked {len(__UpperCamelCase					)} objects"					)

      return mapped







def 					lowercase__				(   __UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase	,		__UpperCamelCase					)->				Tuple:
      # progress bar is not yet supported for _map_with_joblib, because tqdm couldn't accurately be applied to joblib,
      # and it requires monkey-patching joblib internal classes which is subject to change
      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 					lowercase__				(   __UpperCamelCase					)->				List[str]:
      UpperCamelCase       						=      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:
            UpperCamelCase       						=      None








'''simple docstring'''

def 					lowercase__				(   __UpperCamelCase					)->				str:
      if not all(char in """01""" for char in bin_string					):
            raise ValueError("""Non-binary value was passed to the function"""					)
      if not bin_string:
            raise ValueError("""Empty string was passed to the function"""					)
      UpperCamelCase       						=      """"""
      while len(__UpperCamelCase					) % 3 != 0:
            UpperCamelCase       						=      """0""" + bin_string
      UpperCamelCase       						=      [
          bin_string[index : index + 3]
          for index in range(len(__UpperCamelCase					)					)
          if index % 3 == 0
      ]
      for bin_group in bin_string_in_3_list:
            UpperCamelCase       						=      0
            for index, val in enumerate(__UpperCamelCase					):
                  oct_val += int(2 ** (2 - index) * int(__UpperCamelCase					)					)
            oct_string += str(__UpperCamelCase					)
      return oct_string


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

     testmod()
