




# 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       _A				(		TensorFormatter[Mapping, """torch.Tensor""", Mapping]     ):






		def __init__(    self						,		_SCREAMING_SNAKE_CASE=None						,		**_SCREAMING_SNAKE_CASE			):
								super().__init__(features=_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			torch_tensor_kwargs
								import torch  # noqa import torch at initialization






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								import torch

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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								import torch

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

								_UpperCAmelCase      =			{}

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

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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								import torch

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






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								return map_nested(self._recursive_tensorize						,		_SCREAMING_SNAKE_CASE						,		map_list=_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.numpy_arrow_extractor().extract_row(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self.python_features_decoder.decode_row(_SCREAMING_SNAKE_CASE			)
								return self.recursive_tensorize(_SCREAMING_SNAKE_CASE			)






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.numpy_arrow_extractor().extract_column(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self.python_features_decoder.decode_column(_SCREAMING_SNAKE_CASE						,		pa_table.column_names[0]			)
								_UpperCAmelCase      =			self.recursive_tensorize(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self._consolidate(_SCREAMING_SNAKE_CASE			)
								return column






		def 				UpperCAmelCase (    self						,		_SCREAMING_SNAKE_CASE			):
								_UpperCAmelCase      =			self.numpy_arrow_extractor().extract_batch(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self.python_features_decoder.decode_batch(_SCREAMING_SNAKE_CASE			)
								_UpperCAmelCase      =			self.recursive_tensorize(_SCREAMING_SNAKE_CASE			)
								for column_name in batch:
														_UpperCAmelCase      =			self._consolidate(batch[column_name]			)
								return batch





import os
import string
import sys


a													=			1 << 8

a													=			{
    "tab": ord("\t"),
    "newline": ord("\r"),
    "esc": 27,
    "up": 65 + ARROW_KEY_FLAG,
    "down": 66 + ARROW_KEY_FLAG,
    "right": 67 + ARROW_KEY_FLAG,
    "left": 68 + ARROW_KEY_FLAG,
    "mod_int": 91,
    "undefined": sys.maxsize,
    "interrupt": 3,
    "insert": 50,
    "delete": 51,
    "pg_up": 53,
    "pg_down": 54,
}

a													=			KEYMAP["up"]
a													=			KEYMAP["left"]

if sys.platform == "win32":
				a													=			[]
				a													=			{
				    b"\xe0H": KEYMAP["up"] - ARROW_KEY_FLAG,
				    b"\x00H": KEYMAP["up"] - ARROW_KEY_FLAG,
				    b"\xe0P": KEYMAP["down"] - ARROW_KEY_FLAG,
				    b"\x00P": KEYMAP["down"] - ARROW_KEY_FLAG,
				    b"\xe0M": KEYMAP["right"] - ARROW_KEY_FLAG,
				    b"\x00M": KEYMAP["right"] - ARROW_KEY_FLAG,
				    b"\xe0K": KEYMAP["left"] - ARROW_KEY_FLAG,
				    b"\x00K": KEYMAP["left"] - ARROW_KEY_FLAG,
				}

for i in range(10):
				a													=			ord(str(i))




def        _SCREAMING_SNAKE_CASE      (		)						->				Optional[int]:
						if os.name == "nt":
												import msvcrt

												_UpperCAmelCase      =			"""mbcs"""
												# Flush the keyboard buffer
												while msvcrt.kbhit():
																		msvcrt.getch()
												if len(snake_case       ) == 0:
																		# Read the keystroke
																		_UpperCAmelCase      =			msvcrt.getch()

																		# If it is a prefix char, get second part
																		if ch in (b"\x00", b"\xe0"):
																								_UpperCAmelCase      =			ch + msvcrt.getch()
																								# Translate actual Win chars to bullet char types
																								try:
																														_UpperCAmelCase      =			chr(WIN_KEYMAP[cha]       )
																														WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""]       )       )
																														WIN_CH_BUFFER.append(snake_case       )
																														if ord(snake_case       ) in (
																														    KEYMAP["insert"] - 1 << 9,
																														    KEYMAP["delete"] - 1 << 9,
																														    KEYMAP["pg_up"] - 1 << 9,
																														    KEYMAP["pg_down"] - 1 << 9,
																														):
																																				WIN_CH_BUFFER.append(chr(1_2_6       )       )
																														_UpperCAmelCase      =			chr(KEYMAP["""esc"""]       )
																								except KeyError:
																														_UpperCAmelCase      =			cha[1]
																		else:
																								_UpperCAmelCase      =			ch.decode(snake_case       )
												else:
																		_UpperCAmelCase      =			WIN_CH_BUFFER.pop(0       )
						elif os.name == "posix":
												import termios
												import tty

												_UpperCAmelCase      =			sys.stdin.fileno()
												_UpperCAmelCase      =			termios.tcgetattr(snake_case       )
												try:
																		tty.setraw(snake_case       )
																		_UpperCAmelCase      =			sys.stdin.read(1       )
												finally:
																		termios.tcsetattr(snake_case			,     termios.TCSADRAIN			,     snake_case       )
						return ch




def        _SCREAMING_SNAKE_CASE      (		)						->				Dict:
						_UpperCAmelCase      =			get_raw_chars()
						if ord(snake_case       ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
												return char

						elif ord(snake_case       ) == KEYMAP["esc"]:
												_UpperCAmelCase      =			get_raw_chars()
												if ord(snake_case       ) == KEYMAP["mod_int"]:
																		_UpperCAmelCase      =			get_raw_chars()
																		if ord(snake_case       ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(snake_case       ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
																								return chr(ord(snake_case       ) + ARROW_KEY_FLAG       )
																		else:
																								return KEYMAP["undefined"]
												else:
																		return get_raw_chars()

						else:
												if char in string.printable:
																		return char
												else:
																		return KEYMAP["undefined"]