






"""simple docstring"""





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


A__       :			Dict=						get_logger(__name__)


if TYPE_CHECKING:
  from .builder import DatasetBuilder







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE = None       )       ->						Tuple:
    """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       ):
        @wraps(SCREAMING_SNAKE_CASE       )
        def wrapper(*SCREAMING_SNAKE_CASE			,						**SCREAMING_SNAKE_CASE       ):
            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 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE       )       ->						str:
    """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       )












"""simple docstring"""





def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE = 10_00       )       ->						str:
    """simple docstring"""

    if (not isinstance(SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE       )) or (digit_position <= 0):
        raise ValueError('Digit position must be a positive integer'       )
    elif (not isinstance(SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE       )) or (precision < 0):
        raise ValueError('Precision must be a nonnegative integer'       )

    # compute an approximation of (16 ** (n - 1)) * pi whose fractional part is mostly
    # accurate
    UpperCamelCase__								=     (
        4 * _subsum(SCREAMING_SNAKE_CASE			,						1			,						SCREAMING_SNAKE_CASE       )
        - 2 * _subsum(SCREAMING_SNAKE_CASE			,						4			,						SCREAMING_SNAKE_CASE       )
        - _subsum(SCREAMING_SNAKE_CASE			,						5			,						SCREAMING_SNAKE_CASE       )
        - _subsum(SCREAMING_SNAKE_CASE			,						6			,						SCREAMING_SNAKE_CASE       )
    )

    # return the first hex digit of the fractional part of the result
    return hex(int((sum_result % 1) * 16       )       )[2:]







def 	lowerCAmelCase_(			SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE			,						SCREAMING_SNAKE_CASE       )       ->						float:
    """simple docstring"""

    UpperCamelCase__								=     0.0
    for sum_index in range(digit_pos_to_extract + precision       ):
        UpperCamelCase__								=     8 * sum_index + denominator_addend
        if sum_index < digit_pos_to_extract:
            # if the exponential term is an integer and we mod it by the denominator
            # before dividing, only the integer part of the sum will change;
            # the fractional part will not
            UpperCamelCase__								=     pow(
                16			,						digit_pos_to_extract - 1 - sum_index			,						SCREAMING_SNAKE_CASE       )
        else:
            UpperCamelCase__								=     pow(16			,						digit_pos_to_extract - 1 - sum_index       )
        total += exponential_term / denominator
    return total


if __name__ == "__main__":
  import doctest

  doctest.testmod()




