






import os
import tempfile
from pathlib import Path
from unittest import TestCase

import pyarrow as pa
import pytest

from datasets.arrow_dataset import Dataset
from datasets.arrow_reader import ArrowReader, BaseReader, FileInstructions, ReadInstruction, make_file_instructions
from datasets.info import DatasetInfo
from datasets.splits import NamedSplit, Split, SplitDict, SplitInfo

from .utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases


class     _UpperCAmelCase  (						snake_case     ):




    def 			lowerCAmelCase__			(     self					:    Dict					,       a					:    int					,       a					:    Optional[Any]=False     ):






       '''simple docstring'''






       lowercase_		:      Tuple          =       (
           filename_skip_take["filename"],
           filename_skip_take["skip"] if "skip" in filename_skip_take else None,
           filename_skip_take["take"] if "take" in filename_skip_take else None,
       )
       open(os.path.join(a     )					,       "wb"     ).close()
       lowercase_		:      Optional[Any]          =       pa.Table.from_pydict({"filename": [Path(a     ).name] * 1_0_0}     )
       if take == -1:
          lowercase_		:      List[str]          =       len(a     ) - skip
       if skip is not None and take is not None:
          lowercase_		:      Any          =       pa_table.slice(a					,       a     )
       return pa_table


class     _UpperCAmelCase  (						snake_case     ):




    def 			lowerCAmelCase__			(     self					:    Tuple     ):






       '''simple docstring'''






       lowercase_		:      str          =       "my_name"
       lowercase_		:      Optional[int]          =       SplitInfo(name="train"					,       num_examples=1_0_0     )
       lowercase_		:      Any          =       SplitInfo(name="test"					,       num_examples=1_0_0     )
       lowercase_		:      Optional[int]          =       [train_info, test_info]
       lowercase_		:      Optional[int]          =       SplitDict()
       split_dict.add(a     )
       split_dict.add(a     )
       lowercase_		:      str          =       DatasetInfo(splits=a     )

       with tempfile.TemporaryDirectory() as tmp_dir:
          lowercase_		:      str          =       ReaderTest(a					,       a     )

          lowercase_		:      str          =       "test[:33%]"
          lowercase_		:      Any          =       Dataset(**reader.read(a					,       a					,       a     )     )
          self.assertEqual(dset["filename"][0]					,       f"""{name}-test"""     )
          self.assertEqual(dset.num_rows					,       3_3     )
          self.assertEqual(dset.num_columns					,       1     )

          lowercase_		:      Any          =       ["train", "test[:33%]"]
          lowercase_		:      List[Any]          =       [Split.TRAIN, ReadInstruction.from_spec("test[:33%]"     )]
          for instructions in [instructionsa, instructionsa]:
             lowercase_		:      str          =       [reader.read(a					,       a					,       a     ) for instr in instructions]
             lowercase_		:      Optional[Any]          =       (Dataset(**a     ) for dataset_kwargs in datasets_kwargs)
             self.assertEqual(train_dset["filename"][0]					,       f"""{name}-train"""     )
             self.assertEqual(train_dset.num_rows					,       1_0_0     )
             self.assertEqual(train_dset.num_columns					,       1     )
             self.assertIsInstance(train_dset.split					,       a     )
             self.assertEqual(str(train_dset.split     )					,       "train"     )
             self.assertEqual(test_dset["filename"][0]					,       f"""{name}-test"""     )
             self.assertEqual(test_dset.num_rows					,       3_3     )
             self.assertEqual(test_dset.num_columns					,       1     )
             self.assertIsInstance(test_dset.split					,       a     )
             self.assertEqual(str(test_dset.split     )					,       "test[:33%]"     )
             del train_dset, test_dset




    def 			lowerCAmelCase__			(     self					:    str     ):






       '''simple docstring'''






       lowercase_		:      Union[str, Any]          =       "my_name"
       lowercase_		:      List[Any]          =       SplitInfo(name="train"					,       num_examples=1_0_0_0					,       shard_lengths=[1_0_0] * 1_0     )
       lowercase_		:      Optional[int]          =       [train_info]
       lowercase_		:      Union[str, Any]          =       SplitDict()
       split_dict.add(a     )
       lowercase_		:      Dict          =       DatasetInfo(splits=a     )

       with tempfile.TemporaryDirectory() as tmp_dir:
          lowercase_		:      str          =       ReaderTest(a					,       a     )

          lowercase_		:      List[Any]          =       "train[:33%]"
          lowercase_		:      List[Any]          =       Dataset(**reader.read(a					,       a					,       a     )     )
          self.assertEqual(dset["filename"][0]					,       f"""{name}-train-00000-of-00010"""     )
          self.assertEqual(dset["filename"][-1]					,       f"""{name}-train-00003-of-00010"""     )
          self.assertEqual(dset.num_rows					,       3_3_0     )
          self.assertEqual(dset.num_columns					,       1     )





    def 			lowerCAmelCase__			(     self					:    List[str]     ):






       '''simple docstring'''






       lowercase_		:      int          =       SplitInfo(name="train"					,       num_examples=1_0_0     )
       lowercase_		:      Union[str, Any]          =       SplitInfo(name="test"					,       num_examples=1_0_0     )
       lowercase_		:      Optional[int]          =       SplitDict()
       split_dict.add(a     )
       split_dict.add(a     )
       lowercase_		:      List[Any]          =       DatasetInfo(splits=a     )

       with tempfile.TemporaryDirectory() as tmp_dir:
          lowercase_		:      Optional[int]          =       ReaderTest(a					,       a     )

          lowercase_		:      int          =       [
              {"filename": os.path.join(a					,       "train"     )},
              {"filename": os.path.join(a					,       "test"     ), "skip": 1_0, "take": 1_0},
          ]
          lowercase_		:      Dict          =       Dataset(**reader.read_files(a					,       original_instructions="train+test[10:20]"     )     )
          self.assertEqual(dset.num_rows					,       1_1_0     )
          self.assertEqual(dset.num_columns					,       1     )
          del dset




