





import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


snake_case							:	Tuple					      =				'''http://www.mocksite.com/file1.txt'''
snake_case							:	Optional[Any]					      =				'''"text": ["foo", "foo"]'''
snake_case							:	List[str]					      =				'''6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'''







class 	snake_case_       :
					UpperCAmelCase__ :   Union[str, Any]   				=      2_0_0
					UpperCAmelCase__ :   str   				=      {'''Content-Length''': '''100'''}
					UpperCAmelCase__ :   Any   				=      {}
					def 			lowerCamelCase__(				self       :Optional[int]      ,**__snake_case       :List[Any]				) ->   Optional[Any]:
												return [bytes(__snake_case      ,'utf-8'				)]






def 							__lowercase       (			*__lowerCAmelCase							:    Tuple ,   **__lowerCAmelCase							:    Any       ):
							return MockResponse()






@pytest.mark.parametrize('urls_type' ,   [str, list, dict]       )
def 							__lowercase       (			__lowerCAmelCase							:    List[str] ,   __lowerCAmelCase							:    Any ,   __lowerCAmelCase							:    List[Any]       ):
							import requests

							monkeypatch.setattr(__lowerCAmelCase ,   'request' ,   __lowerCAmelCase       )

							a__					=					URL
							if issubclass(__lowerCAmelCase ,   __lowerCAmelCase       ):
														a__					=					url
							elif issubclass(__lowerCAmelCase ,   __lowerCAmelCase       ):
														a__					=					[url]
							elif issubclass(__lowerCAmelCase ,   __lowerCAmelCase       ):
														a__					=					{'train': url}
							a__					=					'dummy'
							a__					=					'downloads'
							a__					=					tmp_path
							a__					=					DownloadConfig(
							    cache_dir=os.path.join(__lowerCAmelCase ,   __lowerCAmelCase       ) ,   use_etag=__lowerCAmelCase ,   )
							a__					=					DownloadManager(dataset_name=__lowerCAmelCase ,   download_config=__lowerCAmelCase       )
							a__					=					dl_manager.download(__lowerCAmelCase       )
							a__					=					urls
							for downloaded_paths in [downloaded_paths]:
														if isinstance(__lowerCAmelCase ,   __lowerCAmelCase       ):
																					a__					=					[downloaded_paths]
																					a__					=					[urls]
														elif isinstance(__lowerCAmelCase ,   __lowerCAmelCase       ):
																					assert "train" in downloaded_paths.keys()
																					a__					=					downloaded_paths.values()
																					a__					=					urls.values()
														assert downloaded_paths
														for downloaded_path, input_url in zip(__lowerCAmelCase ,   __lowerCAmelCase       ):
																					assert downloaded_path == dl_manager.downloaded_paths[input_url]
																					a__					=					Path(__lowerCAmelCase       )
																					a__					=					downloaded_path.parts
																					assert parts[-1] == HASH
																					assert parts[-2] == cache_subdir
																					assert downloaded_path.exists()
																					a__					=					downloaded_path.read_text()
																					assert content == CONTENT
																					a__					=					downloaded_path.with_suffix('.json'       )
																					assert metadata_downloaded_path.exists()
																					a__					=					json.loads(metadata_downloaded_path.read_text()       )
																					assert metadata_content == {"url": URL, "etag": None}






