





"""simple docstring"""


import copy
import json
import os
import tempfile

from transformers import is_torch_available

from .test_configuration_utils import config_common_kwargs



class   __SCREAMING_SNAKE_CASE     (    _UpperCamelCase ):







  '''simple docstring'''


  def __init__(		self :							int       , __a :							List[Any]       , __a :							List[Any]=None       , __a :							Union[str, Any]=True       , __a :							Optional[Any]=None       , **__a :							List[Any]						)		->					int:
        _UpperCamelCase   :						Dict    =							parent
        _UpperCamelCase   :						Dict    =							config_class
        _UpperCamelCase   :						List[Any]    =							has_text_modality
        _UpperCamelCase   :						str    =							kwargs
        _UpperCamelCase   :						Optional[int]    =							common_properties

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple						)		->					str:
        _UpperCamelCase   :						List[str]    =							self.config_class(**self.inputs_dict						)
        _UpperCamelCase   :						Union[str, Any]    =							(
            ["hidden_size", "num_attention_heads", "num_hidden_layers"]
            if self.common_properties is None
            else self.common_properties
        )

        # Add common fields for text models
        if self.has_text_modality:
              common_properties.extend(["vocab_size"]						)

        # Test that config has the common properties as getters
        for prop in common_properties:
              self.parent.assertTrue(hasattr(__a       , __a						)       , msg=F'''`{prop}` does not exist'''						)

        # Test that config has the common properties as setter
        for idx, name in enumerate(__a						):
              try:
                    setattr(__a       , __a       , __a						)
                    self.parent.assertEqual(
                        getattr(__a       , __a						)       , __a       , msg=F'''`{name} value {idx} expected, but was {getattr(__a       , __a						)}'''						)
              except NotImplementedError:
                    # Some models might not be able to implement setters for common_properties
                    # In that case, a NotImplementedError is raised
                    pass

        # Test if config class can be called with Config(prop_name=..)
        for idx, name in enumerate(__a						):
              try:
                    _UpperCamelCase   :						Tuple    =							self.config_class(**{name: idx}						)
                    self.parent.assertEqual(
                        getattr(__a       , __a						)       , __a       , msg=F'''`{name} value {idx} expected, but was {getattr(__a       , __a						)}'''						)
              except NotImplementedError:
                    # Some models might not be able to implement setters for common_properties
                    # In that case, a NotImplementedError is raised
                    pass

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]						)		->					Dict:
        _UpperCamelCase   :						Dict    =							self.config_class(**self.inputs_dict						)
        _UpperCamelCase   :						List[str]    =							json.loads(config.to_json_string()						)
        for key, value in self.inputs_dict.items():
              self.parent.assertEqual(obj[key]       , __a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					Tuple:
        _UpperCamelCase   :						int    =							self.config_class(**self.inputs_dict						)

        with tempfile.TemporaryDirectory() as tmpdirname:
              _UpperCamelCase   :						List[str]    =							os.path.join(__a       , "config.json"						)
              config_first.to_json_file(__a						)
              _UpperCamelCase   :						Union[str, Any]    =							self.config_class.from_json_file(__a						)

        self.parent.assertEqual(config_second.to_dict()       , config_first.to_dict()						)

  def  __SCREAMING_SNAKE_CASE					(		self :							str						)		->					Tuple:
        _UpperCamelCase   :						Any    =							self.config_class(**self.inputs_dict						)

        with tempfile.TemporaryDirectory() as tmpdirname:
              config_first.save_pretrained(__a						)
              _UpperCamelCase   :						str    =							self.config_class.from_pretrained(__a						)

        self.parent.assertEqual(config_second.to_dict()       , config_first.to_dict()						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					List[str]:
        _UpperCamelCase   :						List[Any]    =							self.config_class(**self.inputs_dict						)

        _UpperCamelCase   :						Any    =							"test"
        with tempfile.TemporaryDirectory() as tmpdirname:
              _UpperCamelCase   :						Dict    =							os.path.join(__a       , __a						)
              config_first.save_pretrained(__a						)
              _UpperCamelCase   :						List[str]    =							self.config_class.from_pretrained(__a       , subfolder=__a						)

        self.parent.assertEqual(config_second.to_dict()       , config_first.to_dict()						)

  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Dict:
        _UpperCamelCase   :						int    =							self.config_class(**self.inputs_dict       , num_labels=5						)
        self.parent.assertEqual(len(config.idalabel						)       , 5						)
        self.parent.assertEqual(len(config.labelaid						)       , 5						)

        _UpperCamelCase   :						str    =							3
        self.parent.assertEqual(len(config.idalabel						)       , 3						)
        self.parent.assertEqual(len(config.labelaid						)       , 3						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Any						)		->					str:
        if self.config_class.is_composition:
              return
        _UpperCamelCase   :						str    =							self.config_class()
        self.parent.assertIsNotNone(__a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					List[str]:
        _UpperCamelCase   :						List[str]    =							copy.deepcopy(__a						)
        _UpperCamelCase   :						List[Any]    =							self.config_class(**__a						)
        _UpperCamelCase   :						List[Any]    =							[]
        for key, value in config_common_kwargs.items():
              if key == "torch_dtype":
                    if not is_torch_available():
                          continue
                    else:
                          import torch

                          if config.torch_dtype != torch.floataa:
                                wrong_values.append(("torch_dtype", config.torch_dtype, torch.floataa)						)
              elif getattr(__a       , __a						) != value:
                    wrong_values.append((key, getattr(__a       , __a						), value)						)

        if len(__a						) > 0:
              _UpperCamelCase   :						Union[str, Any]    =							"\n".join([F'''- {v[0]}: got {v[1]} instead of {v[2]}''' for v in wrong_values]						)
              raise ValueError(F'''The following keys were not properly set in the config:\n{errors}'''						)


  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					Optional[int]:
        self.create_and_test_config_common_properties()
        self.create_and_test_config_to_json_string()
        self.create_and_test_config_to_json_file()
        self.create_and_test_config_from_and_save_pretrained()
        self.create_and_test_config_from_and_save_pretrained_subfolder()
        self.create_and_test_config_with_num_labels()
        self.check_config_can_be_init_without_params()
        self.check_config_arguments_init()







"""simple docstring"""


import builtins
import sys

from ...utils.imports import _is_package_available
from . import cursor, input
from .helpers import Direction, clear_line, forceWrite, linebreak, move_cursor, reset_cursor, writeColor
from .keymap import KEYMAP


lowerCamelCase__						= False
try:
  lowerCamelCase__						= _is_package_available("google.colab")
except ModuleNotFoundError:
  pass



@input.register
class   __SCREAMING_SNAKE_CASE     :







  '''simple docstring'''


  def __init__(		self :							Union[str, Any]       , __a :							str = None       , __a :							list = []						)		->					Union[str, Any]:
        _UpperCamelCase   :						Any    =							0
        _UpperCamelCase   :						Any    =							choices
        _UpperCamelCase   :						Tuple    =							prompt
        if sys.platform == "win32":
              _UpperCamelCase   :						str    =							"*"
        else:
              _UpperCamelCase   :						Dict    =							"➔ "

  def  __SCREAMING_SNAKE_CASE					(		self :							int       , __a :							Tuple       , __a :							str = ""						)		->					Dict:
        if sys.platform != "win32":
              writeColor(self.choices[index]       , 32       , __a						)
        else:
              forceWrite(self.choices[index]       , __a						)

  def  __SCREAMING_SNAKE_CASE					(		self :							Tuple       , __a :							int						)		->					Dict:
        if index == self.position:
              forceWrite(F''' {self.arrow_char} '''						)
              self.write_choice(__a						)
        else:
              forceWrite(F'''    {self.choices[index]}'''						)
        reset_cursor()

  def  __SCREAMING_SNAKE_CASE					(		self :							List[str]       , __a :							Direction       , __a :							int = 1						)		->					List[str]:
        _UpperCamelCase   :						Tuple    =							self.position
        if direction == Direction.DOWN:
              if self.position + 1 >= len(self.choices						):
                    return
              self.position += num_spaces
        else:
              if self.position - 1 < 0:
                    return
              self.position -= num_spaces
        clear_line()
        self.print_choice(__a						)
        move_cursor(__a       , direction.name						)
        self.print_choice(self.position						)

  @input.mark(KEYMAP["up"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Union[str, Any]						)		->					Any:
        self.move_direction(Direction.UP						)

  @input.mark(KEYMAP["down"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Dict						)		->					List[Any]:
        self.move_direction(Direction.DOWN						)

  @input.mark(KEYMAP["newline"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							int						)		->					str:
        move_cursor(len(self.choices						) - self.position       , "DOWN"						)
        return self.position

  @input.mark(KEYMAP["interrupt"]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[Any]						)		->					Tuple:
        move_cursor(len(self.choices						) - self.position       , "DOWN"						)
        raise KeyboardInterrupt

  @input.mark_multiple(*[KEYMAP[str(__a						)] for number in range(10						)]						)
  def  __SCREAMING_SNAKE_CASE					(		self :							Optional[int]						)		->					List[str]:
        _UpperCamelCase   :						Union[str, Any]    =							int(chr(self.current_selection						)						)
        _UpperCamelCase   :						List[str]    =							index - self.position
        if index == self.position:
              return
        if index < len(self.choices						):
              if self.position > index:
                    self.move_direction(Direction.UP       , -movement						)
              elif self.position < index:
                    self.move_direction(Direction.DOWN       , __a						)
              else:
                    return
        else:
              return


  def  __SCREAMING_SNAKE_CASE					(		self :							List[Any]       , __a :							int = 0						)		->					List[Any]:
        if self.prompt:
              linebreak()
              forceWrite(self.prompt       , "\n"						)
              if in_colab:
                    forceWrite("Please input a choice index (starting from 0), and press enter"       , "\n"						)
              else:
                    forceWrite("Please select a choice using the arrow or number keys, and selecting with enter"       , "\n"						)
        _UpperCamelCase   :						int    =							default_choice
        for i in range(len(self.choices						)						):
              self.print_choice(__a						)
              forceWrite("\n"						)
        move_cursor(len(self.choices						) - self.position       , "UP"						)
        with cursor.hide():
              while True:
                    if in_colab:
                          try:
                                _UpperCamelCase   :						Union[str, Any]    =							int(builtins.input()						)
                          except ValueError:
                                _UpperCamelCase   :						int    =							default_choice
                    else:
                          _UpperCamelCase   :						Any    =							self.handle_input()
                    if choice is not None:
                          reset_cursor()
                          for _ in range(len(self.choices						) + 1						):
                                move_cursor(1       , "UP"						)
                                clear_line()
                          self.write_choice(__a       , "\n"						)
                          return choice
