
'''simple docstring'''





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


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






class 							UpperCAmelCase_							(    unittest.TestCase				):

 '''simple docstring'''







 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




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

   # File is cached at the same place the second time.
   UpperCAmelCase__      :					Optional[Any]				       =							cached_file(snake_case__			,				snake_case__ )
   self.assertEqual(snake_case__			,				snake_case__ )

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







 def 							UpperCamelCase  (					self		:  Dict ):



   '''simple docstring'''




   with self.assertRaisesRegex(snake_case__			,				"is not a valid model identifier" ):
     UpperCAmelCase__      :					List[str]				       =							cached_file("tiny-random-bert"			,				snake_case__ )

   with self.assertRaisesRegex(snake_case__			,				"is not a valid git identifier" ):
     UpperCAmelCase__      :					Optional[int]				       =							cached_file(snake_case__			,				snake_case__			,				revision="aaaa" )

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







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




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

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

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

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

   UpperCAmelCase__      :					Any				       =							mock.Mock()
   UpperCAmelCase__      :					Union[str, Any]				       =							5_00
   UpperCAmelCase__      :					List[Any]				       =							{}
   UpperCAmelCase__      :					Tuple				       =							HTTPError
   UpperCAmelCase__      :					Dict				       =							{}

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







 def 							UpperCamelCase  (					self		:  Union[str, Any] ):



   '''simple docstring'''




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







 def 							UpperCamelCase  (					self		:  List[str] ):



   '''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(snake_case__			,				"is not a valid model identifier" ):
     get_file_from_repo("bert-base-case"			,				snake_case__ )

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

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







 def 							UpperCamelCase  (					self		:  Any ):



   '''simple docstring'''




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

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





'''simple docstring'''





def snake_case_     (					lowercase__ = "The quick brown fox jumps over the lazy dog"   ,       ):
  UpperCAmelCase__      :					Dict				       =							set()

  # Replace all the whitespace in our sentence
  UpperCAmelCase__      :					str				       =							input_str.replace(" "   ,       ""		)
  for alpha in input_str:
    if "a" <= alpha.lower() <= "z":
      frequency.add(alpha.lower()		)
  return len(lowercase__		) == 2_6







def snake_case_     (					lowercase__ = "The quick brown fox jumps over the lazy dog"   ,       ):
  UpperCAmelCase__      :					str				       =							[False] * 2_6
  for char in input_str:
    if char.islower():
      UpperCAmelCase__      :					List[Any]				       =							True
    elif char.isupper():
      UpperCAmelCase__      :					List[Any]				       =							True
  return all(lowercase__		)







def snake_case_     (					lowercase__ = "The quick brown fox jumps over the lazy dog"   ,       ):
  return len({char for char in input_str.lower() if char.isalpha()}		) == 2_6







def snake_case_     (					):
  from timeit import timeit

  UpperCAmelCase__      :					Union[str, Any]				       =							"from __main__ import is_pangram, is_pangram_faster, is_pangram_fastest"
  print(timeit("is_pangram()"   ,       setup=lowercase__		)		)
  print(timeit("is_pangram_faster()"   ,       setup=lowercase__		)		)
  print(timeit("is_pangram_fastest()"   ,       setup=lowercase__		)		)
  # 5.348480500048026, 2.6477354579837993, 1.8470395830227062
  # 5.036091582966037, 2.644472333951853,  1.8869528750656173


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      benchmark()



