






'''simple docstring'''
# 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 							_UpperCAmelCase    (		TensorFormatter[Mapping, 'torch.Tensor', Mapping]      ):




    def __init__(      self	:							Tuple	,  a	:							Dict=None	,  **a	:							str      ):






       '''simple docstring'''






       super().__init__(features=a      )
       lowercase_						:     Dict							     =  torch_tensor_kwargs
       import torch  # noqa import torch at initialization




    def 					lowerCAmelCase__					(      self	:							int	,  a	:							int      ):






       '''simple docstring'''






       import torch

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




    def 					lowerCAmelCase__					(      self	:							Any	,  a	:							Any      ):






       '''simple docstring'''






       import torch

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

       lowercase_						:     Any							     =  {}

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

          if isinstance(a	,  PIL.Image.Image      ):
             lowercase_						:     List[Any]							     =  np.asarray(a      )
       return torch.tensor(a	,  **{**default_dtype, **self.torch_tensor_kwargs}      )




    def 					lowerCAmelCase__					(      self	:							Union[str, Any]	,  a	:							List[Any]      ):






       '''simple docstring'''






       import torch

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




    def 					lowerCAmelCase__					(      self	:							List[str]	,  a	:							dict      ):






       '''simple docstring'''






       return map_nested(self._recursive_tensorize	,  a	,  map_list=a      )




    def 					lowerCAmelCase__					(      self	:							int	,  a	:							pa.Table      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  self.numpy_arrow_extractor().extract_row(a      )
       lowercase_						:     int							     =  self.python_features_decoder.decode_row(a      )
       return self.recursive_tensorize(a      )




    def 					lowerCAmelCase__					(      self	:							str	,  a	:							pa.Table      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.numpy_arrow_extractor().extract_column(a      )
       lowercase_						:     Tuple							     =  self.python_features_decoder.decode_column(a	,  pa_table.column_names[0]      )
       lowercase_						:     List[str]							     =  self.recursive_tensorize(a      )
       lowercase_						:     str							     =  self._consolidate(a      )
       return column





    def 					lowerCAmelCase__					(      self	:							Optional[int]	,  a	:							pa.Table      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  self.numpy_arrow_extractor().extract_batch(a      )
       lowercase_						:     List[Any]							     =  self.python_features_decoder.decode_batch(a      )
       lowercase_						:     Dict							     =  self.recursive_tensorize(a      )
       for column_name in batch:
          lowercase_						:     Optional[int]							     =  self._consolidate(batch[column_name]      )
       return batch












'''simple docstring'''
from __future__ import annotations




def 		__SCREAMING_SNAKE_CASE       (	_UpperCamelCase				):


   """simple docstring"""




   lowercase_						:     Optional[int]							     =  2
   lowercase_						:     Tuple							     =  []
   while i * i <= n:
      if n % i:
         i += 1
      else:
         n //= i
         factors.append(_UpperCamelCase				)
   if n > 1:
      factors.append(_UpperCamelCase				)
   return factors


if __name__ == "__main__":
 import doctest

 doctest.testmod()




