

import itertools
from dataclasses import dataclass
from typing import Optional

import pyarrow as pa

import datasets
from datasets.table import table_cast


__a						:Optional[int]            = datasets.utils.logging.get_logger(__name__)

@dataclass
class      _a				( datasets.BuilderConfig							):




      """simple docstring"""

      _lowerCamelCase					: str					=					None




class      _a				( datasets.ArrowBasedBuilder							):




      """simple docstring"""

      _lowerCamelCase					: Union[str, Any]					=					ArrowConfig





      def 		__A  (      self       :							str  ):
            return datasets.DatasetInfo(features=self.config.features  )





      def 		__A  (      self       :							Union[str, Any]	, UpperCAmelCase       :							Union[str, Any]  ):



            if not self.config.data_files:
                  raise ValueError(f'''At least one data file must be specified, but got data_files={self.config.data_files}'''  )
            A_	     =							dl_manager.download_and_extract(self.config.data_files  )
            if isinstance(UpperCAmelCase	, (str, list, tuple)  ):
                  A_	     =							data_files
                  if isinstance(UpperCAmelCase	, UpperCAmelCase  ):
                        A_	     =							[files]
                  # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
                  A_	     =							[dl_manager.iter_files(UpperCAmelCase  ) for file in files]
                  return [datasets.SplitGenerator(name=datasets.Split.TRAIN	, gen_kwargs={"files": files}  )]
            A_	     =							[]
            for split_name, files in data_files.items():
                  if isinstance(UpperCAmelCase	, UpperCAmelCase  ):
                        A_	     =							[files]
                  # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
                  A_	     =							[dl_manager.iter_files(UpperCAmelCase  ) for file in files]
                  # Infer features is they are stoed in the arrow schema
                  if self.info.features is None:
                        for file in itertools.chain.from_iterable(UpperCAmelCase  ):
                              with open(UpperCAmelCase	, "rb"  ) as f:
                                    A_	     =							datasets.Features.from_arrow_schema(pa.ipc.open_stream(UpperCAmelCase  ).schema  )
                              break
                  splits.append(datasets.SplitGenerator(name=UpperCAmelCase	, gen_kwargs={"files": files}  )  )
            return splits





      def 		__A  (      self       :							Optional[Any]	, UpperCAmelCase       :							pa.Table  ):



            if self.info.features is not None:
                  # more expensive cast to support nested features with keys in a different order
                  # allows str <-> int/float or str to Audio for example
                  A_	     =							table_cast(UpperCAmelCase	, self.info.features.arrow_schema  )
            return pa_table







      def 		__A  (      self       :							str	, UpperCAmelCase       :							Any  ):



            for file_idx, file in enumerate(itertools.chain.from_iterable(UpperCAmelCase  )  ):
                  with open(UpperCAmelCase	, "rb"  ) as f:
                        try:
                              for batch_idx, record_batch in enumerate(pa.ipc.open_stream(UpperCAmelCase  )  ):
                                    A_	     =							pa.Table.from_batches([record_batch]  )
                                    # Uncomment for debugging (will print the Arrow table size and elements)
                                    # logger.warning(f"pa_table: {pa_table} num rows: {pa_table.num_rows}")
                                    # logger.warning('\n'.join(str(pa_table.slice(i, 1).to_pydict()) for i in range(pa_table.num_rows)))
                                    yield f'''{file_idx}_{batch_idx}''', self._cast_table(UpperCAmelCase  )
                        except ValueError as e:
                              logger.error(f'''Failed to read file \'{file}\' with error {type(UpperCAmelCase  )}: {e}'''  )
                              raise









__a						:Dict            = {hex(i)[2:]: i for i in range(16)}  # Use [:2] to strip off the leading '0x'

def 			__snake_case	(   __UpperCamelCase  :   str						):



      """simple docstring"""

      A_	     =							hex_string.strip().lower()
      if not hex_string:
            raise ValueError("Empty string was passed to the function"						)
      A_	     =							hex_string[0] == "-"
      if is_negative:
            A_	     =							hex_string[1:]
      if not all(char in hex_table for char in hex_string						):
            raise ValueError("Non-hexadecimal value was passed to the function"						)
      A_	     =							0
      for char in hex_string:
            A_	     =							16 * decimal_number + hex_table[char]
      return -decimal_number if is_negative else decimal_number


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

      testmod()