






'''simple docstring'''







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


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




@dataclass
class 					__UpperCAmelCase (		datasets.BuilderConfig	):

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    Any				       =					None
      SCREAMING_SNAKE_CASE				:    Any				       =					None
      SCREAMING_SNAKE_CASE				:    Any				       =					None
      SCREAMING_SNAKE_CASE				:    int				       =					True
      SCREAMING_SNAKE_CASE				:    str				       =					None
      SCREAMING_SNAKE_CASE				:    Any				       =					None
      SCREAMING_SNAKE_CASE				:    List[str]				       =					None
      SCREAMING_SNAKE_CASE				:    List[str]				       =					1_0_0_0_0
      SCREAMING_SNAKE_CASE				:    List[str]				       =					None




      def 							lowerCamelCase     (					self						):





             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  ,		):
             UpperCAmelCase__	:   List[str]												=	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__	:   Dict												=	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__	:   Any												=	sqlalchemy.create_engine(config_kwargs['''con'''].split('''://'''						)[0] + '''://'''						)
                                  UpperCAmelCase__	:   Union[str, Any]												=	str(sql.compile(dialect=engine.dialect						)						)
                                  UpperCAmelCase__	:   Union[str, Any]												=	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__	:   int												=	config_kwargs["""con"""]
             if not isinstance(_UpperCAmelCase  ,		_UpperCAmelCase						):
                    UpperCAmelCase__	:   Any												=	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						):
             UpperCAmelCase__	:   int												=	{
                 """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 					__UpperCAmelCase (		datasets.ArrowBasedBuilder	):

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    List[Any]				       =					SqlConfig




      def 							lowerCamelCase     (					self						):
             return datasets.DatasetInfo(features=self.config.features						)




      def 							lowerCamelCase     (					self  ,		_UpperCAmelCase						):
             return [datasets.SplitGenerator(name=datasets.Split.TRAIN  ,		gen_kwargs={}						)]




      def 							lowerCamelCase     (					self  ,		_UpperCAmelCase						):





             if self.config.features is not None:
                    UpperCAmelCase__	:   Optional[int]												=	self.config.features.arrow_schema
                    if all(not require_storage_cast(_UpperCAmelCase						) for feature in self.config.features.values()						):
                           # cheaper cast
                           UpperCAmelCase__	:   List[Any]												=	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__	:   Optional[int]												=	table_cast(_UpperCAmelCase  ,		_UpperCAmelCase						)
             return pa_table




      def 							lowerCamelCase     (					self						):
             UpperCAmelCase__	:   Tuple												=	self.config.chunksize
             UpperCAmelCase__	:   List[str]												=	pd.read_sql(
                 self.config.sql  ,		self.config.con  ,		chunksize=_UpperCAmelCase  ,		**self.config.pd_read_sql_kwargs						)
             UpperCAmelCase__	:   List[str]												=	[sql_reader] if chunksize is None else sql_reader
             for chunk_idx, df in enumerate(_UpperCAmelCase						):
                    UpperCAmelCase__	:   List[Any]												=	pa.Table.from_pandas(_UpperCAmelCase						)
                    yield chunk_idx, self._cast_table(_UpperCAmelCase						)

'''simple docstring'''
A__ :		dict[str, float]					={
    "kilogram": 1,
    "gram": pow(10, 3),
    "milligram": pow(10, 6),
    "metric-ton": pow(10, -3),
    "long-ton": 0.0_0_0_9_8_4_2_0_7_3,
    "short-ton": 0.0_0_1_1_0_2_3_1_2_2,
    "pound": 2.2_0_4_6_2_4_4_2_0_2,
    "stone": 0.1_5_7_4_7_3_1_7_2_8,
    "ounce": 3_5.2_7_3_9_9_0_7_2_3,
    "carrat": 5_000,
    "atomic-mass-unit": 6.0_2_2_1_3_6_6_5_2E2_6,
}

A__ :		dict[str, float]					={
    "kilogram": 1,
    "gram": pow(10, -3),
    "milligram": pow(10, -6),
    "metric-ton": pow(10, 3),
    "long-ton": 1_0_1_6.0_4_6_0_8,
    "short-ton": 9_0_7.1_8_4,
    "pound": 0.4_5_3_5_9_2,
    "stone": 6.3_5_0_2_9,
    "ounce": 0.0_2_8_3_4_9_5,
    "carrat": 0.0_0_0_2,
    "atomic-mass-unit": 1.6_6_0_5_4_0_1_9_9E-2_7,
}







def 							A_  (    __SCREAMING_SNAKE_CASE    :	str      ,  __SCREAMING_SNAKE_CASE    :	str      ,  __SCREAMING_SNAKE_CASE    :	float			)					->							float:





  """simple docstring"""

  if to_type not in KILOGRAM_CHART or from_type not in WEIGHT_TYPE_CHART:
    __A		:  Tuple       				=		(
        F"Invalid 'from_type' or 'to_type' value: {from_type!r}, {to_type!r}\n"
        F"Supported values are: {', '.join(__SCREAMING_SNAKE_CASE			)}"
    )
    raise ValueError(__SCREAMING_SNAKE_CASE			)
  return value * KILOGRAM_CHART[to_type] * WEIGHT_TYPE_CHART[from_type]


if __name__ == "__main__":
     import doctest

     doctest.testmod()



