




'''simple docstring'''






import json
import os
import pickle
import subprocess
from hashlib import mda
from pathlib import Path
from tempfile import gettempdir
from textwrap import dedent
from types import CodeType, FunctionType
from unittest import TestCase
from unittest.mock import patch

import pytest
from multiprocess import Pool
from packaging import version

import datasets
from datasets.fingerprint import Hasher, fingerprint_transform
from datasets.table import InMemoryTable

from .utils import (
    require_regex,
    require_spacy,
    require_spacy_model,
    require_tiktoken,
    require_torch,
    require_transformers,
)






class 					lowercase_			:





     def __init__(    self    :				Union[str, Any]    ,       __lowerCamelCase    :				Dict ):
           snake_case__					:      Tuple  			=						foo





     def __call__(    self    :				int ):
           return self.foo






class 					lowercase_			(	datasets.Dataset       ):





     @fingerprint_transform(inplace=__lowerCamelCase )
     def  _lowerCAmelCase			(    self    :				Optional[Any]    ,       __lowerCamelCase    :				Tuple    ,       *__lowerCamelCase    :				Optional[Any]    ,       **__lowerCamelCase    :				Optional[int] ):
           return DatasetChild(self.data    ,       fingerprint=__lowerCamelCase )





     @fingerprint_transform(inplace=__lowerCamelCase )
     def  _lowerCAmelCase			(    self    :				List[Any]    ,       __lowerCamelCase    :				List[str]    ,       *__lowerCamelCase    :				Any    ,       **__lowerCamelCase    :				Optional[int] ):
           return DatasetChild(self.data    ,       fingerprint=__lowerCamelCase )






class 					lowercase_			:





     def __init__(    self    :				List[Any]    ,       __lowerCamelCase    :				Tuple ):
           snake_case__					:      str  			=						callable





     def __call__(    self    :				Any    ,       *__lowerCamelCase    :				List[str]    ,       **__lowerCamelCase    :				Tuple ):
           if self.callable is not None:
                 return self.callable(*__lowerCamelCase    ,       **__lowerCamelCase )





     def __getstate__(    self    :				Optional[Any] ):
           raise pickle.PicklingError()






class 					lowercase_			(	lowerCAmelCase_       ):





     @require_transformers
     @pytest.mark.integration
     def  _lowerCAmelCase			(    self    :				List[str] ):
           from transformers import AutoTokenizer

           def encode(__lowerCamelCase    :				List[Any] ):
                 return tokenizer(__lowerCamelCase )

           # TODO: add hash consistency tests across sessions
           snake_case__					:      Optional[int]  			=						AutoTokenizer.from_pretrained('bert-base-uncased' )
           snake_case__					:      int  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      int  			=						mda(datasets.utils.py_utils.dumps(lambda __lowerCamelCase : tokenizer(__lowerCamelCase ) ) ).hexdigest()
           snake_case__					:      List[Any]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      List[str]  			=						AutoTokenizer.from_pretrained('bert-base-cased' )
           snake_case__					:      Dict  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Union[str, Any]  			=						mda(datasets.utils.py_utils.dumps(lambda __lowerCamelCase : tokenizer(__lowerCamelCase ) ) ).hexdigest()
           snake_case__					:      str  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Dict  			=						AutoTokenizer.from_pretrained('bert-base-uncased' )
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Tuple  			=						mda(datasets.utils.py_utils.dumps(lambda __lowerCamelCase : tokenizer(__lowerCamelCase ) ) ).hexdigest()
           snake_case__					:      str  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     @require_transformers
     @pytest.mark.integration
     def  _lowerCAmelCase			(    self    :				Any ):
           from transformers import AutoTokenizer

           snake_case__					:      Optional[Any]  			=						AutoTokenizer.from_pretrained('gpt2' )
           snake_case__					:      Tuple  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           tokenizer('Hello world !' )  # call once to change the tokenizer's cache
           snake_case__					:      Tuple  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )





     @require_regex
     def  _lowerCAmelCase			(    self    :				Dict ):
           import regex

           snake_case__					:      int  			=						regex.Regex('foo' )
           snake_case__					:      str  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Optional[Any]  			=						regex.Regex('bar' )
           snake_case__					:      int  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      str  			=						regex.Regex('foo' )
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )






