
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,
)


_A   =			"hf-internal-testing/tiny-random-bert"
_A   =			os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
_A   =			"9b8c223d42b2188cb49d29af482996f9d0f3e5a6"







class 					lowerCamelCase		(  unittest.TestCase  ):





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

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

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





     def 		UpperCAmelCase(self				:   Optional[Any]       )						->						Optional[Any]:
         with self.assertRaisesRegex(_A    ,   "is not a valid model identifier"       ):
             snake_case								= cached_file("tiny-random-bert"    ,   _A       )

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

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





     def 		UpperCAmelCase(self				:   Optional[Any]       )						->						Tuple:
         with self.assertRaisesRegex(_A    ,   "does not appear to have a file named"       ):
             snake_case								= cached_file(_A    ,   "conf"       )

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

         snake_case								= cached_file(_A    ,   "conf"    ,   _raise_exceptions_for_missing_entries=_A       )
         self.assertIsNone(_A       )

         snake_case								= cached_file(_A    ,   "conf"    ,   local_files_only=_A    ,   _raise_exceptions_for_missing_entries=_A       )
         self.assertIsNone(_A       )

         snake_case								= mock.Mock()
         snake_case								= 5_0_0
         snake_case								= {}
         snake_case								= HTTPError
         snake_case								= {}

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





     def 		UpperCAmelCase(self				:   str       )						->						Union[str, Any]:
         self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only"    ,   _A       )       )
         self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"    ,   _A       )       )
         self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only"    ,   _A       )       )





     def 		UpperCAmelCase(self				:   Optional[Any]       )						->						List[Any]:
         # `get_file_from_repo` returns None if the file does not exist
         self.assertIsNone(get_file_from_repo("bert-base-cased"    ,   "ahah.txt"       )       )

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

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

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





     def 		UpperCAmelCase(self				:   Any       )						->						Optional[int]:
         with tempfile.TemporaryDirectory() as tmp_dir:
             snake_case								= Path(_A       ) / "a.txt"
             filename.touch()
             self.assertEqual(get_file_from_repo(_A    ,   "a.txt"       )    ,   str(_A       )       )

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



from __future__ import annotations



def 						lowercase_      (       A__			,		A__			,		A__		)							->							int | float:
    """simple docstring"""

    if len(A__		) == 0:
        raise ValueError("find_max() arg is an empty sequence"		)
    if (
        left >= len(A__		)
        or left < -len(A__		)
        or right >= len(A__		)
        or right < -len(A__		)
    ):
        raise IndexError("list index out of range"		)
    if left == right:
        return nums[left]
    snake_case								= (left + right) >> 1  # the middle
    snake_case								= find_max(A__			,		A__			,		A__		)  # find max in range[left, mid]
    snake_case								= find_max(A__			,		mid + 1			,		A__		)  # find max in range[mid + 1, right]

    return left_max if left_max >= right_max else right_max


if __name__ == "__main__":
   import doctest

   doctest.testmod(verbose=True)

