
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


_A     =  "http://www.mocksite.com/file1.txt"
_A     =  "\"text\": [\"foo\", \"foo\"]"
_A     =  "6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8"





class 			_lowerCAmelCase      :
				_lowercase    	=2_00
				_lowercase    	={'''Content-Length''': '''100'''}
				_lowercase    	={}




				def   __a		(      self       ,       **_UpperCamelCase  )       ->   Optional[Any]:
							return [bytes(_UpperCamelCase       ,       "utf-8"  )]






def      lowerCamelCase__       (       *__lowerCAmelCase						:						Union[str, Any]					,							**__lowerCAmelCase						:						Optional[int]       ):




			"""simple docstring"""


			return MockResponse()






@pytest.mark.parametrize("urls_type"					,							[str, list, dict]       )
def      lowerCamelCase__       (       __lowerCAmelCase						:						Optional[Any]					,							__lowerCAmelCase						:						List[Any]					,							__lowerCAmelCase						:						str       ):




			"""simple docstring"""


			import requests

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

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






@pytest.mark.parametrize("paths_type"					,							[str, list, dict]       )
def      lowerCamelCase__       (       __lowerCAmelCase						:						int					,							__lowerCAmelCase						:						Any					,							__lowerCAmelCase						:						str       ):




			"""simple docstring"""


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






def      lowerCamelCase__       (       __lowerCAmelCase						:						List[str]					,							__lowerCAmelCase						:						Tuple       ):




			"""simple docstring"""


			assert path.endswith(".jsonl"       )
			for num_items, line in enumerate(__lowerCAmelCase					,							start=1       ):
						lowerCAmelCase_      			=			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      lowerCamelCase__       (       __lowerCAmelCase						:						Any					,							__lowerCAmelCase						:						int       ):




			"""simple docstring"""


			lowerCAmelCase_      			=			request.getfixturevalue(__lowerCAmelCase       )
			lowerCAmelCase_      			=			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      lowerCamelCase__       (       __lowerCAmelCase						:						Optional[Any]					,							__lowerCAmelCase						:						int       ):




			"""simple docstring"""


			lowerCAmelCase_      			=			request.getfixturevalue(__lowerCAmelCase       )
			lowerCAmelCase_      			=			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      lowerCamelCase__       (       __lowerCAmelCase						:						int       ):




			"""simple docstring"""


			lowerCAmelCase_      			=			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





def      lowerCamelCase__       (       __lowerCAmelCase						:						int       ):




			"""simple docstring"""


			if upper_limit < 0:
						raise ValueError("Limit for the Catalan sequence must be ≥ 0"       )

			lowerCAmelCase_      			=			[0] * (upper_limit + 1)

			# Base case: C(0) = C(1) = 1
			lowerCAmelCase_      			=			1
			if upper_limit > 0:
						lowerCAmelCase_      			=			1

			# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
			for i in range(2					,							upper_limit + 1       ):
						for j in range(__lowerCAmelCase       ):
									catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

			return catalan_list


if __name__ == "__main__":
			print("\n********* Catalan Numbers Using Dynamic Programming ************\n")
			print("\n*** Enter -1 at any time to quit ***")
			print("\nEnter the upper limit (≥ 0) for the Catalan number sequence: ", end="")
			try:
						while True:
									_A     =  int(input().strip())
									if N < 0:
												print("\n********* Goodbye!! ************")
												break
									else:
												print(f"""The Catalan numbers from 0 through {N} are:""")
												print(catalan_numbers(N))
												print("Try another upper limit for the sequence: ", end="")
			except (NameError, ValueError):
						print("\n********* Invalid input, goodbye! ************\n")

			import doctest

			doctest.testmod()



