




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


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






class        __magic_name__				(			unittest.TestCase							):






				'''simple docstring'''





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							lowerCamelCase      =   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:
										lowerCamelCase      =   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.
							lowerCamelCase      =   cached_file(_a						,					_a					)
							self.assertEqual(_a						,					_a					)

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





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							with self.assertRaisesRegex(_a						,					"""is not a valid model identifier"""					):
										lowerCamelCase      =   cached_file("""tiny-random-bert"""						,					_a					)

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

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





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							with self.assertRaisesRegex(_a						,					"""does not appear to have a file named"""					):
										lowerCamelCase      =   cached_file(_a						,					"""conf"""					)

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

							lowerCamelCase      =   cached_file(_a						,					"""conf"""						,					_raise_exceptions_for_missing_entries=_a					)
							self.assertIsNone(_a					)

							lowerCamelCase      =   cached_file(_a						,					"""conf"""						,					local_files_only=_a						,					_raise_exceptions_for_missing_entries=_a					)
							self.assertIsNone(_a					)

							lowerCamelCase      =   mock.Mock()
							lowerCamelCase      =   500
							lowerCamelCase      =   {}
							lowerCamelCase      =   HTTPError
							lowerCamelCase      =   {}

							# 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:
										lowerCamelCase      =   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   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							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   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							# `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"""					)

							lowerCamelCase      =   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.
							lowerCamelCase      =   json.loads(open(_a						,					"""r"""					).read()					)
							self.assertEqual(config["""hidden_size"""]						,					768					)





				def   _lowerCAmelCase							(			self					):





							"""simple docstring"""
							with tempfile.TemporaryDirectory() as tmp_dir:
										lowerCamelCase      =   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"""					)					)









"""simple docstring"""




import random



def        a__					(							snake_case__			,					snake_case__			,					snake_case__ = False							)		->   dict:
			lowerCamelCase      =   {i: [] for i in range(snake_case__							)}

			# if probability is greater or equal than 1, then generate a complete graph
			if probability >= 1:
						return complete_graph(snake_case__							)
			# if probability is lower or equal than 0, then return a graph without edges
			if probability <= 0:
						return graph

			# for each couple of nodes, add an edge from u to v
			# if the number randomly generated is greater than probability probability
			for i in range(snake_case__							):
						for j in range(i + 1			,					snake_case__							):
									if random.random() < probability:
												graph[i].append(snake_case__							)
												if not directed:
															# if the graph is undirected, add an edge in from j to i, either
															graph[j].append(snake_case__							)
			return graph



def        a__					(							snake_case__							)		->   dict:
			return {
			    i: [j for j in range(snake_case__							) if i != j] for i in range(snake_case__							)
			}


if __name__ == "__main__":
					import doctest

					doctest.testmod()



