




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__  		=		"""hf-internal-testing/tiny-random-bert"""
lowercase__  		=		os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
lowercase__  		=		"""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""






class lowerCAmelCase__			(  unittest.TestCase		):






			'''simple docstring'''



			def       A_					(						self						):
							_lowerCamelCase  :    Tuple					=					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:
											_lowerCamelCase  :    Optional[int]					=					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.
							_lowerCamelCase  :    int					=					cached_file(_lowerCamelCase						,   _lowerCamelCase						)
							self.assertEqual(_lowerCamelCase						,   _lowerCamelCase						)

							# Using a specific revision to test the full commit hash.
							_lowerCamelCase  :    Union[str, Any]					=					cached_file(_lowerCamelCase						,   _lowerCamelCase						,   revision='9b8c223'						)
							self.assertEqual(_lowerCamelCase						,   os.path.join(_lowerCamelCase						,   'snapshots'						,   _lowerCamelCase						,   _lowerCamelCase						)						)
			def       A_					(						self						):
							with self.assertRaisesRegex(_lowerCamelCase						,   'is not a valid model identifier'						):
											_lowerCamelCase  :    Optional[Any]					=					cached_file('tiny-random-bert'						,   _lowerCamelCase						)

							with self.assertRaisesRegex(_lowerCamelCase						,   'is not a valid git identifier'						):
											_lowerCamelCase  :    int					=					cached_file(_lowerCamelCase						,   _lowerCamelCase						,   revision='aaaa'						)

							with self.assertRaisesRegex(_lowerCamelCase						,   'does not appear to have a file named'						):
											_lowerCamelCase  :    List[Any]					=					cached_file(_lowerCamelCase						,   'conf'						)
			def       A_					(						self						):
							with self.assertRaisesRegex(_lowerCamelCase						,   'does not appear to have a file named'						):
											_lowerCamelCase  :    Tuple					=					cached_file(_lowerCamelCase						,   'conf'						)

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

							_lowerCamelCase  :    int					=					cached_file(_lowerCamelCase						,   'conf'						,   _raise_exceptions_for_missing_entries=_lowerCamelCase						)
							self.assertIsNone(_lowerCamelCase						)

							_lowerCamelCase  :    List[str]					=					cached_file(_lowerCamelCase						,   'conf'						,   local_files_only=_lowerCamelCase						,   _raise_exceptions_for_missing_entries=_lowerCamelCase						)
							self.assertIsNone(_lowerCamelCase						)

							_lowerCamelCase  :    str					=					mock.Mock()
							_lowerCamelCase  :    Optional[Any]					=					500
							_lowerCamelCase  :    Optional[int]					=					{}
							_lowerCamelCase  :    Optional[Any]					=					HTTPError
							_lowerCamelCase  :    Any					=					{}

							# 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:
											_lowerCamelCase  :    Any					=					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						):
							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						):
							# `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(_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'						)

							_lowerCamelCase  :    Tuple					=					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.
							_lowerCamelCase  :    str					=					json.loads(open(_lowerCamelCase						,   'r'						).read()						)
							self.assertEqual(config['hidden_size']						,   768						)






			def       A_					(						self						):
							with tempfile.TemporaryDirectory() as tmp_dir:
											_lowerCamelCase  :    int					=					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'						)						)





"""simple docstring"""






from __future__ import annotations

lowercase__  		=		10







def   _snake_case    (       lowercase__	):
				_lowerCamelCase  :    List[Any]					=					1
				_lowerCamelCase  :    Optional[int]					=					max(lowercase__	)
				while placement <= max_digit:
								# declare and initialize empty buckets
								_lowerCamelCase  :    list[list]					=					[[] for _ in range(lowercase__	)]
								# split list_of_ints between the buckets
								for i in list_of_ints:
												_lowerCamelCase  :    Dict					=					int((i / placement) % RADIX	)
												buckets[tmp].append(lowercase__	)
								# put each buckets' contents into list_of_ints
								_lowerCamelCase  :    str					=					0
								for b in range(lowercase__	):
												for i in buckets[b]:
																_lowerCamelCase  :    str					=					i
																a += 1
        # move to next
								placement *= RADIX
				return list_of_ints


if __name__ == "__main__":
			import doctest

			doctest.testmod()