






import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


__UpperCamelCase   :   Tuple      =    """hf-internal-testing/tiny-random-bert"""
__UpperCamelCase   :   str      =    os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
__UpperCamelCase   :   Optional[Any]      =    """9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""


class 							__UpperCamelCase     (  unittest.TestCase ):







	def 			_a (	self   :				List[Any]      )		->			str:
		"""simple docstring"""







		__lowercase   							=	cached_file(_lowerCAmelCase							,					_lowerCAmelCase      )
		# Should have downloaded the file in here
		self.assertTrue(os.path.isdir(_lowerCAmelCase      )      )
		# Cache should contain at least those three subfolders:
		for subfolder in ["blobs", "refs", "snapshots"]:
			self.assertTrue(os.path.isdir(os.path.join(_lowerCAmelCase							,					_lowerCAmelCase      )      )      )
		with open(os.path.join(_lowerCAmelCase							,					"""refs"""							,					"""main"""      )      ) as f:
			__lowercase   							=	f.read()
		self.assertEqual(_lowerCAmelCase							,					os.path.join(_lowerCAmelCase							,					"""snapshots"""							,					_lowerCAmelCase							,					_lowerCAmelCase      )      )
		self.assertTrue(os.path.isfile(_lowerCAmelCase      )      )

		# File is cached at the same place the second time.
		__lowercase   							=	cached_file(_lowerCAmelCase							,					_lowerCAmelCase      )
		self.assertEqual(_lowerCAmelCase							,					_lowerCAmelCase      )

		# Using a specific revision to test the full commit hash.
		__lowercase   							=	cached_file(_lowerCAmelCase							,					_lowerCAmelCase							,					revision="""9b8c223"""      )
		self.assertEqual(_lowerCAmelCase							,					os.path.join(_lowerCAmelCase							,					"""snapshots"""							,					_lowerCAmelCase							,					_lowerCAmelCase      )      )







	def 			_a (	self   :				Optional[int]      )		->			Any:
		"""simple docstring"""







		with self.assertRaisesRegex(_lowerCAmelCase							,					"""is not a valid model identifier"""      ):
			__lowercase   							=	cached_file("""tiny-random-bert"""							,					_lowerCAmelCase      )

		with self.assertRaisesRegex(_lowerCAmelCase							,					"""is not a valid git identifier"""      ):
			__lowercase   							=	cached_file(_lowerCAmelCase							,					_lowerCAmelCase							,					revision="""aaaa"""      )

		with self.assertRaisesRegex(_lowerCAmelCase							,					"""does not appear to have a file named"""      ):
			__lowercase   							=	cached_file(_lowerCAmelCase							,					"""conf"""      )







	def 			_a (	self   :				Tuple      )		->			str:
		"""simple docstring"""







		with self.assertRaisesRegex(_lowerCAmelCase							,					"""does not appear to have a file named"""      ):
			__lowercase   							=	cached_file(_lowerCAmelCase							,					"""conf"""      )

		with open(os.path.join(_lowerCAmelCase							,					"""refs"""							,					"""main"""      )      ) as f:
			__lowercase   							=	f.read()
		self.assertTrue(os.path.isfile(os.path.join(_lowerCAmelCase							,					""".no_exist"""							,					_lowerCAmelCase							,					"""conf"""      )      )      )

		__lowercase   							=	cached_file(_lowerCAmelCase							,					"""conf"""							,					_raise_exceptions_for_missing_entries=_lowerCAmelCase      )
		self.assertIsNone(_lowerCAmelCase      )

		__lowercase   							=	cached_file(_lowerCAmelCase							,					"""conf"""							,					local_files_only=_lowerCAmelCase							,					_raise_exceptions_for_missing_entries=_lowerCAmelCase      )
		self.assertIsNone(_lowerCAmelCase      )

		__lowercase   							=	mock.Mock()
		__lowercase   							=	500
		__lowercase   							=	{}
		__lowercase   							=	HTTPError
		__lowercase   							=	{}

		# Under the mock environment we get a 500 error when trying to reach the tokenizer.
		with mock.patch("""requests.Session.request"""							,					return_value=_lowerCAmelCase      ) as mock_head:
			__lowercase   							=	cached_file(_lowerCAmelCase							,					"""conf"""							,					_raise_exceptions_for_connection_errors=_lowerCAmelCase      )
			self.assertIsNone(_lowerCAmelCase      )
			# This check we did call the fake head request
			mock_head.assert_called()







	def 			_a (	self   :				Optional[int]      )		->			str:
		"""simple docstring"""







		self.assertTrue(has_file("""hf-internal-testing/tiny-bert-pt-only"""							,					_lowerCAmelCase      )      )
		self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""							,					_lowerCAmelCase      )      )
		self.assertFalse(has_file("""hf-internal-testing/tiny-bert-pt-only"""							,					_lowerCAmelCase      )      )







	def 			_a (	self   :				int      )		->			Tuple:
		"""simple docstring"""







		self.assertIsNone(get_file_from_repo("""bert-base-cased"""							,					"""ahah.txt"""      )      )

		# The function raises if the repository does not exist.
		with self.assertRaisesRegex(_lowerCAmelCase							,					"""is not a valid model identifier"""      ):
			get_file_from_repo("""bert-base-case"""							,					_lowerCAmelCase      )

		# The function raises if the revision does not exist.
		with self.assertRaisesRegex(_lowerCAmelCase							,					"""is not a valid git identifier"""      ):
			get_file_from_repo("""bert-base-cased"""							,					_lowerCAmelCase							,					revision="""ahaha"""      )

		__lowercase   							=	get_file_from_repo("""bert-base-cased"""							,					_lowerCAmelCase      )
		# The name is the cached name which is not very easy to test, so instead we load the content.
		__lowercase   							=	json.loads(open(_lowerCAmelCase							,					"""r"""      ).read()      )
		self.assertEqual(config["""hidden_size"""]							,					768      )







	def 			_a (	self   :				Dict      )		->			Optional[int]:
		"""simple docstring"""







		with tempfile.TemporaryDirectory() as tmp_dir:
			__lowercase   							=	Path(_lowerCAmelCase      ) / """a.txt"""
			filename.touch()
			self.assertEqual(get_file_from_repo(_lowerCAmelCase							,					"""a.txt"""      )							,					str(_lowerCAmelCase      )      )

			self.assertIsNone(get_file_from_repo(_lowerCAmelCase							,					"""b.txt"""      )      )








def 		lowerCAmelCase  (	UpperCamelCase__   :   list       ,			UpperCamelCase__   :   list       ,			UpperCamelCase__   :   int					)      ->    list:

						"""simple docstring"""





						__SCREAMING_SNAKE_CASE:				Tuple           =   len(UpperCamelCase__					)
						__SCREAMING_SNAKE_CASE:				Optional[int]           =   [[0] * n for i in range(UpperCamelCase__					)]
						for i in range(UpperCamelCase__					):
												__SCREAMING_SNAKE_CASE:				str           =   y_points[i]

						for i in range(2       ,			UpperCamelCase__					):
												for j in range(UpperCamelCase__       ,			UpperCamelCase__					):
																		__SCREAMING_SNAKE_CASE:				Optional[int]           =   (
																		    (xa - x_points[j - i + 1]) * q[j][i - 1]
																		    - (xa - x_points[j]) * q[j - 1][i - 1]
																		) / (x_points[j] - x_points[j - i + 1])

						return [q[n - 1][n - 1], q]


if __name__ == "__main__":
							import doctest

							doctest.testmod()



