


'''simple docstring'''



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 	a				(		__SCREAMING_SNAKE_CASE       ):


     """simple docstring"""






     def  lowerCamelCase__ (				self     :		Union[str, Any]							, snake_case     :		Dict							, snake_case     :		str=False			)						->							Any:
          __UpperCAmelCase	: str  							=			(
              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(snake_case			)							, '''wb'''			).close()
          __UpperCAmelCase	: List[Any]  							=			pa.Table.from_pydict({'''filename''': [Path(snake_case			).name] * 100}			)
          if take == -1:
               __UpperCAmelCase	: Dict  							=			len(snake_case			) - skip
          if skip is not None and take is not None:
               __UpperCAmelCase	: Tuple  							=			pa_table.slice(snake_case							, snake_case			)
          return pa_table



class 	a				(		__SCREAMING_SNAKE_CASE       ):


     """simple docstring"""






     def  lowerCamelCase__ (				self     :		Any			)						->							Dict:
          __UpperCAmelCase	: int  							=			'''my_name'''
          __UpperCAmelCase	: str  							=			SplitInfo(name='''train'''							, num_examples=100			)
          __UpperCAmelCase	: str  							=			SplitInfo(name='''test'''							, num_examples=100			)
          __UpperCAmelCase	: Dict  							=			[train_info, test_info]
          __UpperCAmelCase	: Union[str, Any]  							=			SplitDict()
          split_dict.add(snake_case			)
          split_dict.add(snake_case			)
          __UpperCAmelCase	: Dict  							=			DatasetInfo(splits=snake_case			)

          with tempfile.TemporaryDirectory() as tmp_dir:
               __UpperCAmelCase	: Optional[Any]  							=			ReaderTest(snake_case							, snake_case			)

               __UpperCAmelCase	: Optional[Any]  							=			'''test[:33%]'''
               __UpperCAmelCase	: Any  							=			Dataset(**reader.read(snake_case							, snake_case							, snake_case			)			)
               self.assertEqual(dset['''filename'''][0]							, f'{name}-test'			)
               self.assertEqual(dset.num_rows							, 33			)
               self.assertEqual(dset.num_columns							, 1			)

               __UpperCAmelCase	: Union[str, Any]  							=			['''train''', '''test[:33%]''']
               __UpperCAmelCase	: Union[str, Any]  							=			[Split.TRAIN, ReadInstruction.from_spec('''test[:33%]'''			)]
               for instructions in [instructionsa, instructionsa]:
                    __UpperCAmelCase	: List[str]  							=			[reader.read(snake_case							, snake_case							, snake_case			) for instr in instructions]
                    __UpperCAmelCase	: int  							=			(Dataset(**snake_case			) for dataset_kwargs in datasets_kwargs)
                    self.assertEqual(train_dset['''filename'''][0]							, f'{name}-train'			)
                    self.assertEqual(train_dset.num_rows							, 100			)
                    self.assertEqual(train_dset.num_columns							, 1			)
                    self.assertIsInstance(train_dset.split							, snake_case			)
                    self.assertEqual(str(train_dset.split			)							, '''train'''			)
                    self.assertEqual(test_dset['''filename'''][0]							, f'{name}-test'			)
                    self.assertEqual(test_dset.num_rows							, 33			)
                    self.assertEqual(test_dset.num_columns							, 1			)
                    self.assertIsInstance(test_dset.split							, snake_case			)
                    self.assertEqual(str(test_dset.split			)							, '''test[:33%]'''			)
                    del train_dset, test_dset

     def  lowerCamelCase__ (				self     :		Optional[Any]			)						->							List[Any]:
          __UpperCAmelCase	: List[Any]  							=			'''my_name'''
          __UpperCAmelCase	: int  							=			SplitInfo(name='''train'''							, num_examples=1000							, shard_lengths=[100] * 10			)
          __UpperCAmelCase	: Tuple  							=			[train_info]
          __UpperCAmelCase	: List[str]  							=			SplitDict()
          split_dict.add(snake_case			)
          __UpperCAmelCase	: Tuple  							=			DatasetInfo(splits=snake_case			)

          with tempfile.TemporaryDirectory() as tmp_dir:
               __UpperCAmelCase	: Tuple  							=			ReaderTest(snake_case							, snake_case			)

               __UpperCAmelCase	: Optional[Any]  							=			'''train[:33%]'''
               __UpperCAmelCase	: List[Any]  							=			Dataset(**reader.read(snake_case							, snake_case							, snake_case			)			)
               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							, 330			)
               self.assertEqual(dset.num_columns							, 1			)






     def  lowerCamelCase__ (				self     :		Dict			)						->							List[Any]:
          __UpperCAmelCase	: List[str]  							=			SplitInfo(name='''train'''							, num_examples=100			)
          __UpperCAmelCase	: Tuple  							=			SplitInfo(name='''test'''							, num_examples=100			)
          __UpperCAmelCase	: int  							=			SplitDict()
          split_dict.add(snake_case			)
          split_dict.add(snake_case			)
          __UpperCAmelCase	: str  							=			DatasetInfo(splits=snake_case			)

          with tempfile.TemporaryDirectory() as tmp_dir:
               __UpperCAmelCase	: Any  							=			ReaderTest(snake_case							, snake_case			)

               __UpperCAmelCase	: List[Any]  							=			[
                   {'''filename''': os.path.join(snake_case							, '''train'''			)},
                   {'''filename''': os.path.join(snake_case							, '''test'''			), '''skip''': 10, '''take''': 10},
               ]
               __UpperCAmelCase	: Optional[int]  							=			Dataset(**reader.read_files(snake_case							, original_instructions='''train+test[10:20]'''			)			)
               self.assertEqual(dset.num_rows							, 110			)
               self.assertEqual(dset.num_columns							, 1			)
               del dset




