




import itertools
from dataclasses import dataclass
from typing import Optional

import pandas as pd
import pyarrow as pa

import datasets
from datasets.table import table_cast





@dataclass
class      snake_case	(						datasets.BuilderConfig				):


   '''simple docstring'''







   UpperCamelCase__						:							Optional[datasets.Features] 	=   None





class      snake_case	(						datasets.ArrowBasedBuilder				):


   '''simple docstring'''







   UpperCamelCase__						:							Dict 	=   PandasConfig




   def 						UpperCAmelCase							(       self  :						Optional[int]      )       ->Optional[int]:
     '''simple docstring'''







     return datasets.DatasetInfo(features=self.config.features      )




   def 						UpperCAmelCase							(       self  :						str    ,     lowerCamelCase_  :						str      )       ->Union[str, Any]:
     '''simple docstring'''







     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}'''      )
     UpperCAmelCase__         = dl_manager.download_and_extract(self.config.data_files      )
     if isinstance(lowerCamelCase_    ,     (str, list, tuple)      ):
       UpperCAmelCase__         = data_files
       if isinstance(lowerCamelCase_    ,     lowerCamelCase_      ):
         UpperCAmelCase__         = [files]
       # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
       UpperCAmelCase__         = [dl_manager.iter_files(lowerCamelCase_      ) for file in files]
       return [datasets.SplitGenerator(name=datasets.Split.TRAIN    ,     gen_kwargs={"""files""": files}      )]
     UpperCAmelCase__         = []
     for split_name, files in data_files.items():
       if isinstance(lowerCamelCase_    ,     lowerCamelCase_      ):
         UpperCAmelCase__         = [files]
       # Use `dl_manager.iter_files` to skip hidden files in an extracted archive
       UpperCAmelCase__         = [dl_manager.iter_files(lowerCamelCase_      ) for file in files]
       splits.append(datasets.SplitGenerator(name=lowerCamelCase_    ,     gen_kwargs={"""files""": files}      )      )
     return splits




   def 						UpperCAmelCase							(       self  :						str    ,     lowerCamelCase_  :						pa.Table      )       ->Optional[Any]:
     '''simple docstring'''







     if self.config.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
       UpperCAmelCase__         = table_cast(lowerCamelCase_    ,     self.config.features.arrow_schema      )
     return pa_table







   def 						UpperCAmelCase							(       self  :						Tuple    ,     lowerCamelCase_  :						Tuple      )       ->Tuple:
     '''simple docstring'''







     for i, file in enumerate(itertools.chain.from_iterable(lowerCamelCase_      )      ):
       with open(lowerCamelCase_    ,     """rb"""      ) as f:
         UpperCAmelCase__         = pa.Table.from_pandas(pd.read_pickle(lowerCamelCase_      )      )
         yield i, self._cast_table(lowerCamelCase_      )





'''simple docstring'''




import timeit

import numpy as np

import datasets
from datasets.arrow_writer import ArrowWriter
from datasets.features.features import _ArrayXD






def __lowerCAmelCase					(				UpperCamelCase__							)      ->       List[str]:
    def wrapper(*UpperCamelCase__							,    **UpperCamelCase__							):
        __lowerCamelCase            =    timeit.default_timer()
        __lowerCamelCase            =    func(*UpperCamelCase__							,    **UpperCamelCase__							)
        __lowerCamelCase            =    timeit.default_timer() - starttime
        return delta

    __lowerCamelCase            =    func.__name__

    return wrapper






def __lowerCAmelCase					(				UpperCamelCase__							,    UpperCamelCase__=1_00							,    UpperCamelCase__=None							)      ->       Dict:
    __lowerCamelCase            =    []
    __lowerCamelCase            =    seq_shapes or {}
    for i in range(UpperCamelCase__							):
        __lowerCamelCase            =    {}
        for col_id, (k, v) in enumerate(features.items()							):
            if isinstance(UpperCamelCase__							,    _ArrayXD							):
                __lowerCamelCase            =    np.random.rand(*v.shape							).astype(v.dtype							)
            elif isinstance(UpperCamelCase__							,    datasets.Value							):
                if v.dtype == "string":
                    __lowerCamelCase            =    '''The small grey turtle was surprisingly fast when challenged.'''
                else:
                    __lowerCamelCase            =    np.random.randint(10							,    size=1							).astype(v.dtype							).item()
            elif isinstance(UpperCamelCase__							,    datasets.Sequence							):
                while isinstance(UpperCamelCase__							,    datasets.Sequence							):
                    __lowerCamelCase            =    v.feature
                __lowerCamelCase            =    seq_shapes[k]
                __lowerCamelCase            =    np.random.rand(*UpperCamelCase__							).astype(v.dtype							)
            __lowerCamelCase            =    data

        dummy_data.append((i, example)							)

    return dummy_data






def __lowerCAmelCase					(				UpperCamelCase__							,    UpperCamelCase__							,    UpperCamelCase__=1_00							,    UpperCamelCase__=None							)      ->       Dict:
    __lowerCamelCase            =    generate_examples(UpperCamelCase__							,    num_examples=UpperCamelCase__							,    seq_shapes=UpperCamelCase__							)

    with ArrowWriter(features=UpperCamelCase__							,    path=UpperCamelCase__							) as writer:
        for key, record in dummy_data:
            __lowerCamelCase            =    features.encode_example(UpperCamelCase__							)
            writer.write(UpperCamelCase__							)

        __lowerCamelCase      ,    __lowerCamelCase            =    writer.finalize()

    if not num_final_examples == num_examples:
        raise ValueError(
            f"""Error writing the dataset, wrote {num_final_examples} examples but should have written {num_examples}."""							)

    __lowerCamelCase            =    datasets.Dataset.from_file(filename=UpperCamelCase__							,    info=datasets.DatasetInfo(features=UpperCamelCase__							)							)

    return dataset