class 					lowercase_			(	lowerCAmelCase_       ):





     def  _lowerCAmelCase			(    self    :				Union[str, Any] ):
           def func():
                 return foo

           snake_case__					:      Union[str, Any]  			=						[0]
           snake_case__					:      Optional[int]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      List[str]  			=						[1]
           snake_case__					:      List[str]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      int  			=						[0]
           snake_case__					:      List[Any]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Tuple ):
           def func():
                 pass

           snake_case__					:      Union[str, Any]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()

           def func():
                 pass

           snake_case__					:      List[str]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				List[Any] ):
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(Foo([0] ) ) ).hexdigest()
           snake_case__					:      List[str]  			=						mda(datasets.utils.py_utils.dumps(Foo([1] ) ) ).hexdigest()
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(Foo([0] ) ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				List[str] ):
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(Foo([0] ).__call__ ) ).hexdigest()
           snake_case__					:      Optional[Any]  			=						mda(datasets.utils.py_utils.dumps(Foo([1] ).__call__ ) ).hexdigest()
           snake_case__					:      int  			=						mda(datasets.utils.py_utils.dumps(Foo([0] ).__call__ ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Tuple ):
           snake_case__					:      List[Any]  			=						(
               'co_argcount',
               'co_kwonlyargcount',
               'co_nlocals',
               'co_stacksize',
               'co_flags',
               'co_code',
               'co_consts',
               'co_names',
               'co_varnames',
               'co_filename',
               'co_name',
               'co_firstlineno',
               'co_lnotab',
               'co_freevars',
               'co_cellvars',
           )

           def _create_code_pyaa(*__lowerCamelCase    :				List[str] ):
                 if hasattr(args[-3]    ,       'encode' ):
                       snake_case__					:      str  			=						list(__lowerCamelCase )
                       snake_case__					:      Optional[int]  			=						args[-3].encode()  # co_lnotab
                       snake_case__					:      Optional[int]  			=						args[-10].encode()  # co_code
                 if hasattr(__lowerCamelCase    ,       'co_posonlyargcount' ):
                       if len(__lowerCamelCase ) == 16:
                             return CodeType(*__lowerCamelCase )
                       elif len(__lowerCamelCase ) == 15:
                             return CodeType(args[0]    ,       0    ,       *args[1:] )
                       return CodeType(args[0]    ,       0    ,       0    ,       *args[1:] )
                 elif hasattr(__lowerCamelCase    ,       'co_kwonlyargcount' ):
                       if len(__lowerCamelCase ) == 16:
                             return CodeType(args[0]    ,       *args[2:] )
                       elif len(__lowerCamelCase ) == 15:
                             return CodeType(*__lowerCamelCase )
                       return CodeType(args[0]    ,       0    ,       *args[1:] )
                 if len(__lowerCamelCase ) == 16:
                       return CodeType(args[0]    ,       *args[3:] )
                 elif len(__lowerCamelCase ) == 15:
                       return CodeType(args[0]    ,       *args[2:] )
                 return CodeType(*__lowerCamelCase )

           def create_ipython_func(__lowerCamelCase    :				int    ,       __lowerCamelCase    :				Tuple ):
                 def func():
                       return returned_obj

                 snake_case__					:      Union[str, Any]  			=						func.__code__
                 # Use _create_code from dill in order to make it work for different python versions
                 if datasets.config.PY_VERSION < version.parse('3.8' ):
                       snake_case__					:      Optional[int]  			=						_create_code_pyaa(
                           *[getattr(__lowerCamelCase    ,       __lowerCamelCase ) if k != 'co_filename' else co_filename for k in code_args_pyaa] )
                 else:
                       snake_case__					:      Optional[int]  			=						code.replace(co_filename=__lowerCamelCase )
                 return FunctionType(__lowerCamelCase    ,       func.__globals__    ,       func.__name__    ,       func.__defaults__    ,       func.__closure__ )

           snake_case__			,       snake_case__					:      str  			=						'<ipython-input-2-e0383a102aae>', [0]
           snake_case__					:      Tuple  			=						mda(datasets.utils.py_utils.dumps(create_ipython_func(__lowerCamelCase    ,       __lowerCamelCase ) ) ).hexdigest()
           snake_case__			,       snake_case__					:      Union[str, Any]  			=						'<ipython-input-2-e0383a102aae>', [1]
           snake_case__					:      str  			=						mda(datasets.utils.py_utils.dumps(create_ipython_func(__lowerCamelCase    ,       __lowerCamelCase ) ) ).hexdigest()
           snake_case__			,       snake_case__					:      Tuple  			=						'<ipython-input-5-713f6613acf3>', [0]
           snake_case__					:      Optional[Any]  			=						mda(datasets.utils.py_utils.dumps(create_ipython_func(__lowerCamelCase    ,       __lowerCamelCase ) ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )

           snake_case__			,       snake_case__					:      Tuple  			=						os.path.join(gettempdir()    ,       'ipykernel_12345'    ,       '321456789.py' ), [0]
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(create_ipython_func(__lowerCamelCase    ,       __lowerCamelCase ) ) ).hexdigest()
           snake_case__			,       snake_case__					:      Tuple  			=						os.path.join(gettempdir()    ,       'ipykernel_12345'    ,       '321456789.py' ), [1]
           snake_case__					:      Optional[int]  			=						mda(datasets.utils.py_utils.dumps(create_ipython_func(__lowerCamelCase    ,       __lowerCamelCase ) ) ).hexdigest()
           snake_case__			,       snake_case__					:      List[str]  			=						os.path.join(gettempdir()    ,       'ipykernel_12345'    ,       '654123987.py' ), [0]
           snake_case__					:      int  			=						mda(datasets.utils.py_utils.dumps(create_ipython_func(__lowerCamelCase    ,       __lowerCamelCase ) ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				int ):
           snake_case__			,       snake_case__					:      List[Any]  			=						[0], [1]

           def func():
                 return foo, bar

           snake_case__					:      Dict  			=						'__main__'

           def globalvars_mocka_side_effect(__lowerCamelCase    :				List[Any]    ,       *__lowerCamelCase    :				Dict    ,       **__lowerCamelCase    :				List[Any] ):
                 return {"foo": foo, "bar": bar}

           def globalvars_mocka_side_effect(__lowerCamelCase    :				Any    ,       *__lowerCamelCase    :				Dict    ,       **__lowerCamelCase    :				List[str] ):
                 return {"bar": bar, "foo": foo}

           with patch('dill.detect.globalvars'    ,       side_effect=__lowerCamelCase ) as globalvars_mocka:
                 snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
                 self.assertGreater(globalvars_mocka.call_count    ,       0 )
           with patch('dill.detect.globalvars'    ,       side_effect=__lowerCamelCase ) as globalvars_mocka:
                 snake_case__					:      Tuple  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
                 self.assertGreater(globalvars_mocka.call_count    ,       0 )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )






class 					lowercase_			(	lowerCAmelCase_       ):





     def  _lowerCAmelCase			(    self    :				List[str] ):
           snake_case__					:      Dict  			=						Hasher.hash('hello' )
           snake_case__					:      Dict  			=						Hasher.hash('hello' )
           snake_case__					:      str  			=						Hasher.hash('there' )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				List[Any] ):
           snake_case__					:      Tuple  			=						Hasher.hash(Foo('hello' ) )
           snake_case__					:      List[str]  			=						Hasher.hash(Foo('hello' ) )
           snake_case__					:      Any  			=						Hasher.hash(Foo('there' ) )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				Dict ):
           snake_case__					:      Any  			=						Hasher()
           for x in ["hello", Foo('hello' )]:
                 hasher.update(__lowerCamelCase )
           snake_case__					:      Optional[Any]  			=						hasher.hexdigest()
           snake_case__					:      Tuple  			=						Hasher()
           for x in ["hello", Foo('hello' )]:
                 hasher.update(__lowerCamelCase )
           snake_case__					:      Union[str, Any]  			=						hasher.hexdigest()
           snake_case__					:      Optional[int]  			=						Hasher()
           for x in ["there", Foo('there' )]:
                 hasher.update(__lowerCamelCase )
           snake_case__					:      Dict  			=						hasher.hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     def  _lowerCAmelCase			(    self    :				int ):
           with self.assertRaises(pickle.PicklingError ):
                 Hasher.hash(UnpicklableCallable(Foo('hello' ) ) )





     def  _lowerCAmelCase			(    self    :				List[str] ):
           snake_case__					:      str  			=						'abc'
           snake_case__					:      int  			=						[string, string]  # two strings have the same ids
           snake_case__					:      Optional[Any]  			=						[string, string]
           snake_case__					:      Union[str, Any]  			=						json.loads(F"[\"{string}\", \"{string}\"]" )  # two strings have different ids
           self.assertIs(obja[0]    ,       __lowerCamelCase )
           self.assertIs(obja[0]    ,       obja[1] )
           self.assertIs(obja[0]    ,       __lowerCamelCase )
           self.assertIs(obja[0]    ,       obja[1] )
           self.assertIsNot(obja[0]    ,       __lowerCamelCase )
           self.assertIsNot(obja[0]    ,       obja[1] )
           snake_case__					:      str  			=						Hasher.hash(__lowerCamelCase )
           snake_case__					:      Dict  			=						Hasher.hash(__lowerCamelCase )
           snake_case__					:      Optional[int]  			=						Hasher.hash(__lowerCamelCase )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )





     @require_tiktoken
     def  _lowerCAmelCase			(    self    :				str ):
           import tiktoken

           snake_case__					:      List[Any]  			=						tiktoken.get_encoding('gpt2' )
           snake_case__					:      Union[str, Any]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      List[str]  			=						tiktoken.get_encoding('r50k_base' )
           snake_case__					:      List[str]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      int  			=						tiktoken.get_encoding('gpt2' )
           snake_case__					:      List[str]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     @require_torch
     def  _lowerCAmelCase			(    self    :				Any ):
           import torch

           snake_case__					:      Any  			=						torch.tensor([1.0] )
           snake_case__					:      Tuple  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Dict  			=						torch.tensor([2.0] )
           snake_case__					:      int  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Dict  			=						torch.tensor([1.0] )
           snake_case__					:      Any  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )





     @require_spacy
     @require_spacy_model('en_core_web_sm' )
     @require_spacy_model('fr_core_news_sm' )
     @pytest.mark.integration
     def  _lowerCAmelCase			(    self    :				Tuple ):
           import spacy

           snake_case__					:      Optional[Any]  			=						spacy.load('en_core_web_sm' )
           snake_case__					:      Dict  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Union[str, Any]  			=						spacy.load('fr_core_news_sm' )
           snake_case__					:      Optional[int]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           snake_case__					:      Any  			=						spacy.load('en_core_web_sm' )
           snake_case__					:      List[str]  			=						mda(datasets.utils.py_utils.dumps(__lowerCamelCase ) ).hexdigest()
           self.assertEqual(__lowerCamelCase    ,       __lowerCamelCase )
           self.assertNotEqual(__lowerCamelCase    ,       __lowerCamelCase )






