



'''simple docstring'''






import multiprocessing
from typing import TYPE_CHECKING, Optional, Union

from .. import Dataset, Features, config
from ..formatting import query_table
from ..packaged_modules.sql.sql import Sql
from ..utils import logging
from .abc import AbstractDatasetInputStream


if TYPE_CHECKING:
						import sqlitea

						import sqlalchemy

class        SCREAMING_SNAKE_CASE(							lowerCAmelCase__    ):







	"""simple docstring"""






	def __init__(		self    :  str	,    __snake_case    :  Optional[int]	,    __snake_case    :  Dict	,    __snake_case    :  str = None	,    __snake_case    :  str = None	,    __snake_case    :  str = False	,    **__snake_case    :  str	,    )    ->	List[Any]:
								super().__init__(features=_SCREAMING_SNAKE_CASE	,    cache_dir=_SCREAMING_SNAKE_CASE	,    keep_in_memory=_SCREAMING_SNAKE_CASE	,    **_SCREAMING_SNAKE_CASE	)
								UpperCAmelCase				: str											=	Sql(
								    cache_dir=_SCREAMING_SNAKE_CASE	,    features=_SCREAMING_SNAKE_CASE	,    sql=_SCREAMING_SNAKE_CASE	,    con=_SCREAMING_SNAKE_CASE	,    **_SCREAMING_SNAKE_CASE	,    )







	def   A			(		self    :  Dict	)    ->	Optional[Any]:
								UpperCAmelCase				: List[str]											=	None
								UpperCAmelCase				: Optional[int]											=	None
								UpperCAmelCase				: str											=	None
								UpperCAmelCase				: Optional[Any]											=	None

								self.builder.download_and_prepare(
								    download_config=_SCREAMING_SNAKE_CASE	,    download_mode=_SCREAMING_SNAKE_CASE	,    verification_mode=_SCREAMING_SNAKE_CASE	,    base_path=_SCREAMING_SNAKE_CASE	,    )

								# Build dataset for splits
								UpperCAmelCase				: Optional[int]											=	self.builder.as_dataset(
								    split='''train'''	,    verification_mode=_SCREAMING_SNAKE_CASE	,    in_memory=self.keep_in_memory	)
								return dataset

