


"""simple docstring"""







import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
 import torch

if is_vision_available():
 from PIL import Image

 from transformers import ChineseCLIPImageProcessor






class 						_A  (  unittest.TestCase  ):







    """simple docstring"""




    def __init__(					self				:    Optional[Any]				, __UpperCAmelCase				:    Dict				, __UpperCAmelCase				:    str=7				, __UpperCAmelCase				:    str=3				, __UpperCAmelCase				:    Tuple=18				, __UpperCAmelCase				:    Dict=30				, __UpperCAmelCase				:    Any=400				, __UpperCAmelCase				:    List[Any]=True				, __UpperCAmelCase				:    Dict=None				, __UpperCAmelCase				:    Any=True				, __UpperCAmelCase				:    Optional[Any]=None				, __UpperCAmelCase				:    Tuple=True				, __UpperCAmelCase				:    Tuple=[0.48_145_466, 0.4_578_275, 0.40_821_073]				, __UpperCAmelCase				:    Optional[Any]=[0.26_862_954, 0.26_130_258, 0.27_577_711]				, __UpperCAmelCase				:    Union[str, Any]=True				, ):
      a						:		int      							=			size if size is not None else {"height": 224, "width": 224}
      a						:		List[str]      							=			crop_size if crop_size is not None else {"height": 18, "width": 18}
      a						:		List[Any]      							=			parent
      a						:		Any      							=			batch_size
      a						:		str      							=			num_channels
      a						:		Optional[int]      							=			image_size
      a						:		Tuple      							=			min_resolution
      a						:		str      							=			max_resolution
      a						:		Dict      							=			do_resize
      a						:		Any      							=			size
      a						:		Dict      							=			do_center_crop
      a						:		List[str]      							=			crop_size
      a						:		str      							=			do_normalize
      a						:		Optional[int]      							=			image_mean
      a						:		Tuple      							=			image_std
      a						:		Any      							=			do_convert_rgb






    def 	__snake_case							(					self				:    Union[str, Any]):
      return {
          "do_resize": self.do_resize,
          "size": self.size,
          "do_center_crop": self.do_center_crop,
          "crop_size": self.crop_size,
          "do_normalize": self.do_normalize,
          "image_mean": self.image_mean,
          "image_std": self.image_std,
          "do_convert_rgb": self.do_convert_rgb,
      }






    def 	__snake_case							(					self				:    Dict				, __UpperCAmelCase				:    Optional[Any]=False				, __UpperCAmelCase				:    List[Any]=False				, __UpperCAmelCase				:    Any=False):
      assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

      if equal_resolution:
        a						:		str      							=			[]
        for i in range(self.batch_size):
          image_inputs.append(
              np.random.randint(
                  255				, size=(self.num_channels, self.max_resolution, self.max_resolution)				, dtype=np.uinta))
      else:
        a						:		Tuple      							=			[]
        for i in range(self.batch_size):
          a							,		a						:		List[str]      							=			np.random.choice(np.arange(self.min_resolution				, self.max_resolution)				, 2)
          image_inputs.append(np.random.randint(255				, size=(self.num_channels, width, height)				, dtype=np.uinta))

      if not numpify and not torchify:
        # PIL expects the channel dimension as last dimension
        a						:		List[Any]      							=			[Image.fromarray(np.moveaxis(__UpperCAmelCase				, 0				, -1)) for x in image_inputs]

      if torchify:
        a						:		Optional[Any]      							=			[torch.from_numpy(__UpperCAmelCase) for x in image_inputs]

      return image_inputs