@pytest.mark.integration
def 		UpperCamelCase__			(		__SCREAMING_SNAKE_CASE		)						->      List[Any]:
      snake_case__					:      int  			=						tmp_path / 'dir1'
      snake_case__					:      Optional[Any]  			=						tmp_path / 'dir2'
      dira.mkdir()
      dira.mkdir()
      snake_case__					:      List[Any]  			=						'script.py'
      snake_case__					:      Any  			=						dedent(
          '\n    from datasets.fingerprint import Hasher\n    def foo():\n        pass\n    print(Hasher.hash(foo))\n    '		)
      snake_case__					:      int  			=						dira / script_filename
      snake_case__					:      List[str]  			=						dira / script_filename
      with script_patha.open('w'		) as f:
            f.write(__SCREAMING_SNAKE_CASE		)
      with script_patha.open('w'		) as f:
            f.write(__SCREAMING_SNAKE_CASE		)
      snake_case__					:      Optional[int]  			=						subprocess.check_output(['python', str(__SCREAMING_SNAKE_CASE		)]		)
      snake_case__					:      List[Any]  			=						subprocess.check_output(['python', str(__SCREAMING_SNAKE_CASE		)]		)
      assert fingerprinta == fingerprinta






def 		UpperCamelCase__			(		)						->      Optional[Any]:
      snake_case__					:      Optional[int]  			=						{'a': [0, 1, 2]}
      snake_case__					:      Union[str, Any]  			=						DatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE		)		)
      snake_case__					:      List[Any]  			=						dataset.funca()._fingerprint
      assert expected_fingerprint == dataset.funca()._fingerprint
      assert expected_fingerprint != dataset.funca()._fingerprint

      with Pool(2		) as p:
            assert expected_fingerprint == p.apply_async(dataset.funca		).get()._fingerprint
            assert expected_fingerprint != p.apply_async(dataset.funca		).get()._fingerprint






