



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


_lowercase		:				int      		="hf-internal-testing/tiny-random-bert"
_lowercase		:				List[Any]      		=os.path.join(TRANSFORMERS_CACHE, "models--hf-internal-testing--tiny-random-bert")
_lowercase		:				Any      		="9b8c223d42b2188cb49d29af482996f9d0f3e5a6"

class        snake_case__    (unittest.TestCase		):







      """simple docstring"""




      def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Union[str, Any]:
         """simple docstring"""




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

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

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

      def 							SCREAMING_SNAKE_CASE__(       self  ) ->				Tuple:
         """simple docstring"""




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

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

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

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




         with self.assertRaisesRegex(__lowercase ,       """does not appear to have a file named"""  ):
            a__       :						Optional[Any]							=  cached_file(__lowercase ,       """conf"""  )

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

         a__       :						Optional[Any]							=  cached_file(__lowercase ,       """conf""" ,       _raise_exceptions_for_missing_entries=__lowercase  )
         self.assertIsNone(__lowercase  )

         a__       :						Optional[Any]							=  cached_file(__lowercase ,       """conf""" ,       local_files_only=__lowercase ,       _raise_exceptions_for_missing_entries=__lowercase  )
         self.assertIsNone(__lowercase  )

         a__       :						Optional[Any]							=  mock.Mock()
         a__       :						str							=  5_0_0
         a__       :						Optional[int]							=  {}
         a__       :						Union[str, Any]							=  HTTPError
         a__       :						Tuple							=  {}

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

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




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

      def 							SCREAMING_SNAKE_CASE__(       self  ) ->				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(__lowercase ,       """is not a valid model identifier"""  ):
            get_file_from_repo("""bert-base-case""" ,       __lowercase  )

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

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

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




         with tempfile.TemporaryDirectory() as tmp_dir:
            a__       :						List[Any]							=  Path(__lowercase  ) / """a.txt"""
            filename.touch()
            self.assertEqual(get_file_from_repo(__lowercase ,       """a.txt"""  ) ,       str(__lowercase  )  )

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









def      lowerCAmelCase_					(  _lowercase    :       int)							->	int:







			"""simple docstring"""

			if not isinstance(_lowercase						,       _lowercase):
						raise TypeError("""only integers accepted as input""")
			else:
						a__       :						Any							=  str(abs(_lowercase))
						a__       :						str							=  [list(_lowercase) for char in range(len(_lowercase))]
						for index in range(len(_lowercase)):
									num_transpositions[index].pop(_lowercase)
						return max(
						    int("""""".join(list(_lowercase))) for transposition in num_transpositions)


if __name__ == "__main__":
		__import__("doctest").testmod()




