






'''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 tensorflow as tf

class      _UpperCAmelCase    (  TensorFormatter[Mapping, '''tf.Tensor''', Mapping]		):




			"""simple docstring"""

			def __init__(	self				:			Dict   ,   __UpperCAmelCase				:			Tuple=None   ,   **__UpperCAmelCase				:			List[str]	):





									'''simple docstring'''





									super().__init__(features=lowerCamelCase__	)
									_A      =			tf_tensor_kwargs
									import tensorflow as tf  # noqa: import tf at initialization

			def 		lowerCAmelCase				(	self				:			Optional[int]   ,   __UpperCAmelCase				:			List[str]	):





									'''simple docstring'''





									import tensorflow as tf

									if isinstance(lowerCamelCase__   ,   lowerCamelCase__	) and column:
															if all(
															    isinstance(lowerCamelCase__   ,   tf.Tensor	) and x.shape == column[0].shape and x.dtype == column[0].dtype for x in column	):
																					return tf.stack(lowerCamelCase__	)
															elif all(
															    isinstance(lowerCamelCase__   ,   (tf.Tensor, tf.RaggedTensor)	) and x.ndim == 1 and x.dtype == column[0].dtype
															    for x in column	):
																					# only rag 1-D tensors, otherwise some dimensions become ragged even though they were consolidated
																					return tf.ragged.stack(lowerCamelCase__	)

									return column

			def 		lowerCAmelCase				(	self				:			int   ,   __UpperCAmelCase				:			str	):





									'''simple docstring'''





									import tensorflow as tf

									if value is None:
															return value

									_A      =			{}

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

															if isinstance(lowerCamelCase__   ,   PIL.Image.Image	):
																					_A      =			np.asarray(lowerCamelCase__	)

									return tf.convert_to_tensor(lowerCamelCase__   ,   **{**default_dtype, **self.tf_tensor_kwargs}	)

			def 		lowerCAmelCase				(	self				:			str   ,   __UpperCAmelCase				:			Optional[Any]	):





									'''simple docstring'''





									import tensorflow as tf

									# 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__   ,   tf.Tensor	):
															_A      =			data_struct.__array__()
									# support for nested types like struct of list of struct
									if isinstance(lowerCamelCase__   ,   np.ndarray	):
															if data_struct.dtype == object:  # tf 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 		lowerCAmelCase				(	self				:			List[str]   ,   __UpperCAmelCase				:			Optional[int]	):





									'''simple docstring'''





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

			def 		lowerCAmelCase				(	self				:			Optional[Any]   ,   __UpperCAmelCase				:			Union[str, Any]	):





									'''simple docstring'''





									_A      =			self.numpy_arrow_extractor().extract_row(lowerCamelCase__	)
									_A      =			self.python_features_decoder.decode_row(lowerCamelCase__	)
									return self.recursive_tensorize(lowerCamelCase__	)

			def 		lowerCAmelCase				(	self				:			List[str]   ,   __UpperCAmelCase				:			Tuple	):





									'''simple docstring'''





									_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 		lowerCAmelCase				(	self				:			Tuple   ,   __UpperCAmelCase				:			Optional[int]	):





									'''simple docstring'''





									_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













'''simple docstring'''




def       __lowercase		(    __lowercase ) ->		int:

      '''simple docstring'''


      _A      =			str(__lowercase ).strip()
      if not bin_string:
            raise ValueError("Empty string was passed to the function" )
      _A      =			bin_string[0] == "-"
      if is_negative:
            _A      =			bin_string[1:]
      if not all(char in "01" for char in bin_string ):
            raise ValueError("Non-binary value was passed to the function" )
      _A      =			0
      for char in bin_string:
            _A      =			2 * decimal_number + int(__lowercase )
      return -decimal_number if is_negative else decimal_number


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

    testmod()





