




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


lowerCamelCase	:	List[str]				=   'hf-internal-testing/tiny-random-bert'
lowerCamelCase	:	Tuple				=   os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
lowerCamelCase	:	Union[str, Any]				=   '9b8c223d42b2188cb49d29af482996f9d0f3e5a6'




class 						__lowercase		(unittest.TestCase							):




      """simple docstring"""





      def      UpperCAmelCase				(				self     )		->    Optional[Any]:
         snake_case				:    int           =	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				:    Any           =	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				:    Tuple           =	cached_file(A    ,				A     )
         self.assertEqual(A    ,				A     )

         # Using a specific revision to test the full commit hash.
         snake_case				:    Union[str, Any]           =	cached_file(A    ,				A    ,				revision="""9b8c223"""     )
         self.assertEqual(A    ,				os.path.join(A    ,				"""snapshots"""    ,				A    ,				A     )     )

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

         with self.assertRaisesRegex(A    ,				"""is not a valid git identifier"""     ):
            snake_case				:    List[str]           =	cached_file(A    ,				A    ,				revision="""aaaa"""     )

         with self.assertRaisesRegex(A    ,				"""does not appear to have a file named"""     ):
            snake_case				:    Union[str, Any]           =	cached_file(A    ,				"""conf"""     )

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

         with open(os.path.join(A    ,				"""refs"""    ,				"""main"""     )     ) as f:
            snake_case				:    List[str]           =	f.read()
         self.assertTrue(os.path.isfile(os.path.join(A    ,				""".no_exist"""    ,				A    ,				"""conf"""     )     )     )

         snake_case				:    Optional[int]           =	cached_file(A    ,				"""conf"""    ,				_raise_exceptions_for_missing_entries=A     )
         self.assertIsNone(A     )

         snake_case				:    List[Any]           =	cached_file(A    ,				"""conf"""    ,				local_files_only=A    ,				_raise_exceptions_for_missing_entries=A     )
         self.assertIsNone(A     )

         snake_case				:    Optional[Any]           =	mock.Mock()
         snake_case				:    int           =	5_0_0
         snake_case				:    int           =	{}
         snake_case				:    Any           =	HTTPError
         snake_case				:    List[Any]           =	{}

         # 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				:    Tuple           =	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     )		->    int:
         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     )		->    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				:    Any           =	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				:    List[Any]           =	json.loads(open(A    ,				"""r"""     ).read()     )
         self.assertEqual(config["""hidden_size"""]    ,				7_6_8     )

      def      UpperCAmelCase				(				self     )		->    int:
         with tempfile.TemporaryDirectory() as tmp_dir:
            snake_case				:    Union[str, Any]           =	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 math import pow




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase						,lowercase						,lowercase						,lowercase						,)  ->       tuple[int, int]:
   if current_sum == needed_sum:
      # If the sum of the powers is equal to needed_sum, then we have a solution.
      solutions_count += 1
      return current_sum, solutions_count

   snake_case				:    Union[str, Any]           =	int(pow(lowercase						,lowercase					)					)
   if current_sum + i_to_n <= needed_sum:
      # If the sum of the powers is less than needed_sum, then continue adding powers.
      current_sum += i_to_n
      snake_case     , snake_case				:    List[Any]           =	backtrack(
          lowercase						,lowercase						,current_number + 1						,lowercase						,lowercase					)
      current_sum -= i_to_n
   if i_to_n < needed_sum:
      # If the power of i is less than needed_sum, then try with the next power.
      snake_case     , snake_case				:    str           =	backtrack(
          lowercase						,lowercase						,current_number + 1						,lowercase						,lowercase					)
   return current_sum, solutions_count




def 						SCREAMING_SNAKE_CASE__ (   lowercase						,lowercase					)  ->       int:
   if not (1 <= needed_sum <= 1000 and 2 <= power <= 10):
      raise ValueError(
          """Invalid input\n"""
          """needed_sum must be between 1 and 1000, power between 2 and 10."""					)

   return backtrack(lowercase						,lowercase						,1						,0						,0					)[1]  # Return the solutions_count


if __name__ == "__main__":
       import doctest

       doctest.testmod()