@pytest.mark.parametrize("in_memory"  ,						[False, True]							)
def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase							):


   """simple docstring"""




   lowercase_		:      str          =       arrow_file
   with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
      lowercase_		:      str          =       ArrowReader.read_table(_UpperCamelCase  ,						in_memory=_UpperCamelCase							)
   assert table.shape == dataset.data.shape
   assert set(table.column_names							) == set(dataset.data.column_names							)
   assert dict(table.to_pydict()							) == dict(dataset.data.to_pydict()							)  # to_pydict returns OrderedDict




@pytest.mark.parametrize("in_memory"  ,						[False, True]							)
def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase							):


   """simple docstring"""




   lowercase_		:      Optional[int]          =       arrow_file
   lowercase_		:      Optional[int]          =       ArrowReader(""  ,						_UpperCamelCase							)
   with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
      lowercase_		:      Optional[Any]          =       reader.read_files([{"filename": filename}]  ,						in_memory=_UpperCamelCase							)
   assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
   lowercase_		:      Tuple          =       dataset_kwargs["arrow_table"]
   assert table.shape == dataset.data.shape
   assert set(table.column_names							) == set(dataset.data.column_names							)
   assert dict(table.to_pydict()							) == dict(dataset.data.to_pydict()							)  # to_pydict returns OrderedDict




def 						__SCREAMING_SNAKE_CASE   (					):


   """simple docstring"""




   assert ReadInstruction("train"  ,						to=10  ,						unit="abs"							).to_spec() == "train[:10]"
   assert ReadInstruction("train"  ,						from_=-80  ,						to=10  ,						unit="%"							).to_spec() == "train[-80%:10%]"

   lowercase_		:      Tuple          =       "train+test"
   assert ReadInstruction.from_spec(_UpperCamelCase							).to_spec() == spec_train_test

   lowercase_		:      List[str]          =       "train[2:10]"
   assert ReadInstruction.from_spec(_UpperCamelCase							).to_spec() == spec_train_abs

   lowercase_		:      int          =       "train[15%:-20%]"
   assert ReadInstruction.from_spec(_UpperCamelCase							).to_spec() == spec_train_pct

   lowercase_		:      Dict          =       "train[:10%](closest)"
   assert ReadInstruction.from_spec(_UpperCamelCase							).to_spec() == "train[:10%]"

   lowercase_		:      Optional[int]          =       "train[:10%](pct1_dropremainder)"
   assert ReadInstruction.from_spec(_UpperCamelCase							).to_spec() == spec_train_pct_rounding

   lowercase_		:      str          =       "train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)"
   assert ReadInstruction.from_spec(_UpperCamelCase							).to_spec() == spec_train_test_pct_rounding