class        SCREAMING_SNAKE_CASE:







	"""simple docstring"""






	def __init__(		self    :  List[str]	,    __snake_case    :  str	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Any	,    __snake_case    :  int = None	,    __snake_case    :  List[Any] = None	,    **__snake_case    :  Union[str, Any]	,    )    ->	List[Any]:
								if num_proc is not None and num_proc <= 0:
															raise ValueError(F"""num_proc {num_proc} must be an integer > 0."""	)

								UpperCAmelCase				: List[Any]											=	dataset
								UpperCAmelCase				: Union[str, Any]											=	name
								UpperCAmelCase				: Optional[int]											=	con
								UpperCAmelCase				: List[str]											=	batch_size if batch_size else config.DEFAULT_MAX_BATCH_SIZE
								UpperCAmelCase				: Any											=	num_proc
								UpperCAmelCase				: Optional[int]											=	to_sql_kwargs







	def   A			(		self    :  Union[str, Any]	)    ->	int:
								UpperCAmelCase				: Tuple											=	self.to_sql_kwargs.pop('''sql'''	,    _SCREAMING_SNAKE_CASE	)
								UpperCAmelCase				: Optional[Any]											=	self.to_sql_kwargs.pop('''con'''	,    _SCREAMING_SNAKE_CASE	)
								UpperCAmelCase				: str											=	self.to_sql_kwargs.pop('''index'''	,    _SCREAMING_SNAKE_CASE	)

								UpperCAmelCase				: List[str]											=	self._write(index=_SCREAMING_SNAKE_CASE	,    **self.to_sql_kwargs	)
								return written







	def   A			(		self    :  str	,    __snake_case    :  Any	)    ->	Optional[int]:
								UpperCAmelCase							,	UpperCAmelCase							,	UpperCAmelCase				: Optional[int]											=	args
								UpperCAmelCase				: Any											=	{**to_sql_kwargs, '''if_exists''': '''append'''} if offset > 0 else to_sql_kwargs
								UpperCAmelCase				: Any											=	query_table(
								    table=self.dataset.data	,    key=slice(_SCREAMING_SNAKE_CASE	,    offset + self.batch_size	)	,    indices=self.dataset._indices	,    )
								UpperCAmelCase				: Tuple											=	batch.to_pandas()
								UpperCAmelCase				: List[Any]											=	df.to_sql(self.name	,    self.con	,    index=_SCREAMING_SNAKE_CASE	,    **_SCREAMING_SNAKE_CASE	)
								return num_rows or len(_SCREAMING_SNAKE_CASE	)







	def   A			(		self    :  Any	,    __snake_case    :  List[Any]	,    **__snake_case    :  List[str]	)    ->	int:
								UpperCAmelCase				: str											=	0

								if self.num_proc is None or self.num_proc == 1:
															for offset in logging.tqdm(
															    range(0	,    len(self.dataset	)	,    self.batch_size	)	,    unit='''ba'''	,    disable=not logging.is_progress_bar_enabled()	,    desc='''Creating SQL from Arrow format'''	,    ):
																						written += self._batch_sql((offset, index, to_sql_kwargs)	)
								else:
															UpperCAmelCase							,	UpperCAmelCase				: List[str]											=	len(self.dataset	), self.batch_size
															with multiprocessing.Pool(self.num_proc	) as pool:
																						for num_rows in logging.tqdm(
																						    pool.imap(
																						        self._batch_sql	,    [(offset, index, to_sql_kwargs) for offset in range(0	,    _SCREAMING_SNAKE_CASE	,    _SCREAMING_SNAKE_CASE	)]	,    )	,    total=(num_rows // batch_size) + 1 if num_rows % batch_size else num_rows // batch_size	,    unit='''ba'''	,    disable=not logging.is_progress_bar_enabled()	,    desc='''Creating SQL from Arrow format'''	,    ):
																													written += num_rows

								return written










'''simple docstring'''






import numpy as np

UpperCamelCase__:     Any										=    [
    ["a", "b", "c", "d", "e"],
    ["f", "g", "h", "i", "k"],
    ["l", "m", "n", "o", "p"],
    ["q", "r", "s", "t", "u"],
    ["v", "w", "x", "y", "z"],
]

class        SCREAMING_SNAKE_CASE:







	"""simple docstring"""






	def __init__(		self    :  Any	)    ->	None:
								UpperCAmelCase				: Optional[int]											=	np.array(__snake_case	)







	def   A			(		self    :  str	,    __snake_case    :  str	)    ->	np.ndarray:
								UpperCAmelCase							,	UpperCAmelCase				: Dict											=	np.where(letter == self.SQUARE	)
								UpperCAmelCase				: Dict											=	np.concatenate([indexa + 1, indexa + 1]	)
								return indexes







	def   A			(		self    :  Union[str, Any]	,    __snake_case    :  int	,    __snake_case    :  int	)    ->	str:
								UpperCAmelCase				: int											=	self.SQUARE[indexa - 1, indexa - 1]
								return letter







	def   A			(		self    :  List[Any]	,    __snake_case    :  str	)    ->	str:
								UpperCAmelCase				: Any											=	message.lower()
								UpperCAmelCase				: List[Any]											=	message.replace(''' '''	,    ''''''	)
								UpperCAmelCase				: List[Any]											=	message.replace('''j'''	,    '''i'''	)

								UpperCAmelCase				: Tuple											=	np.empty((2, len(__snake_case	))	)
								for letter_index in range(len(__snake_case	)	):
															UpperCAmelCase				: str											=	self.letter_to_numbers(message[letter_index]	)

															UpperCAmelCase				: Any											=	numbers[0]
															UpperCAmelCase				: Optional[Any]											=	numbers[1]

								UpperCAmelCase				: List[str]											=	first_step.reshape(2 * len(__snake_case	)	)
								UpperCAmelCase				: Union[str, Any]											=	''''''
								for numbers_index in range(len(__snake_case	)	):
															UpperCAmelCase				: Dict											=	int(second_step[numbers_index * 2]	)
															UpperCAmelCase				: Union[str, Any]											=	int(second_step[(numbers_index * 2) + 1]	)
															UpperCAmelCase				: Dict											=	self.numbers_to_letter(__snake_case	,    __snake_case	)
															UpperCAmelCase				: int											=	encoded_message + letter

								return encoded_message







	def   A			(		self    :  Optional[Any]	,    __snake_case    :  str	)    ->	str:
								UpperCAmelCase				: str											=	message.lower()
								message.replace(''' '''	,    ''''''	)
								UpperCAmelCase				: Optional[int]											=	np.empty(2 * len(__snake_case	)	)
								for letter_index in range(len(__snake_case	)	):
															UpperCAmelCase				: int											=	self.letter_to_numbers(message[letter_index]	)
															UpperCAmelCase				: Optional[Any]											=	numbers[0]
															UpperCAmelCase				: List[Any]											=	numbers[1]

								UpperCAmelCase				: Tuple											=	first_step.reshape((2, len(__snake_case	))	)
								UpperCAmelCase				: str											=	''''''
								for numbers_index in range(len(__snake_case	)	):
															UpperCAmelCase				: Tuple											=	int(second_step[0, numbers_index]	)
															UpperCAmelCase				: List[str]											=	int(second_step[1, numbers_index]	)
															UpperCAmelCase				: Union[str, Any]											=	self.numbers_to_letter(__snake_case	,    __snake_case	)
															UpperCAmelCase				: Union[str, Any]											=	decoded_message + letter

								return decoded_message