@require_torch
@require_vision
class 						_A  (  _a    ,unittest.TestCase  ):







    """simple docstring"""




    UpperCAmelCase       :							Union[str, Any]	=       ChineseCLIPImageProcessor if is_vision_available() else None






    def 	__snake_case							(					self				:    List[str]):
      a						:		Dict      							=			ChineseCLIPImageProcessingTester(self				, do_center_crop=__UpperCAmelCase)






    @property
    def 	__snake_case							(					self				:    Union[str, Any]):
      return self.image_processor_tester.prepare_image_processor_dict()






    def 	__snake_case							(					self				:    str):
      a						:		Optional[int]      							=			self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_resize"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "size"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_center_crop"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "center_crop"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_normalize"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "image_mean"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "image_std"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_convert_rgb"))






    def 	__snake_case							(					self				:    Any):
      a						:		Union[str, Any]      							=			self.image_processing_class.from_dict(self.image_processor_dict)
      self.assertEqual(image_processor.size				, {"height": 224, "width": 224})
      self.assertEqual(image_processor.crop_size				, {"height": 18, "width": 18})

      a						:		int      							=			self.image_processing_class.from_dict(self.image_processor_dict				, size=42				, crop_size=84)
      self.assertEqual(image_processor.size				, {"shortest_edge": 42})
      self.assertEqual(image_processor.crop_size				, {"height": 84, "width": 84})






    def 	__snake_case							(					self				:    str):
      pass






    def 	__snake_case							(					self				:    Tuple):
      # Initialize image_processing
      a						:		Union[str, Any]      							=			self.image_processing_class(**self.image_processor_dict)
      # create random PIL images
      a						:		Any      							=			self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCAmelCase)
      for image in image_inputs:
        self.assertIsInstance(__UpperCAmelCase				, Image.Image)

      # Test not batched input
      a						:		str      							=			image_processing(image_inputs[0]				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )

      # Test batched
      a						:		Dict      							=			image_processing(__UpperCAmelCase				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )






    def 	__snake_case							(					self				:    List[Any]):
      # Initialize image_processing
      a						:		str      							=			self.image_processing_class(**self.image_processor_dict)
      # create random numpy tensors
      a						:		Any      							=			self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCAmelCase				, numpify=__UpperCAmelCase)
      for image in image_inputs:
        self.assertIsInstance(__UpperCAmelCase				, np.ndarray)

      # Test not batched input
      a						:		Optional[Any]      							=			image_processing(image_inputs[0]				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )

      # Test batched
      a						:		Union[str, Any]      							=			image_processing(__UpperCAmelCase				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )






    def 	__snake_case							(					self				:    List[str]):
      # Initialize image_processing
      a						:		str      							=			self.image_processing_class(**self.image_processor_dict)
      # create random PyTorch tensors
      a						:		Tuple      							=			self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCAmelCase				, torchify=__UpperCAmelCase)
      for image in image_inputs:
        self.assertIsInstance(__UpperCAmelCase				, torch.Tensor)

      # Test not batched input
      a						:		List[str]      							=			image_processing(image_inputs[0]				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              1,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )

      # Test batched
      a						:		str      							=			image_processing(__UpperCAmelCase				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              self.image_processor_tester.batch_size,
              self.image_processor_tester.num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )






@require_torch
@require_vision
class 						_A  (  _a    ,unittest.TestCase  ):







    """simple docstring"""




    UpperCAmelCase       :							Optional[int]	=       ChineseCLIPImageProcessor if is_vision_available() else None






    def 	__snake_case							(					self				:    Union[str, Any]):
      a						:		str      							=			ChineseCLIPImageProcessingTester(self				, num_channels=4				, do_center_crop=__UpperCAmelCase)
      a						:		Dict      							=			3






    @property
    def 	__snake_case							(					self				:    Optional[Any]):
      return self.image_processor_tester.prepare_image_processor_dict()






    def 	__snake_case							(					self				:    Optional[int]):
      a						:		Tuple      							=			self.image_processing_class(**self.image_processor_dict)
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_resize"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "size"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_center_crop"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "center_crop"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_normalize"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "image_mean"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "image_std"))
      self.assertTrue(hasattr(__UpperCAmelCase				, "do_convert_rgb"))






    def 	__snake_case							(					self				:    Any):
      pass






    def 	__snake_case							(					self				:    Union[str, Any]):
      # Initialize image_processing
      a						:		List[Any]      							=			self.image_processing_class(**self.image_processor_dict)
      # create random PIL images
      a						:		str      							=			self.image_processor_tester.prepare_inputs(equal_resolution=__UpperCAmelCase)
      for image in image_inputs:
        self.assertIsInstance(__UpperCAmelCase				, Image.Image)

      # Test not batched input
      a						:		Tuple      							=			image_processing(image_inputs[0]				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              1,
              self.expected_encoded_image_num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )

      # Test batched
      a						:		Optional[Any]      							=			image_processing(__UpperCAmelCase				, return_tensors="pt").pixel_values
      self.assertEqual(
          encoded_images.shape				, (
              self.image_processor_tester.batch_size,
              self.expected_encoded_image_num_channels,
              self.image_processor_tester.crop_size["height"],
              self.image_processor_tester.crop_size["width"],
          )				, )