def 						__SCREAMING_SNAKE_CASE   (					):


   """simple docstring"""




   lowercase_		:      Union[str, Any]          =       "dummy"
   lowercase_		:      int          =       [SplitInfo(name="train"  ,						num_examples=100							)]
   lowercase_		:      List[str]          =       "train[:33%]"
   lowercase_		:      Dict          =       "arrow"
   lowercase_		:      Dict          =       "prefix"

   lowercase_		:      str          =       make_file_instructions(_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase							)
   assert isinstance(_UpperCamelCase  ,						_UpperCamelCase							)
   assert file_instructions.num_examples == 33
   assert file_instructions.file_instructions == [
       {"filename": os.path.join(_UpperCamelCase  ,						F"""{name}-train.arrow"""							), "skip": 0, "take": 33}
   ]

   lowercase_		:      str          =       [SplitInfo(name="train"  ,						num_examples=100  ,						shard_lengths=[10] * 10							)]
   lowercase_		:      str          =       make_file_instructions(_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase							)
   assert isinstance(_UpperCamelCase  ,						_UpperCamelCase							)
   assert file_instructions.num_examples == 33
   assert file_instructions.file_instructions == [
       {"filename": os.path.join(_UpperCamelCase  ,						F"""{name}-train-00000-of-00010.arrow"""							), "skip": 0, "take": -1},
       {"filename": os.path.join(_UpperCamelCase  ,						F"""{name}-train-00001-of-00010.arrow"""							), "skip": 0, "take": -1},
       {"filename": os.path.join(_UpperCamelCase  ,						F"""{name}-train-00002-of-00010.arrow"""							), "skip": 0, "take": -1},
       {"filename": os.path.join(_UpperCamelCase  ,						F"""{name}-train-00003-of-00010.arrow"""							), "skip": 0, "take": 3},
   ]





@pytest.mark.parametrize("name, expected_exception"  ,						[(None, TypeError), ("", ValueError)]							)
def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase  ,						_UpperCamelCase							):


   """simple docstring"""




   lowercase_		:      Optional[int]          =       [SplitInfo(name="train"  ,						num_examples=100							)]
   lowercase_		:      List[Any]          =       "train"
   lowercase_		:      Dict          =       "arrow"
   lowercase_		:      Dict          =       "prefix_path"
   with pytest.raises(_UpperCamelCase							):
      lowercase_		:      Union[str, Any]          =       make_file_instructions(_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase  ,						_UpperCamelCase							)












'''simple docstring'''
UpperCamelCase__	=     (1, 153, 370, 371, 1634, 24678051, 115132219018763992565095597973971522401)
UpperCamelCase__	=     (-153, -1, 0, 1.2, 200, "A", [], {}, None)




def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase							):


   """simple docstring"""




   if not isinstance(_UpperCamelCase  ,						_UpperCamelCase							) or n < 1:
      return False

   # Initialization of sum and number of digits.
   lowercase_		:      Optional[int]          =       0
   lowercase_		:      Any          =       0
   lowercase_		:      Optional[Any]          =       n
   # Calculation of digits of the number
   while temp > 0:
      number_of_digits += 1
      temp //= 10
   # Dividing number into separate digits and find Armstrong number
   lowercase_		:      int          =       n
   while temp > 0:
      lowercase_		:      Any          =       temp % 10
      total += rem**number_of_digits
      temp //= 10
   return n == total




def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase							):


   """simple docstring"""




   if not isinstance(_UpperCamelCase  ,						_UpperCamelCase							) or n < 1:
      return False

   # Init a "histogram" of the digits
   lowercase_		:      Optional[Any]          =       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
   lowercase_		:      Tuple          =       0
   lowercase_		:      Any          =       0
   lowercase_		:      Tuple          =       n
   while temp > 0:
      lowercase_      ,						lowercase_		:      Optional[Any]          =       divmod(_UpperCamelCase  ,						10							)
      digit_histogram[rem] += 1
      digit_total += 1

   for cnt, i in zip(_UpperCamelCase  ,						range(len(_UpperCamelCase							)							)							):
      total += cnt * i**digit_total

   return n == total




def 						__SCREAMING_SNAKE_CASE   (					_UpperCamelCase							):


   """simple docstring"""




   if not isinstance(_UpperCamelCase  ,						_UpperCamelCase							) or n < 1:
      return False
   lowercase_		:      List[str]          =       len(str(_UpperCamelCase							)							)  # the power that all digits will be raised to
   # check if sum of each digit multiplied expo times is equal to number
   return n == sum(int(_UpperCamelCase							) ** expo for i in str(_UpperCamelCase							)							)




def 						__SCREAMING_SNAKE_CASE   (					):


   """simple docstring"""




   lowercase_		:      Dict          =       int(input("Enter an integer to see if it is an Armstrong number: "							).strip()							)
   print(F"""{num} is {"" if armstrong_number(_UpperCamelCase							) else "not "}an Armstrong number."""							)
   print(F"""{num} is {"" if narcissistic_number(_UpperCamelCase							) else "not "}an Armstrong number."""							)
   print(F"""{num} is {"" if pluperfect_number(_UpperCamelCase							) else "not "}an Armstrong number."""							)


if __name__ == "__main__":
 import doctest

 doctest.testmod()
 main()




