
import sys
from dataclasses import dataclass
from typing import TYPE_CHECKING, Dict, List, Optional, Tuple, Union

import pandas as pd
import pyarrow as pa

import datasets
import datasets.config
from datasets.features.features import require_storage_cast
from datasets.table import table_cast


if TYPE_CHECKING:
      import sqlitea

      import sqlalchemy


_A							=       datasets.utils.logging.get_logger(__name__)

@dataclass
class 		lowercase_							(							datasets.BuilderConfig					):
 A__    :					Union[str, "sqlalchemy.sql.Selectable"]    =     None
 A__    :					Union[str, "sqlalchemy.engine.Connection", "sqlalchemy.engine.Engine", "sqlite3.Connection"]    =     None
 A__    :					Optional[Union[str, List[str]]]    =     None
 A__    :					bool    =     True
 A__    :					Optional[Union[List, Tuple, Dict]]    =     None
 A__    :					Optional[Union[List, Dict]]    =     None
 A__    :					Optional[List[str]]    =     None
 A__    :					Optional[int]    =     1_0000
 A__    :					Optional[datasets.Features]    =     None

 def lowerCamelCase_					(   self  ):




        """simple docstring"""


        if self.sql is None:
               raise ValueError("""sql must be specified"""  )
        if self.con is None:
               raise ValueError("""con must be specified"""  )

 def lowerCamelCase_					(   self  ,						__UpperCamelCase  ,						__UpperCamelCase = None  ,						):




        """simple docstring"""


        UpperCamelCase_											=							config_kwargs.copy()
        # We need to stringify the Selectable object to make its hash deterministic

        # The process of stringifying is explained here: http://docs.sqlalchemy.org/en/latest/faq/sqlexpressions.html
        UpperCamelCase_											=							config_kwargs["""sql"""]
        if not isinstance(__UpperCamelCase  ,						__UpperCamelCase  ):
               if datasets.config.SQLALCHEMY_AVAILABLE and "sqlalchemy" in sys.modules:
                      import sqlalchemy

                      if isinstance(__UpperCamelCase  ,						sqlalchemy.sql.Selectable  ):
                             UpperCamelCase_											=							sqlalchemy.create_engine(config_kwargs["""con"""].split("""://"""  )[0] + """://"""  )
                             UpperCamelCase_											=							str(sql.compile(dialect=engine.dialect  )  )
                             UpperCamelCase_											=							sql_str
                      else:
                             raise TypeError(
                                 f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(__UpperCamelCase  )}: {sql}'''  )
               else:
                      raise TypeError(
                          f'''Supported types for \'sql\' are string and sqlalchemy.sql.Selectable but got {type(__UpperCamelCase  )}: {sql}'''  )
        UpperCamelCase_											=							config_kwargs["""con"""]
        if not isinstance(__UpperCamelCase  ,						__UpperCamelCase  ):
               UpperCamelCase_											=							id(__UpperCamelCase  )
               logger.info(
                   f'''SQL connection \'con\' of type {type(__UpperCamelCase  )} couldn\'t be hashed properly. To enable hashing, specify \'con\' as URI string instead.'''  )

        return super().create_config_id(__UpperCamelCase  ,						custom_features=__UpperCamelCase  )

 @property
 def lowerCamelCase_					(   self  ):




        """simple docstring"""


        UpperCamelCase_											=							{
            """index_col""": self.index_col,
            """columns""": self.columns,
            """params""": self.params,
            """coerce_float""": self.coerce_float,
            """parse_dates""": self.parse_dates,
        }
        return pd_read_sql_kwargs



class 		lowercase_							(							datasets.ArrowBasedBuilder					):
 A__    :					List[Any]    =     SqlConfig

 def lowerCamelCase_					(   self  ):




        """simple docstring"""


        return datasets.DatasetInfo(features=self.config.features  )

 def lowerCamelCase_					(   self  ,						__UpperCamelCase  ):




        """simple docstring"""


        return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,						gen_kwargs={}  )]

 def lowerCamelCase_					(   self  ,						__UpperCamelCase  ):




        """simple docstring"""


        if self.config.features is not None:
               UpperCamelCase_											=							self.config.features.arrow_schema
               if all(not require_storage_cast(__UpperCamelCase  ) for feature in self.config.features.values()  ):
                      # cheaper cast
                      UpperCamelCase_											=							pa.Table.from_arrays([pa_table[field.name] for field in schema]  ,						schema=__UpperCamelCase  )
               else:
                      # more expensive cast; allows str <-> int/float or str to Audio for example
                      UpperCamelCase_											=							table_cast(__UpperCamelCase  ,						__UpperCamelCase  )
        return pa_table

 def lowerCamelCase_					(   self  ):




        """simple docstring"""


        UpperCamelCase_											=							self.config.chunksize
        UpperCamelCase_											=							pd.read_sql(
            self.config.sql  ,						self.config.con  ,						chunksize=__UpperCamelCase  ,						**self.config.pd_read_sql_kwargs  )
        UpperCamelCase_											=							[sql_reader] if chunksize is None else sql_reader
        for chunk_idx, df in enumerate(__UpperCamelCase  ):
               UpperCamelCase_											=							pa.Table.from_pandas(__UpperCamelCase  )
               yield chunk_idx, self._cast_table(__UpperCamelCase  )





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_A							=       {'''configuration_dinat''': ['''DINAT_PRETRAINED_CONFIG_ARCHIVE_MAP''', '''DinatConfig''']}


try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      _A							=       [
          '''DINAT_PRETRAINED_MODEL_ARCHIVE_LIST''',
          '''DinatForImageClassification''',
          '''DinatModel''',
          '''DinatPreTrainedModel''',
          '''DinatBackbone''',
      ]

if TYPE_CHECKING:
      from .configuration_dinat import DINAT_PRETRAINED_CONFIG_ARCHIVE_MAP, DinatConfig

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_dinat import (
                DINAT_PRETRAINED_MODEL_ARCHIVE_LIST,
                DinatBackbone,
                DinatForImageClassification,
                DinatModel,
                DinatPreTrainedModel,
            )

else:
      import sys

      _A							=       _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)



