

from __future__ import annotations





def 	snake_case_      (__A     :						int					,   __A     :						int					,   __A     :						int     )			->	int:
   assert n > 1 and a > 0 and greatest_common_divisor(__A					,   __A     ) == 1
   ((__lowerCAmelCase)    ,(__lowerCAmelCase)    ,(__lowerCAmelCase))  :			Any					    =						extended_gcd(__A					,   __A     )  # Implemented below
   __lowerCAmelCase  :			Any					    =						(b * s) % n
   return x





def 	snake_case_      (__A     :						int					,   __A     :						int     )			->	int:
   ((__lowerCAmelCase)    ,(__lowerCAmelCase))  :			int					    =						extended_euclid(__A					,   __A     )  # Implemented below
   if b < 0:
      __lowerCAmelCase  :			str					    =						(b % n + n) % n
   return b





def 	snake_case_      (__A     :						int					,   __A     :						int					,   __A     :						int     )			->	int:
   __lowerCAmelCase  :			int					    =						invert_modulo(__A					,   __A     )
   __lowerCAmelCase  :			Dict					    =						(b * s) % n
   return x





def 	snake_case_      (__A     :						int					,   __A     :						int     )			->	tuple[int, int, int]:
   assert a >= 0 and b >= 0

   if b == 0:
      __lowerCAmelCase    ,__lowerCAmelCase    ,__lowerCAmelCase  :			Optional[int]					    =						a, 1, 0
   else:
      ((__lowerCAmelCase)    ,(__lowerCAmelCase)    ,(__lowerCAmelCase))  :			List[str]					    =						extended_gcd(__A					,   a % b     )
      __lowerCAmelCase  :			Union[str, Any]					    =						q
      __lowerCAmelCase  :			Any					    =						p - q * (a // b)

   assert a % d == 0 and b % d == 0
   assert d == a * x + b * y

   return (d, x, y)





def 	snake_case_      (__A     :						int					,   __A     :						int     )			->	tuple[int, int]:
   if b == 0:
      return (1, 0)
   ((__lowerCAmelCase)    ,(__lowerCAmelCase))  :			Optional[Any]					    =						extended_euclid(__A					,   a % b     )
   __lowerCAmelCase  :			Union[str, Any]					    =						a // b
   return (y, x - k * y)





def 	snake_case_      (__A     :						int					,   __A     :						int     )			->	int:
   if a < b:
      __lowerCAmelCase    ,__lowerCAmelCase  :			int					    =						b, a

   while a % b != 0:
      __lowerCAmelCase    ,__lowerCAmelCase  :			str					    =						b, a % b

   return b


if __name__ == "__main__":
   from doctest import testmod

   testmod(name="""modular_division""", verbose=True)
   testmod(name="""modular_division2""", verbose=True)
   testmod(name="""invert_modulo""", verbose=True)
   testmod(name="""extended_gcd""", verbose=True)
   testmod(name="""extended_euclid""", verbose=True)
   testmod(name="""greatest_common_divisor""", verbose=True)





import csv
import os

import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.csv import CsvDatasetReader, CsvDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases





def 	snake_case_      (__A     :						Any					,   __A     :						Union[str, Any]     )			->	int:
   assert isinstance(__A					,   __A     )
   assert dataset.num_rows == 4
   assert dataset.num_columns == 3
   assert dataset.column_names == ["col_1", "col_2", "col_3"]
   for feature, expected_dtype in expected_features.items():
      assert dataset.features[feature].dtype == expected_dtype





@pytest.mark.parametrize("""keep_in_memory"""					,   [False, True]     )
def 	snake_case_      (__A     :						int					,   __A     :						int					,   __A     :						List[str]     )			->	Tuple:
   __lowerCAmelCase  :			Dict					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			int					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
      __lowerCAmelCase  :			List[str]					    =						CsvDatasetReader(__A					,   cache_dir=__A					,   keep_in_memory=__A     ).read()
   _check_csv_dataset(__A					,   __A     )





@pytest.mark.parametrize(
    """features"""					,   [
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]					,   )
def 	snake_case_      (__A     :						Dict					,   __A     :						List[Any]					,   __A     :						List[str]     )			->	Union[str, Any]:
   __lowerCAmelCase  :			str					    =						tmp_path / """cache"""
   # CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
   __lowerCAmelCase  :			Tuple					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   __lowerCAmelCase  :			int					    =						features.copy() if features else default_expected_features
   __lowerCAmelCase  :			List[Any]					    =						(
       Features({feature: Value(__A     ) for feature, dtype in features.items()}     ) if features is not None else None
   )
   __lowerCAmelCase  :			Tuple					    =						CsvDatasetReader(__A					,   features=__A					,   cache_dir=__A     ).read()
   _check_csv_dataset(__A					,   __A     )





@pytest.mark.parametrize("""split"""					,   [None, NamedSplit("""train"""     ), """train""", """test"""]     )
def 	snake_case_      (__A     :						str					,   __A     :						Optional[int]					,   __A     :						int     )			->	Union[str, Any]:
   __lowerCAmelCase  :			str					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			Tuple					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   __lowerCAmelCase  :			Optional[Any]					    =						CsvDatasetReader(__A					,   cache_dir=__A					,   split=__A     ).read()
   _check_csv_dataset(__A					,   __A     )
   assert dataset.split == split if split else "train"





@pytest.mark.parametrize("""path_type"""					,   [str, list]     )
def 	snake_case_      (__A     :						Union[str, Any]					,   __A     :						Any					,   __A     :						List[str]     )			->	Optional[int]:
   if issubclass(__A					,   __A     ):
      __lowerCAmelCase  :			Dict					    =						csv_path
   elif issubclass(__A					,   __A     ):
      __lowerCAmelCase  :			Tuple					    =						[csv_path]
   __lowerCAmelCase  :			Dict					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			Dict					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   __lowerCAmelCase  :			List[str]					    =						CsvDatasetReader(__A					,   cache_dir=__A     ).read()
   _check_csv_dataset(__A					,   __A     )





def 	snake_case_      (__A     :						int					,   __A     :						Optional[int]					,   __A     :						Any=("train",)     )			->	Optional[int]:
   assert isinstance(__A					,   __A     )
   for split in splits:
      __lowerCAmelCase  :			int					    =						dataset_dict[split]
      assert dataset.num_rows == 4
      assert dataset.num_columns == 3
      assert dataset.column_names == ["col_1", "col_2", "col_3"]
      for feature, expected_dtype in expected_features.items():
         assert dataset.features[feature].dtype == expected_dtype





@pytest.mark.parametrize("""keep_in_memory"""					,   [False, True]     )
def 	snake_case_      (__A     :						Optional[Any]					,   __A     :						List[str]					,   __A     :						Any     )			->	List[str]:
   __lowerCAmelCase  :			int					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			Optional[Any]					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
      __lowerCAmelCase  :			Tuple					    =						CsvDatasetReader({"""train""": csv_path}					,   cache_dir=__A					,   keep_in_memory=__A     ).read()
   _check_csv_datasetdict(__A					,   __A     )





@pytest.mark.parametrize(
    """features"""					,   [
        None,
        {"""col_1""": """string""", """col_2""": """int64""", """col_3""": """float64"""},
        {"""col_1""": """string""", """col_2""": """string""", """col_3""": """string"""},
        {"""col_1""": """int32""", """col_2""": """int32""", """col_3""": """int32"""},
        {"""col_1""": """float32""", """col_2""": """float32""", """col_3""": """float32"""},
    ]					,   )
def 	snake_case_      (__A     :						List[str]					,   __A     :						List[Any]					,   __A     :						Optional[int]     )			->	Optional[int]:
   __lowerCAmelCase  :			Optional[int]					    =						tmp_path / """cache"""
   # CSV file loses col_1 string dtype information: default now is "int64" instead of "string"
   __lowerCAmelCase  :			Union[str, Any]					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   __lowerCAmelCase  :			Optional[int]					    =						features.copy() if features else default_expected_features
   __lowerCAmelCase  :			Optional[Any]					    =						(
       Features({feature: Value(__A     ) for feature, dtype in features.items()}     ) if features is not None else None
   )
   __lowerCAmelCase  :			Union[str, Any]					    =						CsvDatasetReader({"""train""": csv_path}					,   features=__A					,   cache_dir=__A     ).read()
   _check_csv_datasetdict(__A					,   __A     )





@pytest.mark.parametrize("""split"""					,   [None, NamedSplit("""train"""     ), """train""", """test"""]     )
def 	snake_case_      (__A     :						Optional[Any]					,   __A     :						Optional[Any]					,   __A     :						Union[str, Any]     )			->	Optional[int]:
   if split:
      __lowerCAmelCase  :			Any					    =						{split: csv_path}
   else:
      __lowerCAmelCase  :			Dict					    =						{"""train""": csv_path, """test""": csv_path}
   __lowerCAmelCase  :			List[str]					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			int					    =						{"""col_1""": """int64""", """col_2""": """int64""", """col_3""": """float64"""}
   __lowerCAmelCase  :			int					    =						CsvDatasetReader(__A					,   cache_dir=__A     ).read()
   _check_csv_datasetdict(__A					,   __A					,   splits=list(path.keys()     )     )
   assert all(dataset[split].split == split for split in path.keys()     )





def 	snake_case_      (__A     :						List[str]     )			->	Optional[Any]:
   with open(__A					,   encoding="""utf-8"""     ) as csvfile:
      yield from csv.reader(__A     )





def 	snake_case_      (__A     :						Optional[int]					,   __A     :						Optional[int]     )			->	Union[str, Any]:
   __lowerCAmelCase  :			Optional[Any]					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			Tuple					    =						os.path.join(__A					,   """tmp.csv"""     )
   __lowerCAmelCase  :			Tuple					    =						CsvDatasetReader({"""train""": csv_path}					,   cache_dir=__A     ).read()
   CsvDatasetWriter(dataset["""train"""]					,   __A					,   num_proc=1     ).write()

   __lowerCAmelCase  :			Dict					    =						iter_csv_file(__A     )
   __lowerCAmelCase  :			Union[str, Any]					    =						iter_csv_file(__A     )

   for rowa, rowa in zip(__A					,   __A     ):
      assert rowa == rowa





def 	snake_case_      (__A     :						int					,   __A     :						List[str]     )			->	str:
   __lowerCAmelCase  :			Any					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			List[str]					    =						os.path.join(__A					,   """tmp.csv"""     )
   __lowerCAmelCase  :			Dict					    =						CsvDatasetReader({"""train""": csv_path}					,   cache_dir=__A     ).read()
   CsvDatasetWriter(dataset["""train"""]					,   __A					,   num_proc=2     ).write()

   __lowerCAmelCase  :			Union[str, Any]					    =						iter_csv_file(__A     )
   __lowerCAmelCase  :			Dict					    =						iter_csv_file(__A     )

   for rowa, rowa in zip(__A					,   __A     ):
      assert rowa == rowa





def 	snake_case_      (__A     :						int					,   __A     :						Optional[Any]     )			->	List[str]:
   __lowerCAmelCase  :			Any					    =						tmp_path / """cache"""
   __lowerCAmelCase  :			List[Any]					    =						os.path.join(__A					,   """tmp.csv"""     )
   __lowerCAmelCase  :			List[str]					    =						CsvDatasetReader({"""train""": csv_path}					,   cache_dir=__A     ).read()
   with pytest.raises(__A     ):
      CsvDatasetWriter(dataset["""train"""]					,   __A					,   num_proc=0     )


