



"""simple docstring"""





import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoFeatureExtractor, WavaVecaFeatureExtractor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / """utils"""))

from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


lowerCamelCase      =		get_tests_dir("""fixtures""")
class    lowercase__     (					unittest.TestCase   ):







       '''simple docstring'''


       def   lowercase__       (							self    :						Optional[int]					)				->		Optional[Any]:


             '''simple docstring'''




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

             # Download this model to make sure it's in the cache.
             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"					)
             # Under the mock environment we get a 500 error when trying to reach the model.
             with mock.patch("requests.Session.request"	,       return_value=_UpperCAmelCase					) as mock_head:
                   UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained("hf-internal-testing/tiny-random-wav2vec2"					)
                   # This check we did call the fake head request
                   mock_head.assert_called()







       def   lowercase__       (							self    :						List[str]					)				->		Dict:


             '''simple docstring'''




             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained(
                 "https://huggingface.co/hf-internal-testing/tiny-random-wav2vec2/resolve/main/preprocessor_config.json"					)



@is_staging_test
class    lowercase__     (					unittest.TestCase   ):







       '''simple docstring'''


       @classmethod
       def   lowercase__       (							cls    :						Optional[Any]					)				->		int:


             '''simple docstring'''




             UpperCAmelCase_    				=							TOKEN
             HfFolder.save_token(_UpperCAmelCase					)



       @classmethod
       def   lowercase__       (							cls    :						int					)				->		Union[str, Any]:


             '''simple docstring'''




             try:
                   delete_repo(token=cls._token	,       repo_id="test-feature-extractor"					)
             except HTTPError:
                   pass

             try:
                   delete_repo(token=cls._token	,       repo_id="valid_org/test-feature-extractor-org"					)
             except HTTPError:
                   pass

             try:
                   delete_repo(token=cls._token	,       repo_id="test-dynamic-feature-extractor"					)
             except HTTPError:
                   pass



       def   lowercase__       (							self    :						Any					)				->		List[str]:


             '''simple docstring'''




             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained(_UpperCAmelCase					)
             feature_extractor.push_to_hub("test-feature-extractor"	,       use_auth_token=self._token					)

             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained(F"""{USER}/test-feature-extractor"""					)
             for k, v in feature_extractor.__dict__.items():
                   self.assertEqual(_UpperCAmelCase	,       getattr(_UpperCAmelCase	,       _UpperCAmelCase					)					)

             # Reset repo
             delete_repo(token=self._token	,       repo_id="test-feature-extractor"					)

             # Push to hub via save_pretrained
             with tempfile.TemporaryDirectory() as tmp_dir:
                   feature_extractor.save_pretrained(
                       _UpperCAmelCase	,       repo_id="test-feature-extractor"	,       push_to_hub=_UpperCAmelCase	,       use_auth_token=self._token					)

             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained(F"""{USER}/test-feature-extractor"""					)
             for k, v in feature_extractor.__dict__.items():
                   self.assertEqual(_UpperCAmelCase	,       getattr(_UpperCAmelCase	,       _UpperCAmelCase					)					)



       def   lowercase__       (							self    :						Tuple					)				->		Dict:


             '''simple docstring'''




             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained(_UpperCAmelCase					)
             feature_extractor.push_to_hub("valid_org/test-feature-extractor"	,       use_auth_token=self._token					)

             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor"					)
             for k, v in feature_extractor.__dict__.items():
                   self.assertEqual(_UpperCAmelCase	,       getattr(_UpperCAmelCase	,       _UpperCAmelCase					)					)

             # Reset repo
             delete_repo(token=self._token	,       repo_id="valid_org/test-feature-extractor"					)

             # Push to hub via save_pretrained
             with tempfile.TemporaryDirectory() as tmp_dir:
                   feature_extractor.save_pretrained(
                       _UpperCAmelCase	,       repo_id="valid_org/test-feature-extractor-org"	,       push_to_hub=_UpperCAmelCase	,       use_auth_token=self._token					)

             UpperCAmelCase_    				=							WavaVecaFeatureExtractor.from_pretrained("valid_org/test-feature-extractor-org"					)
             for k, v in feature_extractor.__dict__.items():
                   self.assertEqual(_UpperCAmelCase	,       getattr(_UpperCAmelCase	,       _UpperCAmelCase					)					)







       def   lowercase__       (							self    :						Any					)				->		Optional[int]:


             '''simple docstring'''




             CustomFeatureExtractor.register_for_auto_class()
             UpperCAmelCase_    				=							CustomFeatureExtractor.from_pretrained(_UpperCAmelCase					)

             feature_extractor.push_to_hub("test-dynamic-feature-extractor"	,       use_auth_token=self._token					)

             # This has added the proper auto_map field to the config
             self.assertDictEqual(
                 feature_extractor.auto_map	,       {"AutoFeatureExtractor": "custom_feature_extraction.CustomFeatureExtractor"}	,       )

             UpperCAmelCase_    				=							AutoFeatureExtractor.from_pretrained(
                 F"""{USER}/test-dynamic-feature-extractor"""	,       trust_remote_code=_UpperCAmelCase					)
             # Can't make an isinstance check because the new_feature_extractor is from the CustomFeatureExtractor class of a dynamic module
             self.assertEqual(new_feature_extractor.__class__.__name__	,       "CustomFeatureExtractor"					)









