




import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


SCREAMING_SNAKE_CASE__ 						=False
try:
			SCREAMING_SNAKE_CASE__ 						=_is_package_available("""google.colab""")
except ModuleNotFoundError:
			pass






@input.register
class  __magic_name__						:
				'''simple docstring'''







				def __init__(							self:   List[Any]		,					_lowerCamelCase:   Any = None		,					_lowerCamelCase:   Dict = []		):
											SCREAMING_SNAKE_CASE_   	=    0
											SCREAMING_SNAKE_CASE_   	=    choices
											SCREAMING_SNAKE_CASE_   	=    prompt
											if sys.platform == "win32":
																		SCREAMING_SNAKE_CASE_   	=    "*"
											else:
																		SCREAMING_SNAKE_CASE_   	=    "➔ "


				def     _A							(							self:   Dict		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Optional[int] = ""		):
											if sys.platform != "win32":
																		writeColor(self.choices[index]		,					32		,					_lowerCamelCase		)
											else:
																		forceWrite(self.choices[index]		,					_lowerCamelCase		)


				def     _A							(							self:   Dict		,					_lowerCamelCase:   Tuple		):
											if index == self.position:
																		forceWrite(f" {self.arrow_char} "		)
																		self.write_choice(_lowerCamelCase		)
											else:
																		forceWrite(f"    {self.choices[index]}"		)
											reset_cursor()


				def     _A							(							self:   Tuple		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Union[str, Any] = 1		):
											SCREAMING_SNAKE_CASE_   	=    self.position
											if direction == Direction.DOWN:
																		if self.position + 1 >= len(self.choices		):
																									return
																		self.position += num_spaces
											else:
																		if self.position - 1 < 0:
																									return
																		self.position -= num_spaces
											clear_line()
											self.print_choice(_lowerCamelCase		)
											move_cursor(_lowerCamelCase		,					direction.name		)
											self.print_choice(self.position		)


				@input.mark(KEYMAP['''up''']		)
				def     _A							(							self:   Optional[int]		):
											self.move_direction(Direction.UP		)


				@input.mark(KEYMAP['''down''']		)
				def     _A							(							self:   Optional[int]		):
											self.move_direction(Direction.DOWN		)


				@input.mark(KEYMAP['''newline''']		)
				def     _A							(							self:   Optional[int]		):
											move_cursor(len(self.choices		) - self.position		,					'''DOWN'''		)
											return self.position


				@input.mark(KEYMAP['''interrupt''']		)
				def     _A							(							self:   List[Any]		):
											move_cursor(len(self.choices		) - self.position		,					'''DOWN'''		)
											raise KeyboardInterrupt


				@input.mark_multiple(*[KEYMAP[str(_lowerCamelCase		)] for number in range(10		)]		)
				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    int(chr(self.current_selection		)		)
											SCREAMING_SNAKE_CASE_   	=    index - self.position
											if index == self.position:
																		return
											if index < len(self.choices		):
																		if self.position > index:
																									self.move_direction(Direction.UP		,					-movement		)
																		elif self.position < index:
																									self.move_direction(Direction.DOWN		,					_lowerCamelCase		)
																		else:
																									return
											else:
																		return





				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   List[Any] = 0		):
											if self.prompt:
																		linebreak()
																		forceWrite(self.prompt		,					'''\n'''		)
																		if in_colab:
																									forceWrite('''Please input a choice index (starting from 0), and press enter'''		,					'''\n'''		)
																		else:
																									forceWrite('''Please select a choice using the arrow or number keys, and selecting with enter'''		,					'''\n'''		)
											SCREAMING_SNAKE_CASE_   	=    default_choice
											for i in range(len(self.choices		)		):
																		self.print_choice(_lowerCamelCase		)
																		forceWrite('''\n'''		)
											move_cursor(len(self.choices		) - self.position		,					'''UP'''		)
											with cursor.hide():
																		while True:
																									if in_colab:
																																try:
																																							SCREAMING_SNAKE_CASE_   	=    int(builtins.input()		)
																																except ValueError:
																																							SCREAMING_SNAKE_CASE_   	=    default_choice
																									else:
																																SCREAMING_SNAKE_CASE_   	=    self.handle_input()
																									if choice is not None:
																																reset_cursor()
																																for _ in range(len(self.choices		) + 1		):
																																							move_cursor(1		,					'''UP'''		)
																																							clear_line()
																																self.write_choice(_lowerCamelCase		,					'''\n'''		)
																																return choice








# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
			import torch






class  __magic_name__						(  TensorFormatter[Mapping, "torch.Tensor", Mapping]):
				'''simple docstring'''







				def __init__(							self:   int		,					_lowerCamelCase:   int=None		,					**_lowerCamelCase:   List[Any]		):
											super().__init__(features=_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    torch_tensor_kwargs
											import torch  # noqa import torch at initialization


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   Optional[Any]		):
											import torch

											if isinstance(_lowerCamelCase		,					_lowerCamelCase		) and column:
																		if all(
																		    isinstance(_lowerCamelCase		,					torch.Tensor		) and x.shape == column[0].shape and x.dtype == column[0].dtype
																		    for x in column		):
																									return torch.stack(_lowerCamelCase		)
											return column


				def     _A							(							self:   Tuple		,					_lowerCamelCase:   List[str]		):
											import torch

											if isinstance(_lowerCamelCase		,					(str, bytes, type(_lowerCamelCase		))		):
																		return value
											elif isinstance(_lowerCamelCase		,					(np.character, np.ndarray)		) and np.issubdtype(value.dtype		,					np.character		):
																		return value.tolist()

											SCREAMING_SNAKE_CASE_   	=    {}

											if isinstance(_lowerCamelCase		,					(np.number, np.ndarray)		) and np.issubdtype(value.dtype		,					np.integer		):
																		SCREAMING_SNAKE_CASE_   	=    {'''dtype''': torch.intaa}
											elif isinstance(_lowerCamelCase		,					(np.number, np.ndarray)		) and np.issubdtype(value.dtype		,					np.floating		):
																		SCREAMING_SNAKE_CASE_   	=    {'''dtype''': torch.floataa}
											elif config.PIL_AVAILABLE and "PIL" in sys.modules:
																		import PIL.Image

																		if isinstance(_lowerCamelCase		,					PIL.Image.Image		):
																									SCREAMING_SNAKE_CASE_   	=    np.asarray(_lowerCamelCase		)
											return torch.tensor(_lowerCamelCase		,					**{**default_dtype, **self.torch_tensor_kwargs}		)


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   Dict		):
											import torch

											# support for torch, tf, jax etc.
											if hasattr(_lowerCamelCase		,					'''__array__'''		) and not isinstance(_lowerCamelCase		,					torch.Tensor		):
																		SCREAMING_SNAKE_CASE_   	=    data_struct.__array__()
											# support for nested types like struct of list of struct
											if isinstance(_lowerCamelCase		,					np.ndarray		):
																		if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
																									return self._consolidate([self.recursive_tensorize(_lowerCamelCase		) for substruct in data_struct]		)
											elif isinstance(_lowerCamelCase		,					(list, tuple)		):
																		return self._consolidate([self.recursive_tensorize(_lowerCamelCase		) for substruct in data_struct]		)
											return self._tensorize(_lowerCamelCase		)


				def     _A							(							self:   str		,					_lowerCamelCase:   dict		):
											return map_nested(self._recursive_tensorize		,					_lowerCamelCase		,					map_list=_lowerCamelCase		)


				def     _A							(							self:   Optional[int]		,					_lowerCamelCase:   pa.Table		):
											SCREAMING_SNAKE_CASE_   	=    self.numpy_arrow_extractor().extract_row(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.python_features_decoder.decode_row(_lowerCamelCase		)
											return self.recursive_tensorize(_lowerCamelCase		)


				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   pa.Table		):
											SCREAMING_SNAKE_CASE_   	=    self.numpy_arrow_extractor().extract_column(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.python_features_decoder.decode_column(_lowerCamelCase		,					pa_table.column_names[0]		)
											SCREAMING_SNAKE_CASE_   	=    self.recursive_tensorize(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self._consolidate(_lowerCamelCase		)
											return column





				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   pa.Table		):
											SCREAMING_SNAKE_CASE_   	=    self.numpy_arrow_extractor().extract_batch(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.python_features_decoder.decode_batch(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    self.recursive_tensorize(_lowerCamelCase		)
											for column_name in batch:
																		SCREAMING_SNAKE_CASE_   	=    self._consolidate(batch[column_name]		)
											return batch