def 		UpperCamelCase__			(		)						->      Optional[int]:
      snake_case__					:      int  			=						{'a': [0, 1, 2]}






      class 					lowercase_			(	datasets.Dataset       ):





           @fingerprint_transform(inplace=__lowerCamelCase )
           def  _lowerCAmelCase			(    self    :				Optional[int]    ,       __lowerCamelCase    :				int ):
                 return DummyDatasetChild(self.data    ,       fingerprint=__lowerCamelCase )



      snake_case__					:      str  			=						DummyDatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE		)		).func()






      class 					lowercase_			(	datasets.Dataset       ):





           @fingerprint_transform(inplace=__lowerCamelCase    ,       version='1.0.0' )
           def  _lowerCAmelCase			(    self    :				Optional[Any]    ,       __lowerCamelCase    :				List[str] ):
                 return DummyDatasetChild(self.data    ,       fingerprint=__lowerCamelCase )



      snake_case__					:      List[Any]  			=						DummyDatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE		)		).func()






      class 					lowercase_			(	datasets.Dataset       ):





           @fingerprint_transform(inplace=__lowerCamelCase    ,       version='2.0.0' )
           def  _lowerCAmelCase			(    self    :				int    ,       __lowerCamelCase    :				Optional[Any] ):
                 return DummyDatasetChild(self.data    ,       fingerprint=__lowerCamelCase )



      snake_case__					:      int  			=						DummyDatasetChild(InMemoryTable.from_pydict(__SCREAMING_SNAKE_CASE		)		).func()

      assert len({fingeprint_no_version, fingeprint_a, fingeprint_a}		) == 3






