






"""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 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  def 					UpperCamelCase__   (      self:		str				,			UpperCamelCase_:		Optional[int]				,			UpperCamelCase_:		Optional[int]=False					):
     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(UpperCamelCase_					)				,			"wb"					).close()
     UpperCamelCase_     =pa.Table.from_pydict({"filename": [Path(UpperCamelCase_					).name] * 100}					)
     if take == -1:
        UpperCamelCase_     =len(UpperCamelCase_					) - skip
     if skip is not None and take is not None:
        UpperCamelCase_     =pa_table.slice(UpperCamelCase_				,			UpperCamelCase_					)
     return pa_table




class 						__lowerCAmelCase   (			UpperCAmelCase       ):







  '''simple docstring'''


  def 					UpperCamelCase__   (      self:		Tuple					):
     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(UpperCamelCase_					)
     split_dict.add(UpperCamelCase_					)
     UpperCamelCase_     =DatasetInfo(splits=UpperCamelCase_					)

     with tempfile.TemporaryDirectory() as tmp_dir:
        UpperCamelCase_     =ReaderTest(UpperCamelCase_				,			UpperCamelCase_					)

        UpperCamelCase_     ="test[:33%]"
        UpperCamelCase_     =Dataset(**reader.read(UpperCamelCase_				,			UpperCamelCase_				,			UpperCamelCase_					)					)
        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(UpperCamelCase_				,			UpperCamelCase_				,			UpperCamelCase_					) for instr in instructions]
           UpperCamelCase_    ,							UpperCamelCase_     =(Dataset(**UpperCamelCase_					) 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				,			UpperCamelCase_					)
           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				,			UpperCamelCase_					)
           self.assertEqual(str(test_dset.split					)				,			"test[:33%]"					)
           del train_dset, test_dset
  def 					UpperCamelCase__   (      self:		List[Any]					):
     UpperCamelCase_     ="my_name"
     UpperCamelCase_     =SplitInfo(name="train"				,			num_examples=1000				,			shard_lengths=[100] * 10					)
     UpperCamelCase_     =[train_info]
     UpperCamelCase_     =SplitDict()
     split_dict.add(UpperCamelCase_					)
     UpperCamelCase_     =DatasetInfo(splits=UpperCamelCase_					)

     with tempfile.TemporaryDirectory() as tmp_dir:
        UpperCamelCase_     =ReaderTest(UpperCamelCase_				,			UpperCamelCase_					)

        UpperCamelCase_     ="train[:33%]"
        UpperCamelCase_     =Dataset(**reader.read(UpperCamelCase_				,			UpperCamelCase_				,			UpperCamelCase_					)					)
        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 					UpperCamelCase__   (      self:		List[Any]					):
     UpperCamelCase_     =SplitInfo(name="train"				,			num_examples=100					)
     UpperCamelCase_     =SplitInfo(name="test"				,			num_examples=100					)
     UpperCamelCase_     =SplitDict()
     split_dict.add(UpperCamelCase_					)
     split_dict.add(UpperCamelCase_					)
     UpperCamelCase_     =DatasetInfo(splits=UpperCamelCase_					)

     with tempfile.TemporaryDirectory() as tmp_dir:
        UpperCamelCase_     =ReaderTest(UpperCamelCase_				,			UpperCamelCase_					)

        UpperCamelCase_     =[
            {"filename": os.path.join(UpperCamelCase_				,			"train"					)},
            {"filename": os.path.join(UpperCamelCase_				,			"test"					), "skip": 10, "take": 10},
        ]
        UpperCamelCase_     =Dataset(**reader.read_files(UpperCamelCase_				,			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 						_UpperCamelCase			(  A    ,       A    ,       A			):
   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 						_UpperCamelCase			(  A    ,       A    ,       A			):
   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 						_UpperCamelCase			(  ):
   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 						_UpperCamelCase			(  ):
   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 						_UpperCamelCase			(  A    ,       A			):
   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			)








"""simple docstring"""






from __future__ import annotations

from pathlib import Path




def 						_UpperCamelCase			(  A    ,       A			):
   return pointa[0] * pointa[1] - pointa[1] * pointa[0]




def 						_UpperCamelCase			(  A    ,       A    ,       A    ,       A    ,       A    ,       A			):
   UpperCamelCase_     =(xa, ya)
   UpperCamelCase_     =(xa - xa, ya - ya)
   UpperCamelCase_     =(xa - xa, ya - ya)
   UpperCamelCase_     =-vector_product(A    ,       A			) / vector_product(
       A    ,       A			)
   UpperCamelCase_     =+vector_product(A    ,       A			) / vector_product(
       A    ,       A			)

   return a > 0 and b > 0 and a + b < 1




def 						_UpperCamelCase			(  A = "p102_triangles.txt"			):
   UpperCamelCase_     =Path(A			).parent.joinpath(A			).read_text(encoding="utf-8"			)

   UpperCamelCase_     =[]
   for line in data.strip().split("\n"			):
      triangles.append([int(A			) for number in line.split(","			)]			)

   UpperCamelCase_     =0
   UpperCamelCase_     =42

   for triangle in triangles:
      ret += contains_origin(*A			)

   return ret


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