"""simple docstring"""







from __future__ import annotations






class 						_A  :







    """simple docstring"""




    def __init__(					self				:    List[str]				, __UpperCAmelCase				:    int = 0):
      a						:		Tuple      							=			key






    def 	__snake_case							(					self				:    List[str]				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    int):
      assert isinstance(__UpperCAmelCase				, __UpperCAmelCase) and isinstance(__UpperCAmelCase				, __UpperCAmelCase)

      a						:		Dict      							=			key or self.__key or 1

      # make sure key is an appropriate size
      key %= 255

      return [chr(ord(__UpperCAmelCase) ^ key) for ch in content]






    def 	__snake_case							(					self				:    int				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    int):
      assert isinstance(__UpperCAmelCase				, __UpperCAmelCase) and isinstance(__UpperCAmelCase				, __UpperCAmelCase)

      a						:		Optional[Any]      							=			key or self.__key or 1

      # make sure key is an appropriate size
      key %= 255

      return [chr(ord(__UpperCAmelCase) ^ key) for ch in content]






    def 	__snake_case							(					self				:    Optional[Any]				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    int = 0):
      assert isinstance(__UpperCAmelCase				, __UpperCAmelCase) and isinstance(__UpperCAmelCase				, __UpperCAmelCase)

      a						:		List[Any]      							=			key or self.__key or 1

      # make sure key can be any size
      while key > 255:
        key -= 255

      # This will be returned
      a						:		Any      							=			""

      for ch in content:
        ans += chr(ord(__UpperCAmelCase) ^ key)

      return ans






    def 	__snake_case							(					self				:    List[str]				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    int = 0):
      assert isinstance(__UpperCAmelCase				, __UpperCAmelCase) and isinstance(__UpperCAmelCase				, __UpperCAmelCase)

      a						:		Dict      							=			key or self.__key or 1

      # make sure key can be any size
      while key > 255:
        key -= 255

      # This will be returned
      a						:		str      							=			""

      for ch in content:
        ans += chr(ord(__UpperCAmelCase) ^ key)

      return ans






    def 	__snake_case							(					self				:    int				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    int = 0):
      assert isinstance(__UpperCAmelCase				, __UpperCAmelCase) and isinstance(__UpperCAmelCase				, __UpperCAmelCase)

      try:
        with open(__UpperCAmelCase) as fin, open("encrypt.out"				, "w+") as fout:
          # actual encrypt-process
          for line in fin:
            fout.write(self.encrypt_string(__UpperCAmelCase				, __UpperCAmelCase))

      except OSError:
        return False

      return True






    def 	__snake_case							(					self				:    Any				, __UpperCAmelCase				:    str				, __UpperCAmelCase				:    int):
      assert isinstance(__UpperCAmelCase				, __UpperCAmelCase) and isinstance(__UpperCAmelCase				, __UpperCAmelCase)

      try:
        with open(__UpperCAmelCase) as fin, open("decrypt.out"				, "w+") as fout:
          # actual encrypt-process
          for line in fin:
            fout.write(self.decrypt_string(__UpperCAmelCase				, __UpperCAmelCase))

      except OSError:
        return False

      return True


# Tests
# crypt = XORCipher()
# key = 67

# # test encrypt
# print(crypt.encrypt("hallo welt",key))
# # test decrypt
# print(crypt.decrypt(crypt.encrypt("hallo welt",key), key))

# # test encrypt_string
# print(crypt.encrypt_string("hallo welt",key))

# # test decrypt_string
# print(crypt.decrypt_string(crypt.encrypt_string("hallo welt",key),key))

# if (crypt.encrypt_file("test.txt",key)):
#       print("encrypt successful")
# else:
#       print("encrypt unsuccessful")

# if (crypt.decrypt_file("encrypt.out",key)):
#       print("decrypt successful")
# else:
#       print("decrypt unsuccessful")






