


'''simple docstring'''

import sys
from collections.abc import Mapping

import numpy as np
import pyarrow as pa

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





class     __SCREAMING_SNAKE_CASE       (   TensorFormatter[Mapping, np.ndarray, Mapping]			):






     def __init__(      self  ,				__UpperCamelCase=None  ,				**__UpperCamelCase		)							->     Union[str, Any]:
          super().__init__(features=__lowerCamelCase		)
          _a            =		np_array_kwargs






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Optional[Any]:
          if isinstance(__lowerCamelCase  ,				__lowerCamelCase		):
               if column and all(
                   isinstance(__lowerCamelCase  ,				np.ndarray		) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column		):
                    return np.stack(__lowerCamelCase		)
               else:
                    # don't use np.array(column, dtype=object)
                    # since it fails in certain cases
                    # see https://stackoverflow.com/q/51005699
                    _a            =		np.empty(len(__lowerCamelCase		)  ,				dtype=__lowerCamelCase		)
                    _a            =		column
                    return out
          return column






     def 			a_       (      self  ,				__UpperCamelCase		)							->     int:
          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
          elif isinstance(__lowerCamelCase  ,				np.number		):
               return value

          _a            =		{}

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

               if isinstance(__lowerCamelCase  ,				PIL.Image.Image		):
                    return np.asarray(__lowerCamelCase  ,				**self.np_array_kwargs		)

          return np.asarray(__lowerCamelCase  ,				**{**default_dtype, **self.np_array_kwargs}		)






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Optional[int]:
          # support for torch, tf, jax etc.
          if config.TORCH_AVAILABLE and "torch" in sys.modules:
               import torch

               if isinstance(__lowerCamelCase  ,				torch.Tensor		):
                    return self._tensorize(data_struct.detach().cpu().numpy()[()]		)
          if hasattr(__lowerCamelCase  ,				"__array__"		) and not isinstance(__lowerCamelCase  ,				(np.ndarray, np.character, np.number)		):
               _a            =		data_struct.__array__()
          # support for nested types like struct of list of struct
          if isinstance(__lowerCamelCase  ,				np.ndarray		):
               if data_struct.dtype == object:
                    return self._consolidate([self.recursive_tensorize(__lowerCamelCase		) for substruct in data_struct]		)
          if isinstance(__lowerCamelCase  ,				(list, tuple)		):
               return self._consolidate([self.recursive_tensorize(__lowerCamelCase		) for substruct in data_struct]		)
          return self._tensorize(__lowerCamelCase		)






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Dict:
          return map_nested(self._recursive_tensorize  ,				__lowerCamelCase  ,				map_list=__lowerCamelCase		)






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Mapping:
          _a            =		self.numpy_arrow_extractor().extract_row(__lowerCamelCase		)
          _a            =		self.python_features_decoder.decode_row(__lowerCamelCase		)
          return self.recursive_tensorize(__lowerCamelCase		)






     def 			a_       (      self  ,				__UpperCamelCase		)							->     np.ndarray:
          _a            =		self.numpy_arrow_extractor().extract_column(__lowerCamelCase		)
          _a            =		self.python_features_decoder.decode_column(__lowerCamelCase  ,				pa_table.column_names[0]		)
          _a            =		self.recursive_tensorize(__lowerCamelCase		)
          _a            =		self._consolidate(__lowerCamelCase		)
          return column






     def 			a_       (      self  ,				__UpperCamelCase		)							->     Mapping:
          _a            =		self.numpy_arrow_extractor().extract_batch(__lowerCamelCase		)
          _a            =		self.python_features_decoder.decode_batch(__lowerCamelCase		)
          _a            =		self.recursive_tensorize(__lowerCamelCase		)
          for column_name in batch:
               _a            =		self._consolidate(batch[column_name]		)
          return batch






def 						UpperCAmelCase (    _lowerCamelCase     ):
			return "".join(chr(ord(_lowerCamelCase     ) + 32     ) if "A" <= char <= "Z" else char for char in word     )


if __name__ == "__main__":
							from doctest import testmod

							testmod()