@pytest.mark.parametrize('paths_type' ,   [str, list, dict]       )
def 							__lowercase       (			__lowerCAmelCase							:    Dict ,   __lowerCAmelCase							:    str ,   __lowerCAmelCase							:    Dict       ):
							a__					=					str(__lowerCAmelCase       )
							if issubclass(__lowerCAmelCase ,   __lowerCAmelCase       ):
														a__					=					filename
							elif issubclass(__lowerCAmelCase ,   __lowerCAmelCase       ):
														a__					=					[filename]
							elif issubclass(__lowerCAmelCase ,   __lowerCAmelCase       ):
														a__					=					{'train': filename}
							a__					=					'dummy'
							a__					=					xz_file.parent
							a__					=					'extracted'
							a__					=					DownloadConfig(
							    cache_dir=__lowerCAmelCase ,   use_etag=__lowerCAmelCase ,   )
							a__					=					DownloadManager(dataset_name=__lowerCAmelCase ,   download_config=__lowerCAmelCase       )
							a__					=					dl_manager.extract(__lowerCAmelCase       )
							a__					=					paths
							for extracted_paths in [extracted_paths]:
														if isinstance(__lowerCAmelCase ,   __lowerCAmelCase       ):
																					a__					=					[extracted_paths]
																					a__					=					[paths]
														elif isinstance(__lowerCAmelCase ,   __lowerCAmelCase       ):
																					assert "train" in extracted_paths.keys()
																					a__					=					extracted_paths.values()
																					a__					=					paths.values()
														assert extracted_paths
														for extracted_path, input_path in zip(__lowerCAmelCase ,   __lowerCAmelCase       ):
																					assert extracted_path == dl_manager.extracted_paths[input_path]
																					a__					=					Path(__lowerCAmelCase       )
																					a__					=					extracted_path.parts
																					assert parts[-1] == hash_url_to_filename(__lowerCAmelCase ,   etag=__lowerCAmelCase       )
																					assert parts[-2] == extracted_subdir
																					assert extracted_path.exists()
																					a__					=					extracted_path.read_text()
																					a__					=					text_file.read_text()
																					assert extracted_file_content == expected_file_content






def 							__lowercase       (			__lowerCAmelCase							:    Dict ,   __lowerCAmelCase							:    List[Any]       ):
							assert path.endswith('.jsonl'       )
							for num_items, line in enumerate(__lowerCAmelCase ,   start=1       ):
														a__					=					json.loads(line.decode('utf-8'       )       )
														assert item.keys() == {"col_1", "col_2", "col_3"}
							assert num_items == 4






@pytest.mark.parametrize('archive_jsonl' ,   ['tar_jsonl_path', 'zip_jsonl_path']       )
def 							__lowercase       (			__lowerCAmelCase							:    str ,   __lowerCAmelCase							:    List[str]       ):
							a__					=					request.getfixturevalue(__lowerCAmelCase       )
							a__					=					DownloadManager()
							for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(__lowerCAmelCase       ) ,   start=1       ):
														_test_jsonl(__lowerCAmelCase ,   __lowerCAmelCase       )
							assert num_jsonl == 2






@pytest.mark.parametrize('archive_nested_jsonl' ,   ['tar_nested_jsonl_path', 'zip_nested_jsonl_path']       )
def 							__lowercase       (			__lowerCAmelCase							:    List[str] ,   __lowerCAmelCase							:    Tuple       ):
							a__					=					request.getfixturevalue(__lowerCAmelCase       )
							a__					=					DownloadManager()
							for num_tar, (path, file) in enumerate(dl_manager.iter_archive(__lowerCAmelCase       ) ,   start=1       ):
														for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(__lowerCAmelCase       ) ,   start=1       ):
																					_test_jsonl(__lowerCAmelCase ,   __lowerCAmelCase       )
							assert num_tar == 1
							assert num_jsonl == 2






def 							__lowercase       (			__lowerCAmelCase							:    Any       ):
							a__					=					DownloadManager()
							for num_file, file in enumerate(dl_manager.iter_files(__lowerCAmelCase       ) ,   start=1       ):
														assert os.path.basename(__lowerCAmelCase       ) == ("test.txt" if num_file == 1 else "train.txt")
							assert num_file == 2








from __future__ import annotations






def 							__lowercase       (			__lowerCAmelCase							:    List[Any] ,   __lowerCAmelCase							:    Optional[Any] ,   __lowerCAmelCase							:    str ,   __lowerCAmelCase							:    List[str]       ):  # noqa: E741
							while r - l > 1:
														a__					=					(l + r) // 2
														if v[m] >= key:
																					a__					=					m
														else:
																					a__					=					m  # noqa: E741
							return r






def 							__lowercase       (			__lowerCAmelCase							:    list[int]       ):
							if len(__lowerCAmelCase       ) == 0:
														return 0

							a__					=					[0] * len(__lowerCAmelCase       )
							a__					=					1

							a__					=					v[0]

							for i in range(1 ,   len(__lowerCAmelCase       )       ):
														if v[i] < tail[0]:
																					a__					=					v[i]
														elif v[i] > tail[length - 1]:
																					a__					=					v[i]
																					length += 1
														else:
																					a__					=					v[i]

							return length


if __name__ == "__main__":
						import doctest

						doctest.testmod()




