





'''simple docstring'''





from __future__ import annotations

from PIL import Image

# Define glider example
__lowercase  :   Any						      =[
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
__lowercase  :   Dict						      =[[0, 1, 0], [0, 1, 0], [0, 1, 0]]




def     a__ (					lowercase__  ):






   '''simple docstring'''






   UpperCAmelCase_											=[]
   for i in range(len(SCREAMING_SNAKE_CASE_  )  ):
      UpperCAmelCase_											=[]
      for j in range(len(cells[i]  )  ):
         # Get the number of live neighbours
         UpperCAmelCase_											=0
         if i > 0 and j > 0:
            neighbour_count += cells[i - 1][j - 1]
         if i > 0:
            neighbour_count += cells[i - 1][j]
         if i > 0 and j < len(cells[i]  ) - 1:
            neighbour_count += cells[i - 1][j + 1]
         if j > 0:
            neighbour_count += cells[i][j - 1]
         if j < len(cells[i]  ) - 1:
            neighbour_count += cells[i][j + 1]
         if i < len(SCREAMING_SNAKE_CASE_  ) - 1 and j > 0:
            neighbour_count += cells[i + 1][j - 1]
         if i < len(SCREAMING_SNAKE_CASE_  ) - 1:
            neighbour_count += cells[i + 1][j]
         if i < len(SCREAMING_SNAKE_CASE_  ) - 1 and j < len(cells[i]  ) - 1:
            neighbour_count += cells[i + 1][j + 1]

         # Rules of the game of life (excerpt from Wikipedia):
         # 1. Any live cell with two or three live neighbours survives.
         # 2. Any dead cell with three live neighbours becomes a live cell.
         # 3. All other live cells die in the next generation.
         #    Similarly, all other dead cells stay dead.
         UpperCAmelCase_											=cells[i][j] == 1
         if (
             (alive and 2 <= neighbour_count <= 3)
             or not alive
             and neighbour_count == 3
         ):
            next_generation_row.append(1  )
         else:
            next_generation_row.append(0  )

      next_generation.append(SCREAMING_SNAKE_CASE_  )
   return next_generation




def     a__ (					lowercase__  ,    lowercase__  ):






   '''simple docstring'''






   UpperCAmelCase_											=[]
   for _ in range(SCREAMING_SNAKE_CASE_  ):
      # Create output image
      UpperCAmelCase_											=Image.new("RGB"  ,    (len(cells[0]  ), len(SCREAMING_SNAKE_CASE_  ))  )
      UpperCAmelCase_											=img.load()

      # Save cells to image
      for x in range(len(SCREAMING_SNAKE_CASE_  )  ):
         for y in range(len(cells[0]  )  ):
            UpperCAmelCase_											=2_5_5 - cells[y][x] * 2_5_5
            UpperCAmelCase_											=(colour, colour, colour)

        # Save image
      images.append(SCREAMING_SNAKE_CASE_  )
      UpperCAmelCase_											=new_generation(SCREAMING_SNAKE_CASE_  )
   return images


if __name__ == "__main__":
     __lowercase  :   Dict						      =generate_images(GLIDER, 16)
     images[0].save("""out.gif""", save_all=True, append_images=images[1:])











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  :   Any						      ="""hf-internal-testing/tiny-random-bert"""
__lowercase  :   List[str]						      =os.path.join(TRANSFORMERS_CACHE, """models--hf-internal-testing--tiny-random-bert""")
__lowercase  :   Dict						      ="""9b8c223d42b2188cb49d29af482996f9d0f3e5a6"""

class      A       (	unittest.TestCase						):



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    str:
         '''simple docstring'''

         UpperCAmelCase_											=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:
            UpperCAmelCase_											=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.
         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  _lowerCAmelCase       )
         self.assertEqual(_lowerCAmelCase		,  _lowerCAmelCase       )

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



      def 	lowerCAmelCase__    (	self:						Any       )						->    Optional[Any]:
         '''simple docstring'''

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

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

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



      def 	lowerCAmelCase__    (	self:						Tuple       )						->    Optional[Any]:
         '''simple docstring'''

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

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

         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"		,  _raise_exceptions_for_missing_entries=_lowerCAmelCase       )
         self.assertIsNone(_lowerCAmelCase       )

         UpperCAmelCase_											=cached_file(_lowerCAmelCase		,  "conf"		,  local_files_only=_lowerCAmelCase		,  _raise_exceptions_for_missing_entries=_lowerCAmelCase       )
         self.assertIsNone(_lowerCAmelCase       )

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

         # 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:
            UpperCAmelCase_											=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 	lowerCAmelCase__    (	self:						Any       )						->    Tuple:
         '''simple docstring'''

         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 	lowerCAmelCase__    (	self:						Any       )						->    Dict:
         '''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(_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"       )

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






      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Dict:
         '''simple docstring'''

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