@pytest.mark.parametrize('''in_memory'''			,					[False, True]  )
def 	_a	(						_lowercase					:	List[str]			,					_lowercase					:	List[Any]			,					_lowercase					:	str  ):





     '''simple docstring'''






     __UpperCAmelCase	: Optional[int]  							=			arrow_file
     with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
          __UpperCAmelCase	: Optional[Any]  							=			ArrowReader.read_table(_lowercase			,					in_memory=_lowercase  )
     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 	_a	(						_lowercase					:	Optional[int]			,					_lowercase					:	Optional[int]			,					_lowercase					:	Tuple  ):





     '''simple docstring'''






     __UpperCAmelCase	: Optional[Any]  							=			arrow_file
     __UpperCAmelCase	: Optional[int]  							=			ArrowReader(''''''			,					_lowercase  )
     with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
          __UpperCAmelCase	: Union[str, Any]  							=			reader.read_files([{'''filename''': filename}]			,					in_memory=_lowercase  )
     assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
     __UpperCAmelCase	: List[str]  							=			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 	_a	(						):





     '''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%]"

     __UpperCAmelCase	: Union[str, Any]  							=			'''train+test'''
     assert ReadInstruction.from_spec(_lowercase  ).to_spec() == spec_train_test

     __UpperCAmelCase	: Optional[Any]  							=			'''train[2:10]'''
     assert ReadInstruction.from_spec(_lowercase  ).to_spec() == spec_train_abs

     __UpperCAmelCase	: List[str]  							=			'''train[15%:-20%]'''
     assert ReadInstruction.from_spec(_lowercase  ).to_spec() == spec_train_pct

     __UpperCAmelCase	: Optional[Any]  							=			'''train[:10%](closest)'''
     assert ReadInstruction.from_spec(_lowercase  ).to_spec() == "train[:10%]"

     __UpperCAmelCase	: Union[str, Any]  							=			'''train[:10%](pct1_dropremainder)'''
     assert ReadInstruction.from_spec(_lowercase  ).to_spec() == spec_train_pct_rounding

     __UpperCAmelCase	: Optional[Any]  							=			'''train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)'''
     assert ReadInstruction.from_spec(_lowercase  ).to_spec() == spec_train_test_pct_rounding




def 	_a	(						):





     '''simple docstring'''






     __UpperCAmelCase	: Optional[int]  							=			'''dummy'''
     __UpperCAmelCase	: int  							=			[SplitInfo(name='''train'''			,					num_examples=100  )]
     __UpperCAmelCase	: List[Any]  							=			'''train[:33%]'''
     __UpperCAmelCase	: Optional[Any]  							=			'''arrow'''
     __UpperCAmelCase	: Any  							=			'''prefix'''

     __UpperCAmelCase	: Optional[Any]  							=			make_file_instructions(_lowercase			,					_lowercase			,					_lowercase			,					_lowercase			,					_lowercase  )
     assert isinstance(_lowercase			,					_lowercase  )
     assert file_instructions.num_examples == 33
     assert file_instructions.file_instructions == [
         {"filename": os.path.join(_lowercase			,					F'{name}-train.arrow'  ), "skip": 0, "take": 33}
     ]

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




@pytest.mark.parametrize('''name, expected_exception'''			,					[(None, TypeError), ('''''', ValueError)]  )
def 	_a	(						_lowercase					:	Any			,					_lowercase					:	Tuple  ):





     '''simple docstring'''






     __UpperCAmelCase	: str  							=			[SplitInfo(name='''train'''			,					num_examples=100  )]
     __UpperCAmelCase	: Union[str, Any]  							=			'''train'''
     __UpperCAmelCase	: Tuple  							=			'''arrow'''
     __UpperCAmelCase	: List[Any]  							=			'''prefix_path'''
     with pytest.raises(_lowercase  ):
          __UpperCAmelCase	: int  							=			make_file_instructions(_lowercase			,					_lowercase			,					_lowercase			,					_lowercase			,					_lowercase  )





def  lowerCAmelCase  (	UpperCAmelCase       )  ->int:






					"""simple docstring"""






					return n * (2 * n - 1)







def  lowerCAmelCase  (	UpperCAmelCase       )  ->bool:






					"""simple docstring"""






					__magic_name__					:							Any		   =							(1 + 24 * n) ** 0.5
					return ((1 + root) / 6) % 1 == 0







def  lowerCAmelCase  (	UpperCAmelCase = 144       )  ->int:






					"""simple docstring"""






					__magic_name__					:							Optional[int]		   =							start
					__magic_name__					:							int		   =							hexagonal_num(UpperCAmelCase       )
					while not is_pentagonal(UpperCAmelCase       ):
										n += 1
										__magic_name__					:							Optional[int]		   =							hexagonal_num(UpperCAmelCase       )
					return num


if __name__ == "__main__":
				print(f"{solution()} = ")



