





import json
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
from transformers import (
    CONFIG_MAPPING,
    FEATURE_EXTRACTOR_MAPPING,
    AutoConfig,
    AutoFeatureExtractor,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
)
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


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

from test_module.custom_configuration import CustomConfig  # noqa E402
from test_module.custom_feature_extraction import CustomFeatureExtractor  # noqa E402


__lowerCamelCase       :     Union[str, Any]   =     get_tests_dir("""fixtures""")
__lowerCamelCase       :     List[Any]   =     get_tests_dir("""fixtures/dummy_feature_extractor_config.json""")
__lowerCamelCase       :     Union[str, Any]   =     get_tests_dir("""fixtures/dummy-config.json""")



class 					A__   (       unittest.TestCase			):





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					str       	=   0





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					int       	=   AutoFeatureExtractor.from_pretrained("facebook/wav2vec2-base-960h"   )
       self.assertIsInstance(__a       ,							__a   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Dict       	=   AutoFeatureExtractor.from_pretrained(__a   )
       self.assertIsInstance(__a       ,							__a   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       with tempfile.TemporaryDirectory() as tmpdirname:
        UpperCamelCase	:					Tuple       	=   WavaVecaConfig()

        # remove feature_extractor_type to make sure config.json alone is enough to load feature processor locally
        UpperCamelCase	:					List[str]       	=   AutoFeatureExtractor.from_pretrained(__a   ).to_dict()

        config_dict.pop("feature_extractor_type"   )
        UpperCamelCase	:					Optional[int]       	=   WavaVecaFeatureExtractor(**__a   )

        # save in new folder
        model_config.save_pretrained(__a   )
        config.save_pretrained(__a   )

        UpperCamelCase	:					List[str]       	=   AutoFeatureExtractor.from_pretrained(__a   )

        # make sure private variable is not incorrectly saved
        UpperCamelCase	:					Dict       	=   json.loads(config.to_json_string()   )
        self.assertTrue("_processor_class" not in dict_as_saved   )

       self.assertIsInstance(__a       ,							__a   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       UpperCamelCase	:					Optional[Any]       	=   AutoFeatureExtractor.from_pretrained(__a   )
       self.assertIsInstance(__a       ,							__a   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       with self.assertRaisesRegex(
           __a       ,							"bert-base is not a local folder and is not a valid model identifier"   ):
        UpperCamelCase	:					Optional[int]       	=   AutoFeatureExtractor.from_pretrained("bert-base"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       with self.assertRaisesRegex(
           __a       ,							R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"   ):
        UpperCamelCase	:					Any       	=   AutoFeatureExtractor.from_pretrained(__a       ,							revision="aaaaaa"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       with self.assertRaisesRegex(
           __a       ,							"hf-internal-testing/config-no-model does not appear to have a file named preprocessor_config.json."       ,							):
        UpperCamelCase	:					Optional[Any]       	=   AutoFeatureExtractor.from_pretrained("hf-internal-testing/config-no-model"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       with self.assertRaises(__a   ):
        UpperCamelCase	:					List[Any]       	=   AutoFeatureExtractor.from_pretrained(
            "hf-internal-testing/test_dynamic_feature_extractor"   )
       # If remote code is disabled, we can't load this config.
       with self.assertRaises(__a   ):
        UpperCamelCase	:					List[Any]       	=   AutoFeatureExtractor.from_pretrained(
            "hf-internal-testing/test_dynamic_feature_extractor"       ,							trust_remote_code=__a   )

       UpperCamelCase	:					Tuple       	=   AutoFeatureExtractor.from_pretrained(
           "hf-internal-testing/test_dynamic_feature_extractor"       ,							trust_remote_code=__a   )
       self.assertEqual(feature_extractor.__class__.__name__       ,							"NewFeatureExtractor"   )

       # Test feature extractor can be reloaded.
       with tempfile.TemporaryDirectory() as tmp_dir:
        feature_extractor.save_pretrained(__a   )
        UpperCamelCase	:					Union[str, Any]       	=   AutoFeatureExtractor.from_pretrained(__a       ,							trust_remote_code=__a   )
       self.assertEqual(reloaded_feature_extractor.__class__.__name__       ,							"NewFeatureExtractor"   )





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       try:
        AutoConfig.register("custom"       ,							__a   )
        AutoFeatureExtractor.register(__a       ,							__a   )
        # Trying to register something existing in the Transformers library will raise an error
        with self.assertRaises(__a   ):
         AutoFeatureExtractor.register(__a       ,							__a   )

        # Now that the config is registered, it can be used as any other config with the auto-API
        UpperCamelCase	:					str       	=   CustomFeatureExtractor.from_pretrained(__a   )
        with tempfile.TemporaryDirectory() as tmp_dir:
         feature_extractor.save_pretrained(__a   )
         UpperCamelCase	:					Tuple       	=   AutoFeatureExtractor.from_pretrained(__a   )
         self.assertIsInstance(__a       ,							__a   )

       finally:
        if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]
        if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
         del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]





      def        __UpperCamelCase( self   ):
       '''simple docstring'''




       class 					A__   (       UpperCamelCase__			):
             _UpperCAmelCase	:Optional[Any]						    =   True

       try:
        AutoConfig.register("custom"       ,							__a   )
        AutoFeatureExtractor.register(__a       ,							__a   )
        # If remote code is not set, the default is to use local
        UpperCamelCase	:					Tuple       	=   AutoFeatureExtractor.from_pretrained(
            "hf-internal-testing/test_dynamic_feature_extractor"   )
        self.assertEqual(feature_extractor.__class__.__name__       ,							"NewFeatureExtractor"   )
        self.assertTrue(feature_extractor.is_local   )

        # If remote code is disabled, we load the local one.
        UpperCamelCase	:					Optional[int]       	=   AutoFeatureExtractor.from_pretrained(
            "hf-internal-testing/test_dynamic_feature_extractor"       ,							trust_remote_code=__a   )
        self.assertEqual(feature_extractor.__class__.__name__       ,							"NewFeatureExtractor"   )
        self.assertTrue(feature_extractor.is_local   )

        # If remote is enabled, we load from the Hub
        UpperCamelCase	:					Any       	=   AutoFeatureExtractor.from_pretrained(
            "hf-internal-testing/test_dynamic_feature_extractor"       ,							trust_remote_code=__a   )
        self.assertEqual(feature_extractor.__class__.__name__       ,							"NewFeatureExtractor"   )
        self.assertTrue(not hasattr(__a       ,							"is_local"   )   )

       finally:
        if "custom" in CONFIG_MAPPING._extra_content:
         del CONFIG_MAPPING._extra_content["custom"]
        if CustomConfig in FEATURE_EXTRACTOR_MAPPING._extra_content:
         del FEATURE_EXTRACTOR_MAPPING._extra_content[CustomConfig]

'''simple docstring'''





from multiprocessing import Lock, Pipe, Process

# lock used to ensure that two processes do not access a pipe at the same time
SCREAMING_SNAKE_CASE_:     Optional[int]  =Lock()





def 	lowerCAmelCase_				(					snake_case_       : Dict					,    snake_case_       : Dict					,    snake_case_       : Optional[int]					,    snake_case_       : Tuple					,    snake_case_       : Any					,    snake_case_       : Optional[Any]					,    snake_case_       : Union[str, Any]			)    ->      Optional[Any]:


     '''simple docstring'''




     global process_lock

     # we perform n swaps since after n swaps we know we are sorted
     # we *could* stop early if we are sorted already, but it takes as long to
     # find out we are sorted as it does to sort the list with this algorithm
     for i in range(0					,    10			):
          if (i + position) % 2 == 0 and r_send is not None:
               # send your value to your right neighbor
               process_lock.acquire()
               r_send[1].send(snake_case_			)
               process_lock.release()

               # receive your right neighbor's value
               process_lock.acquire()
               UpperCAmelCase_           =   rr_cv[0].recv()
               process_lock.release()

               # take the lower value since you are on the left
               UpperCAmelCase_           =   min(snake_case_					,    snake_case_			)
          elif (i + position) % 2 != 0 and l_send is not None:
               # send your value to your left neighbor
               process_lock.acquire()
               l_send[1].send(snake_case_			)
               process_lock.release()

               # receive your left neighbor's value
               process_lock.acquire()
               UpperCAmelCase_           =   lr_cv[0].recv()
               process_lock.release()

               # take the higher value since you are on the right
               UpperCAmelCase_           =   max(snake_case_					,    snake_case_			)
    # after all swaps are performed, send the values back to main
     result_pipe[1].send(snake_case_			)





def 	lowerCAmelCase_				(					snake_case_       : Optional[Any]			)    ->      Dict:


     '''simple docstring'''




     UpperCAmelCase_           =   []
     UpperCAmelCase_           =   []
     # initialize the list of pipes where the values will be retrieved
     for _ in arr:
          result_pipe.append(Pipe()			)
     # creates the processes
     # the first and last process only have one neighbor so they are made outside
     # of the loop
     UpperCAmelCase_           =   Pipe()
     UpperCAmelCase_           =   Pipe()
     process_array_.append(
         Process(
             target=snake_case_					,    args=(0, arr[0], None, temp_rs, None, temp_rr, result_pipe[0])					,    )			)
     UpperCAmelCase_           =   temp_rs
     UpperCAmelCase_           =   temp_rr

     for i in range(1					,    len(snake_case_			) - 1			):
          UpperCAmelCase_           =   Pipe()
          UpperCAmelCase_           =   Pipe()
          process_array_.append(
              Process(
                  target=snake_case_					,    args=(i, arr[i], temp_ls, temp_rs, temp_lr, temp_rr, result_pipe[i])					,    )			)
          UpperCAmelCase_           =   temp_rs
          UpperCAmelCase_           =   temp_rr

     process_array_.append(
         Process(
             target=snake_case_					,    args=(
                 len(snake_case_			) - 1,
                 arr[len(snake_case_			) - 1],
                 temp_ls,
                 None,
                 temp_lr,
                 None,
                 result_pipe[len(snake_case_			) - 1],
             )					,    )			)

     # start the processes
     for p in process_array_:
          p.start()

     # wait for the processes to end and write their values to the list
     for p in range(0					,    len(snake_case_			)			):
          UpperCAmelCase_           =   result_pipe[p][0].recv()
          process_array_[p].join()
     return arr





def 	lowerCAmelCase_				(					)    ->      str:


     '''simple docstring'''




     UpperCAmelCase_           =   list(range(10					,    0					,    -1			)			)
     print("Initial List"			)
     print(*snake_case_			)
     UpperCAmelCase_           =   odd_even_transposition(snake_case_			)
     print("Sorted List\n"			)
     print(*snake_case_			)


if __name__ == "__main__":
    main()