"""simple docstring"""





def      a__			(   lowerCAmelCase__   ):
      if not head:
            return True
      # split the list to two parts
      UpperCAmelCase_				,					UpperCAmelCase_    				=							head.next, head
      while fast and fast.next:
            UpperCAmelCase_    				=							fast.next.next
            UpperCAmelCase_    				=							slow.next
      UpperCAmelCase_    				=							slow.next
      UpperCAmelCase_    				=							None  # Don't forget here! But forget still works!
      # reverse the second part
      UpperCAmelCase_    				=							None
      while second:
            UpperCAmelCase_    				=							second.next
            UpperCAmelCase_    				=							node
            UpperCAmelCase_    				=							second
            UpperCAmelCase_    				=							nxt
      # compare two parts
      # second part has the same or one less node
      while node:
            if node.val != head.val:
                  return False
            UpperCAmelCase_    				=							node.next
            UpperCAmelCase_    				=							head.next
      return True
def      a__			(   lowerCAmelCase__   ):
      if not head or not head.next:
            return True

      # 1. Get the midpoint (slow)
      UpperCAmelCase_    				=							UpperCAmelCase_    				=							UpperCAmelCase_    				=							head
      while fast and fast.next:
            UpperCAmelCase_				,					UpperCAmelCase_    				=							fast.next.next, slow.next

      # 2. Push the second half into the stack
      UpperCAmelCase_    				=							[slow.val]
      while slow.next:
            UpperCAmelCase_    				=							slow.next
            stack.append(slow.val   )

      # 3. Comparison
      while stack:
            if stack.pop() != cur.val:
                  return False
            UpperCAmelCase_    				=							cur.next

      return True


def      a__			(   lowerCAmelCase__   ):
      if not head or not head.next:
            return True
      UpperCAmelCase_    				=							{}
      UpperCAmelCase_    				=							0
      while head:
            if head.val in d:
                  d[head.val].append(lowerCAmelCase__   )
            else:
                  UpperCAmelCase_    				=							[pos]
            UpperCAmelCase_    				=							head.next
            pos += 1
      UpperCAmelCase_    				=							pos - 1
      UpperCAmelCase_    				=							0
      for v in d.values():
            if len(lowerCAmelCase__   ) % 2 != 0:
                  middle += 1
            else:
                  UpperCAmelCase_    				=							0
                  for i in range(0			,    len(lowerCAmelCase__   )   ):
                        if v[i] + v[len(lowerCAmelCase__   ) - 1 - step] != checksum:
                              return False
                        step += 1
            if middle > 1:
                  return False
      return True




