





import importlib
import inspect
from functools import wraps
from typing import TYPE_CHECKING, Optional, Union

from .download.streaming_download_manager import (
    xbasename,
    xdirname,
    xet_parse,
    xexists,
    xgetsize,
    xglob,
    xgzip_open,
    xisdir,
    xisfile,
    xjoin,
    xlistdir,
    xnumpy_load,
    xopen,
    xpandas_read_csv,
    xpandas_read_excel,
    xPath,
    xrelpath,
    xsio_loadmat,
    xsplit,
    xsplitext,
    xwalk,
    xxml_dom_minidom_parse,
)
from .utils.logging import get_logger
from .utils.patching import patch_submodule
from .utils.py_utils import get_imports


UpperCAmelCase_											=  get_logger(__name__)


if TYPE_CHECKING:
   from .builder import DatasetBuilder
def     A__					(						SCREAMING_SNAKE_CASE_      :			Dict							,  SCREAMING_SNAKE_CASE_      :			Optional[Union[str, bool]] = None	)     ->     Any:






   """simple docstring"""







   _UpperCAmelCase											=      importlib.import_module(SCREAMING_SNAKE_CASE_	)

   # TODO(QL): always update the module to add subsequent new authentication
   if hasattr(SCREAMING_SNAKE_CASE_							,  '''_patched_for_streaming'''	) and module._patched_for_streaming:
      return

   def wrap_auth(SCREAMING_SNAKE_CASE_      :			Any	):
      @wraps(SCREAMING_SNAKE_CASE_	)
      def wrapper(*SCREAMING_SNAKE_CASE_      :			List[str]							,  **SCREAMING_SNAKE_CASE_      :			Optional[int]	):
         return function(*SCREAMING_SNAKE_CASE_							,  use_auth_token=SCREAMING_SNAKE_CASE_							,  **SCREAMING_SNAKE_CASE_	)

      _UpperCAmelCase											=      '''wrap_auth'''
      return wrapper

   # open files in a streaming fashion
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''open'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.listdir'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.walk'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''glob.glob'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   # allow to navigate in remote zip files
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.join'''							,  SCREAMING_SNAKE_CASE_	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.dirname'''							,  SCREAMING_SNAKE_CASE_	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.basename'''							,  SCREAMING_SNAKE_CASE_	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.relpath'''							,  SCREAMING_SNAKE_CASE_	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.split'''							,  SCREAMING_SNAKE_CASE_	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.splitext'''							,  SCREAMING_SNAKE_CASE_	).start()
   # allow checks on paths
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.exists'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.isdir'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.isfile'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''os.path.getsize'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''pathlib.Path'''							,  SCREAMING_SNAKE_CASE_	).start()
   # file readers
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''gzip.open'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''numpy.load'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''pandas.read_csv'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)							,  attrs=['''__version__''']	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''pandas.read_excel'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)							,  attrs=['''__version__''']	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''scipy.io.loadmat'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)							,  attrs=['''__version__''']	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''xml.etree.ElementTree.parse'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   patch_submodule(SCREAMING_SNAKE_CASE_							,  '''xml.dom.minidom.parse'''							,  wrap_auth(SCREAMING_SNAKE_CASE_	)	).start()
   _UpperCAmelCase											=      True
def     A__					(						SCREAMING_SNAKE_CASE_      :			"DatasetBuilder"	)     ->     Tuple:






   """simple docstring"""







   extend_module_for_streaming(builder.__module__							,  use_auth_token=builder.use_auth_token	)
   # if needed, we also have to extend additional internal imports (like wmt14 -> wmt_utils)
   if not builder.__module__.startswith('''datasets.'''	):  # check that it's not a packaged builder like csv
      for imports in get_imports(inspect.getfile(builder.__class__	)	):
         if imports[0] == "internal":
            _UpperCAmelCase											=      imports[1]
            _UpperCAmelCase											=      '''.'''.join(builder.__module__.split('''.'''	)[:-1] + [internal_import_name]	)
            extend_module_for_streaming(SCREAMING_SNAKE_CASE_							,  use_auth_token=builder.use_auth_token	)

    # builders can inherit from other builders that might use streaming functionality
    # (for example, ImageFolder and AudioFolder inherit from FolderBuilder which implements examples generation)
    # but these parents builders are not patched automatically as they are not instantiated, so we patch them here
   from .builder import DatasetBuilder

   _UpperCAmelCase											=      [
       cls.__module__
       for cls in type(SCREAMING_SNAKE_CASE_	).__mro__[1:]  # make sure it's not the same module we've already patched
       if issubclass(cls							,  SCREAMING_SNAKE_CASE_	) and cls.__module__ != DatasetBuilder.__module__
   ]  # check it's not a standard builder from datasets.builder
   for module in parent_builder_modules:
      extend_module_for_streaming(SCREAMING_SNAKE_CASE_							,  use_auth_token=builder.use_auth_token	)






def     A__					(						SCREAMING_SNAKE_CASE_      :			list	)     ->     bool:






   """simple docstring"""







   if not isinstance(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_	):
      raise ValueError('''Input series is not valid, valid series - [1, 2/3, 2]'''	)
   if len(SCREAMING_SNAKE_CASE_	) == 0:
      raise ValueError('''Input list must be a non empty list'''	)
   if len(SCREAMING_SNAKE_CASE_	) == 1 and series[0] != 0:
      return True
   _UpperCAmelCase											=      []
   _UpperCAmelCase											=      len(SCREAMING_SNAKE_CASE_	)
   for i in range(0							,  SCREAMING_SNAKE_CASE_	):
      if series[i] == 0:
         raise ValueError('''Input series cannot have 0 as an element'''	)
      rec_series.append(1 / series[i]	)
   _UpperCAmelCase											=      rec_series[1] - rec_series[0]
   for index in range(2							,  SCREAMING_SNAKE_CASE_	):
      if rec_series[index] - rec_series[index - 1] != common_diff:
         return False
   return True
def     A__					(						SCREAMING_SNAKE_CASE_      :			list	)     ->     float:






   """simple docstring"""







   if not isinstance(SCREAMING_SNAKE_CASE_							,  SCREAMING_SNAKE_CASE_	):
      raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''	)
   if len(SCREAMING_SNAKE_CASE_	) == 0:
      raise ValueError('''Input list must be a non empty list'''	)
   _UpperCAmelCase											=      0
   for val in series:
      answer += 1 / val
   return len(SCREAMING_SNAKE_CASE_	) / answer


if __name__ == "__main__":
   import doctest

   doctest.testmod()