def 		UpperCamelCase__			(		)						->      Any:
      # AttributeError: module 'dill._dill' has no attribute 'stack'
      snake_case__					:      Union[str, Any]  			=						Hasher()
      hasher.update(lambda __SCREAMING_SNAKE_CASE		: x		)






'''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, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import ViltImageProcessor






class 					lowercase_			(	unittest.TestCase       ):





     def __init__(    self    :				Optional[Any]    ,       __lowerCamelCase    :				Any    ,       __lowerCamelCase    :				Optional[Any]=7    ,       __lowerCamelCase    :				Optional[Any]=3    ,       __lowerCamelCase    :				int=18    ,       __lowerCamelCase    :				List[str]=30    ,       __lowerCamelCase    :				List[str]=400    ,       __lowerCamelCase    :				Optional[Any]=True    ,       __lowerCamelCase    :				Any=None    ,       __lowerCamelCase    :				List[str]=2    ,       __lowerCamelCase    :				Optional[Any]=True    ,       __lowerCamelCase    :				List[Any]=[0.5, 0.5, 0.5]    ,       __lowerCamelCase    :				Any=[0.5, 0.5, 0.5]    ,       ):
           snake_case__					:      Dict  			=						size if size is not None else {'shortest_edge': 30}
           snake_case__					:      Any  			=						parent
           snake_case__					:      int  			=						batch_size
           snake_case__					:      List[str]  			=						num_channels
           snake_case__					:      str  			=						image_size
           snake_case__					:      List[Any]  			=						min_resolution
           snake_case__					:      Dict  			=						max_resolution
           snake_case__					:      str  			=						do_resize
           snake_case__					:      str  			=						size
           snake_case__					:      List[str]  			=						size_divisor
           snake_case__					:      List[Any]  			=						do_normalize
           snake_case__					:      Optional[Any]  			=						image_mean
           snake_case__					:      Union[str, Any]  			=						image_std





     def  _lowerCAmelCase			(    self    :				Union[str, Any] ):
           return {
               "image_mean": self.image_mean,
               "image_std": self.image_std,
               "do_normalize": self.do_normalize,
               "do_resize": self.do_resize,
               "size": self.size,
               "size_divisor": self.size_divisor,
           }





     def  _lowerCAmelCase			(    self    :				Optional[Any]    ,       __lowerCamelCase    :				str    ,       __lowerCamelCase    :				Union[str, Any]=False ):
           if not batched:
                 snake_case__					:      int  			=						self.size['shortest_edge']
                 snake_case__					:      Union[str, Any]  			=						image_inputs[0]
                 if isinstance(__lowerCamelCase    ,       Image.Image ):
                       snake_case__			,       snake_case__					:      Any  			=						image.size
                 else:
                       snake_case__			,       snake_case__					:      Optional[Any]  			=						image.shape[1], image.shape[2]
                 snake_case__					:      int  			=						size / min(__lowerCamelCase    ,       __lowerCamelCase )
                 if h < w:
                       snake_case__			,       snake_case__					:      Tuple  			=						size, scale * w
                 else:
                       snake_case__			,       snake_case__					:      List[str]  			=						scale * h, size

                 snake_case__					:      List[Any]  			=						int((1333 / 800) * size )
                 if max(__lowerCamelCase    ,       __lowerCamelCase ) > max_size:
                       snake_case__					:      Dict  			=						max_size / max(__lowerCamelCase    ,       __lowerCamelCase )
                       snake_case__					:      List[Any]  			=						newh * scale
                       snake_case__					:      Optional[Any]  			=						neww * scale

                 snake_case__			,       snake_case__					:      str  			=						int(newh + 0.5 ), int(neww + 0.5 )
                 snake_case__			,       snake_case__					:      Any  			=						(
                     newh // self.size_divisor * self.size_divisor,
                     neww // self.size_divisor * self.size_divisor,
                 )

           else:
                 snake_case__					:      int  			=						[]
                 for image in image_inputs:
                       snake_case__			,       snake_case__					:      Union[str, Any]  			=						self.get_expected_values([image] )
                       expected_values.append((expected_height, expected_width) )
                 snake_case__					:      Any  			=						max(__lowerCamelCase    ,       key=lambda __lowerCamelCase : item[0] )[0]
                 snake_case__					:      List[Any]  			=						max(__lowerCamelCase    ,       key=lambda __lowerCamelCase : item[1] )[1]

           return expected_height, expected_width






@require_torch
@require_vision
class 					lowercase_			(	lowerCAmelCase_	,					unittest.TestCase       ):
     A_       							= ViltImageProcessor if is_vision_available() else None





     def  _lowerCAmelCase			(    self    :				Union[str, Any] ):
           snake_case__					:      int  			=						ViltImageProcessingTester(self )





     @property
     def  _lowerCAmelCase			(    self    :				List[Any] ):
           return self.image_processor_tester.prepare_image_processor_dict()





     def  _lowerCAmelCase			(    self    :				Optional[int] ):
           snake_case__					:      int  			=						self.image_processing_class(**self.image_processor_dict )
           self.assertTrue(hasattr(__lowerCamelCase    ,       'image_mean' ) )
           self.assertTrue(hasattr(__lowerCamelCase    ,       'image_std' ) )
           self.assertTrue(hasattr(__lowerCamelCase    ,       'do_normalize' ) )
           self.assertTrue(hasattr(__lowerCamelCase    ,       'do_resize' ) )
           self.assertTrue(hasattr(__lowerCamelCase    ,       'size' ) )
           self.assertTrue(hasattr(__lowerCamelCase    ,       'size_divisor' ) )





     def  _lowerCAmelCase			(    self    :				Tuple ):
           snake_case__					:      Optional[Any]  			=						self.image_processing_class.from_dict(self.image_processor_dict )
           self.assertEqual(image_processor.size    ,       {'shortest_edge': 30} )

           snake_case__					:      List[str]  			=						self.image_processing_class.from_dict(self.image_processor_dict    ,       size=42 )
           self.assertEqual(image_processor.size    ,       {'shortest_edge': 42} )





     def  _lowerCAmelCase			(    self    :				str ):
           pass





     def  _lowerCAmelCase			(    self    :				List[Any] ):
           # Initialize image_processing
           snake_case__					:      int  			=						self.image_processing_class(**self.image_processor_dict )
           # create random PIL images
           snake_case__					:      str  			=						prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__lowerCamelCase )
           for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase    ,       Image.Image )

           # Test not batched input
           snake_case__					:      int  			=						image_processing(image_inputs[0]    ,       return_tensors='pt' ).pixel_values

           snake_case__			,       snake_case__					:      Union[str, Any]  			=						self.image_processor_tester.get_expected_values(__lowerCamelCase )
           self.assertEqual(
               encoded_images.shape    ,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,       )

           # Test batched
           snake_case__					:      Tuple  			=						image_processing(__lowerCamelCase    ,       return_tensors='pt' ).pixel_values

           snake_case__			,       snake_case__					:      Optional[Any]  			=						self.image_processor_tester.get_expected_values(__lowerCamelCase    ,       batched=__lowerCamelCase )
           self.assertEqual(
               encoded_images.shape    ,       (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    ,       )





     def  _lowerCAmelCase			(    self    :				Union[str, Any] ):
           # Initialize image_processing
           snake_case__					:      List[Any]  			=						self.image_processing_class(**self.image_processor_dict )
           # create random numpy tensors
           snake_case__					:      Dict  			=						prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__lowerCamelCase    ,       numpify=__lowerCamelCase )
           for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase    ,       np.ndarray )

           # Test not batched input
           snake_case__					:      List[Any]  			=						image_processing(image_inputs[0]    ,       return_tensors='pt' ).pixel_values

           snake_case__			,       snake_case__					:      Tuple  			=						self.image_processor_tester.get_expected_values(__lowerCamelCase )
           self.assertEqual(
               encoded_images.shape    ,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,       )

           # Test batched
           snake_case__					:      Tuple  			=						image_processing(__lowerCamelCase    ,       return_tensors='pt' ).pixel_values

           snake_case__			,       snake_case__					:      Union[str, Any]  			=						self.image_processor_tester.get_expected_values(__lowerCamelCase    ,       batched=__lowerCamelCase )
           self.assertEqual(
               encoded_images.shape    ,       (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    ,       )





     def  _lowerCAmelCase			(    self    :				str ):
           # Initialize image_processing
           snake_case__					:      str  			=						self.image_processing_class(**self.image_processor_dict )
           # create random PyTorch tensors
           snake_case__					:      Dict  			=						prepare_image_inputs(self.image_processor_tester    ,       equal_resolution=__lowerCamelCase    ,       torchify=__lowerCamelCase )
           for image in image_inputs:
                 self.assertIsInstance(__lowerCamelCase    ,       torch.Tensor )

           # Test not batched input
           snake_case__					:      Optional[int]  			=						image_processing(image_inputs[0]    ,       return_tensors='pt' ).pixel_values

           snake_case__			,       snake_case__					:      Optional[int]  			=						self.image_processor_tester.get_expected_values(__lowerCamelCase )
           self.assertEqual(
               encoded_images.shape    ,       (1, self.image_processor_tester.num_channels, expected_height, expected_width)    ,       )

           # Test batched
           snake_case__					:      Optional[int]  			=						image_processing(__lowerCamelCase    ,       return_tensors='pt' ).pixel_values

           snake_case__			,       snake_case__					:      Tuple  			=						self.image_processor_tester.get_expected_values(__lowerCamelCase    ,       batched=__lowerCamelCase )
           self.assertEqual(
               encoded_images.shape    ,       (
                   self.image_processor_tester.batch_size,
                   self.image_processor_tester.num_channels,
                   expected_height,
                   expected_width,
               )    ,       )
