code: 
  
  
  
  
  
  
  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 (    lowercase_							):
      """simple docstring"""
  
      def _a       (							self			:    Any   ,    __SCREAMING_SNAKE_CASE			:    List[str]   ,    __SCREAMING_SNAKE_CASE			:    Dict=False			)			->       Union[str, Any]:
             __UpperCAmelCase							,   __UpperCAmelCase							,   __UpperCAmelCase    							=(
                 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(__SCREAMING_SNAKE_CASE			)   ,    """wb"""			).close()
             __UpperCAmelCase    							=pa.Table.from_pydict({"""filename""": [Path(__SCREAMING_SNAKE_CASE			).name] * 100}			)
             if take == -1:
                    __UpperCAmelCase    							=len(__SCREAMING_SNAKE_CASE			) - skip
             if skip is not None and take is not None:
                    __UpperCAmelCase    							=pa_table.slice(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			)
             return pa_table
  
  
  
  
  class 							_A (    lowercase_							):
      """simple docstring"""
  
      def _a       (							self			:    Optional[int]			)			->       int:
             __UpperCAmelCase    							="""my_name"""
             __UpperCAmelCase    							=SplitInfo(name="""train"""   ,    num_examples=100			)
             __UpperCAmelCase    							=SplitInfo(name="""test"""   ,    num_examples=100			)
             __UpperCAmelCase    							=[train_info, test_info]
             __UpperCAmelCase    							=SplitDict()
             split_dict.add(__SCREAMING_SNAKE_CASE			)
             split_dict.add(__SCREAMING_SNAKE_CASE			)
             __UpperCAmelCase    							=DatasetInfo(splits=__SCREAMING_SNAKE_CASE			)
  
             with tempfile.TemporaryDirectory() as tmp_dir:
                    __UpperCAmelCase    							=ReaderTest(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			)
  
                    __UpperCAmelCase    							="""test[:33%]"""
                    __UpperCAmelCase    							=Dataset(**reader.read(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			)			)
                    self.assertEqual(dset["""filename"""][0]   ,    f'''{name}-test'''			)
                    self.assertEqual(dset.num_rows   ,    33			)
                    self.assertEqual(dset.num_columns   ,    1			)
  
                    __UpperCAmelCase    							=["""train""", """test[:33%]"""]
                    __UpperCAmelCase    							=[Split.TRAIN, ReadInstruction.from_spec("""test[:33%]"""			)]
                    for instructions in [instructionsa, instructionsa]:
                           __UpperCAmelCase    							=[reader.read(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			) for instr in instructions]
                           __UpperCAmelCase							,   __UpperCAmelCase    							=(Dataset(**__SCREAMING_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   ,    __SCREAMING_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   ,    __SCREAMING_SNAKE_CASE			)
                           self.assertEqual(str(test_dset.split			)   ,    """test[:33%]"""			)
                           del train_dset, test_dset
  
  
  
      def _a       (							self			:    Optional[Any]			)			->       List[str]:
             __UpperCAmelCase    							="""my_name"""
             __UpperCAmelCase    							=SplitInfo(name="""train"""   ,    num_examples=1000   ,    shard_lengths=[100] * 10			)
             __UpperCAmelCase    							=[train_info]
             __UpperCAmelCase    							=SplitDict()
             split_dict.add(__SCREAMING_SNAKE_CASE			)
             __UpperCAmelCase    							=DatasetInfo(splits=__SCREAMING_SNAKE_CASE			)
  
             with tempfile.TemporaryDirectory() as tmp_dir:
                    __UpperCAmelCase    							=ReaderTest(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			)
  
                    __UpperCAmelCase    							="""train[:33%]"""
                    __UpperCAmelCase    							=Dataset(**reader.read(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE   ,    __SCREAMING_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 _a       (							self			:    Optional[int]			)			->       Any:
             __UpperCAmelCase    							=SplitInfo(name="""train"""   ,    num_examples=100			)
             __UpperCAmelCase    							=SplitInfo(name="""test"""   ,    num_examples=100			)
             __UpperCAmelCase    							=SplitDict()
             split_dict.add(__SCREAMING_SNAKE_CASE			)
             split_dict.add(__SCREAMING_SNAKE_CASE			)
             __UpperCAmelCase    							=DatasetInfo(splits=__SCREAMING_SNAKE_CASE			)
  
             with tempfile.TemporaryDirectory() as tmp_dir:
                    __UpperCAmelCase    							=ReaderTest(__SCREAMING_SNAKE_CASE   ,    __SCREAMING_SNAKE_CASE			)
  
                    __UpperCAmelCase    							=[
                        {"""filename""": os.path.join(__SCREAMING_SNAKE_CASE   ,    """train"""			)},
                        {"""filename""": os.path.join(__SCREAMING_SNAKE_CASE   ,    """test"""			), """skip""": 10, """take""": 10},
                    ]
                    __UpperCAmelCase    							=Dataset(**reader.read_files(__SCREAMING_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 			lowercase__  (				A_:	List[str]     ,			A_:	Union[str, Any]     ,			A_:	List[str]		)  ->							Optional[int]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=arrow_file
         with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
                __UpperCAmelCase    							=ArrowReader.read_table(A_     ,			in_memory=A_		)
         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 			lowercase__  (				A_:	str     ,			A_:	Union[str, Any]     ,			A_:	str		)  ->							Optional[Any]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=arrow_file
         __UpperCAmelCase    							=ArrowReader(""""""     ,			A_		)
         with assert_arrow_memory_increases() if in_memory else assert_arrow_memory_doesnt_increase():
                __UpperCAmelCase    							=reader.read_files([{"""filename""": filename}]     ,			in_memory=A_		)
         assert dataset_kwargs.keys() == {"arrow_table", "info", "split"}
         __UpperCAmelCase    							=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 			lowercase__  (				)  ->							Optional[Any]:
  
  
  
  
  
  
  
         """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    							="""train+test"""
         assert ReadInstruction.from_spec(A_		).to_spec() == spec_train_test
  
         __UpperCAmelCase    							="""train[2:10]"""
         assert ReadInstruction.from_spec(A_		).to_spec() == spec_train_abs
  
         __UpperCAmelCase    							="""train[15%:-20%]"""
         assert ReadInstruction.from_spec(A_		).to_spec() == spec_train_pct
  
         __UpperCAmelCase    							="""train[:10%](closest)"""
         assert ReadInstruction.from_spec(A_		).to_spec() == "train[:10%]"
  
         __UpperCAmelCase    							="""train[:10%](pct1_dropremainder)"""
         assert ReadInstruction.from_spec(A_		).to_spec() == spec_train_pct_rounding
  
         __UpperCAmelCase    							="""train[:10%](pct1_dropremainder)+test[-10%:](pct1_dropremainder)"""
         assert ReadInstruction.from_spec(A_		).to_spec() == spec_train_test_pct_rounding
  
  
  
  
  
  
  
  def 			lowercase__  (				)  ->							Optional[Any]:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							="""dummy"""
         __UpperCAmelCase    							=[SplitInfo(name="""train"""     ,			num_examples=100		)]
         __UpperCAmelCase    							="""train[:33%]"""
         __UpperCAmelCase    							="""arrow"""
         __UpperCAmelCase    							="""prefix"""
  
         __UpperCAmelCase    							=make_file_instructions(A_     ,			A_     ,			A_     ,			A_     ,			A_		)
         assert isinstance(A_     ,			A_		)
         assert file_instructions.num_examples == 33
         assert file_instructions.file_instructions == [
             {"filename": os.path.join(A_     ,			F'''{name}-train.arrow'''		), "skip": 0, "take": 33}
         ]
  
         __UpperCAmelCase    							=[SplitInfo(name="""train"""     ,			num_examples=100     ,			shard_lengths=[10] * 10		)]
         __UpperCAmelCase    							=make_file_instructions(A_     ,			A_     ,			A_     ,			A_     ,			A_		)
         assert isinstance(A_     ,			A_		)
         assert file_instructions.num_examples == 33
         assert file_instructions.file_instructions == [
             {"filename": os.path.join(A_     ,			F'''{name}-train-00000-of-00010.arrow'''		), "skip": 0, "take": -1},
             {"filename": os.path.join(A_     ,			F'''{name}-train-00001-of-00010.arrow'''		), "skip": 0, "take": -1},
             {"filename": os.path.join(A_     ,			F'''{name}-train-00002-of-00010.arrow'''		), "skip": 0, "take": -1},
             {"filename": os.path.join(A_     ,			F'''{name}-train-00003-of-00010.arrow'''		), "skip": 0, "take": 3},
         ]
  
  
  
  
  
  
  
  @pytest.mark.parametrize("""name, expected_exception"""     ,			[(None, TypeError), ("""""", ValueError)]		)
  def 			lowercase__  (				A_:	Union[str, Any]     ,			A_:	Union[str, Any]		)  ->							int:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=[SplitInfo(name="""train"""     ,			num_examples=100		)]
         __UpperCAmelCase    							="""train"""
         __UpperCAmelCase    							="""arrow"""
         __UpperCAmelCase    							="""prefix_path"""
         with pytest.raises(A_		):
                __UpperCAmelCase    							=make_file_instructions(A_     ,			A_     ,			A_     ,			A_     ,			A_		)
  
  
  
  
  
style_context: 
  
  
  
  
  
  
  from __future__ import annotations
  
  __A  							=  [2, 1, 4, 5, 6, 0, 8, 9, 1, 2, 0, 6, 4, 2, 0, 6, 5, 3, 2, 7]
  
  
  
  class 							_A :
      """simple docstring"""
  
      def __init__(							self			:    List[str]   ,    __SCREAMING_SNAKE_CASE			:    int			)			->       None:
             __UpperCAmelCase    							=-1
             __UpperCAmelCase    							=-1
             __UpperCAmelCase    							=[-1] * length
             __UpperCAmelCase    							=None
             __UpperCAmelCase    							=None
  
  
  
      def __repr__(							self			:    Optional[int]			)			->       str:
             return f'''Node(min_value={self.minn} max_value={self.maxx})'''
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	list[int]		)  ->							Node | None:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         __UpperCAmelCase    							=Node(len(A_		)		)
         __UpperCAmelCase							,   __UpperCAmelCase    							=min(A_		), max(A_		)
         # Leaf node case where the node contains only one unique value
         if root.minn == root.maxx:
                return root
         __UpperCAmelCase    							=(root.minn + root.maxx) // 2
  
         __UpperCAmelCase    							=[]
         __UpperCAmelCase    							=[]
  
         for index, num in enumerate(A_		):
                if num <= pivot:
                       left_arr.append(A_		)
                else:
                       right_arr.append(A_		)
                __UpperCAmelCase    							=len(A_		)
         __UpperCAmelCase    							=build_tree(A_		)
         __UpperCAmelCase    							=build_tree(A_		)
         return root
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Node | None     ,			A_:	int     ,			A_:	int		)  ->							int:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         if index < 0 or node is None:
                return 0
         # Leaf node cases
         if node.minn == node.maxx:
                return index + 1 if node.minn == num else 0
         __UpperCAmelCase    							=(node.minn + node.maxx) // 2
         if num <= pivot:
                # go the left subtree and map index to the left subtree
                return rank_till_index(node.left     ,			A_     ,			node.map_left[index] - 1		)
         else:
                # go to the right subtree and map index to the right subtree
                return rank_till_index(node.right     ,			A_     ,			index - node.map_left[index]		)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Node | None     ,			A_:	int     ,			A_:	int     ,			A_:	int		)  ->							int:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         if start > end:
                return 0
         __UpperCAmelCase    							=rank_till_index(A_     ,			A_     ,			A_		)
         __UpperCAmelCase    							=rank_till_index(A_     ,			A_     ,			start - 1		)
         return rank_till_end - rank_before_start
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Node | None     ,			A_:	int     ,			A_:	int     ,			A_:	int		)  ->							int:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         if index > (end - start) or start > end or node is None:
                return -1
         # Leaf node case
         if node.minn == node.maxx:
                return node.minn
         # Number of elements in the left subtree in interval [start, end]
         __UpperCAmelCase    							=node.map_left[end] - (
             node.map_left[start - 1] if start else 0
         )
         if num_elements_in_left_tree > index:
                return quantile(
                    node.left     ,			A_     ,			(node.map_left[start - 1] if start else 0)     ,			node.map_left[end] - 1     ,			)
         else:
                return quantile(
                    node.right     ,			index - num_elements_in_left_tree     ,			start - (node.map_left[start - 1] if start else 0)     ,			end - node.map_left[end]     ,			)
  
  
  
  
  
  
  
  def 			lowercase__  (				A_:	Node | None     ,			A_:	int     ,			A_:	int     ,			A_:	int     ,			A_:	int		)  ->							int:
  
  
  
  
  
  
  
         """simple docstring"""
  
  
  
  
  
  
         if (
             start > end
             or node is None
             or start_num > end_num
             or node.minn > end_num
             or node.maxx < start_num
         ):
                return 0
         if start_num <= node.minn and node.maxx <= end_num:
                return end - start + 1
         __UpperCAmelCase    							=range_counting(
             node.left     ,			(node.map_left[start - 1] if start else 0)     ,			node.map_left[end] - 1     ,			A_     ,			A_     ,			)
         __UpperCAmelCase    							=range_counting(
             node.right     ,			start - (node.map_left[start - 1] if start else 0)     ,			end - node.map_left[end]     ,			A_     ,			A_     ,			)
         return left + right
  
  
  if __name__ == "__main__":
     import doctest
  
     doctest.testmod()
  
  
  
  
  