






import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
_a    :   Tuple        =			sys.version_info >= (3, 10)


def 				snake_case__							(							UpperCAmelCase							:  Tuple=None    ,			UpperCAmelCase							:  int=None       ):
    return field(default_factory=lambda: default    ,			metadata=UpperCAmelCase       )







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    42
      A          =    42
      A          =    42
      A          =    42







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    42
      A          =    field(default='''toto'''				,							metadata={'''help''': '''help message'''}		)







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    False
      A          =    True
      A          =    None







class 	_UpperCAmelCase  (  _A		):




      """simple docstring"""



      A          =    '''titi'''
      A          =    '''toto'''







class 	_UpperCAmelCase  (  _A		):




      """simple docstring"""



      A          =    '''titi'''
      A          =    '''toto'''
      A          =    42







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    "toto"
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				BasicEnum(self.foo		)







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    "toto"
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				MixedTypeEnum(self.foo		)







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    None
      A          =    field(default=_A				,							metadata={'''help''': '''help message'''}		)
      A          =    None
      A          =    list_field(default=[]		)
      A          =    list_field(default=[]		)







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    list_field(default=[]		)
      A          =    list_field(default=[1, 2, 3]		)
      A          =    list_field(default=['''Hallo''', '''Bonjour''', '''Hello''']		)
      A          =    list_field(default=[0.1, 0.2, 0.3]		)







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    field()
      A          =    field()
      A          =    field()
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				BasicEnum(self.required_enum		)







@dataclass
class 	_UpperCAmelCase  :




      """simple docstring"""



      A          =    42
      A          =    field()
      A          =    None
      A          =    field(default='''toto'''				,							metadata={'''help''': '''help message'''}		)
      A          =    list_field(default=['''Hallo''', '''Bonjour''', '''Hello''']		)


if is_python_no_less_than_3_10:







 @dataclass
 class 	_UpperCAmelCase  :




       """simple docstring"""



       A          =    False
       A          =    True
       A          =    None







 @dataclass
 class 	_UpperCAmelCase  :




       """simple docstring"""



       A          =    None
       A          =    field(default=_A				,							metadata={'''help''': '''help message'''}		)
       A          =    None
       A          =    list_field(default=[]		)
       A          =    list_field(default=[]		)







class 	_UpperCAmelCase  (  unittest.TestCase		):




      """simple docstring"""



      def 			snake_case_      (   self    ,	_lowerCAmelCase    ,	_lowerCAmelCase		):



          '''simple docstring'''
          self.assertEqual(len(a._actions		)    ,	len(b._actions		)		)
          for x, y in zip(a._actions    ,	b._actions		):
              lowerCAmelCase__   :Optional[int]      							=				{k: v for k, v in vars(_lowerCAmelCase		).items() if k != "container"}
              lowerCAmelCase__   :int      							=				{k: v for k, v in vars(_lowerCAmelCase		).items() if k != "container"}

              # Choices with mixed type have custom function as "type"
              # So we need to compare results directly for equality
              if xx.get("choices"    ,	_lowerCAmelCase		) and yy.get("choices"    ,	_lowerCAmelCase		):
                  for expected_choice in yy["choices"] + xx["choices"]:
                      self.assertEqual(xx["type"](_lowerCAmelCase		)    ,	yy["type"](_lowerCAmelCase		)		)
                  del xx["type"], yy["type"]

              self.assertEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Tuple      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Dict      							=				argparse.ArgumentParser()
          expected.add_argument("--foo"    ,	type=_lowerCAmelCase    ,	required=_lowerCAmelCase		)
          expected.add_argument("--bar"    ,	type=_lowerCAmelCase    ,	required=_lowerCAmelCase		)
          expected.add_argument("--baz"    ,	type=_lowerCAmelCase    ,	required=_lowerCAmelCase		)
          expected.add_argument("--flag"    ,	type=_lowerCAmelCase    ,	default=_lowerCAmelCase    ,	const=_lowerCAmelCase    ,	nargs="?"		)
          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[Any]      							=				["--foo", "1", "--baz", "quux", "--bar", "0.5"]
          ((lowerCAmelCase__)							,)   :Any      							=				parser.parse_args_into_dataclasses(_lowerCAmelCase    ,	look_for_args_file=_lowerCAmelCase		)
          self.assertFalse(example.flag		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Dict      							=				argparse.ArgumentParser()
          expected.add_argument("--foo"    ,	default=42    ,	type=_lowerCAmelCase		)
          expected.add_argument("--baz"    ,	default="toto"    ,	type=_lowerCAmelCase    ,	help="help message"		)
          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				argparse.ArgumentParser()
          expected.add_argument("--foo"    ,	type=_lowerCAmelCase    ,	default=_lowerCAmelCase    ,	const=_lowerCAmelCase    ,	nargs="?"		)
          expected.add_argument("--baz"    ,	type=_lowerCAmelCase    ,	default=_lowerCAmelCase    ,	const=_lowerCAmelCase    ,	nargs="?"		)
          # A boolean no_* argument always has to come after its "default: True" regular counter-part
          # and its default must be set to False
          expected.add_argument("--no_baz"    ,	action="store_false"    ,	default=_lowerCAmelCase    ,	dest="baz"		)
          expected.add_argument("--opt"    ,	type=_lowerCAmelCase    ,	default=_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[Any]      							=				[WithDefaultBoolExample]
          if is_python_no_less_than_3_10:
              dataclass_types.append(_lowerCAmelCase		)

          for dataclass_type in dataclass_types:
              lowerCAmelCase__   :List[str]      							=				HfArgumentParser(_lowerCAmelCase		)
              self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)

              lowerCAmelCase__   :int      							=				parser.parse_args([]		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=_lowerCAmelCase    ,	baz=_lowerCAmelCase    ,	opt=_lowerCAmelCase		)		)

              lowerCAmelCase__   :int      							=				parser.parse_args(["--foo", "--no_baz"]		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=_lowerCAmelCase    ,	baz=_lowerCAmelCase    ,	opt=_lowerCAmelCase		)		)

              lowerCAmelCase__   :Dict      							=				parser.parse_args(["--foo", "--baz"]		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=_lowerCAmelCase    ,	baz=_lowerCAmelCase    ,	opt=_lowerCAmelCase		)		)

              lowerCAmelCase__   :Optional[int]      							=				parser.parse_args(["--foo", "True", "--baz", "True", "--opt", "True"]		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=_lowerCAmelCase    ,	baz=_lowerCAmelCase    ,	opt=_lowerCAmelCase		)		)

              lowerCAmelCase__   :Optional[int]      							=				parser.parse_args(["--foo", "False", "--baz", "False", "--opt", "False"]		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=_lowerCAmelCase    ,	baz=_lowerCAmelCase    ,	opt=_lowerCAmelCase		)		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Any      							=				argparse.ArgumentParser()
          expected.add_argument(
              "--foo"    ,	default="toto"    ,	choices=["titi", "toto", 42]    ,	type=make_choice_type_function(["titi", "toto", 42]		)    ,	)
          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)

          lowerCAmelCase__   :Dict      							=				parser.parse_args([]		)
          self.assertEqual(args.foo    ,	"toto"		)
          lowerCAmelCase__   :Optional[int]      							=				parser.parse_args_into_dataclasses([]		)[0]
          self.assertEqual(enum_ex.foo    ,	MixedTypeEnum.toto		)

          lowerCAmelCase__   :Tuple      							=				parser.parse_args(["--foo", "titi"]		)
          self.assertEqual(args.foo    ,	"titi"		)
          lowerCAmelCase__   :Union[str, Any]      							=				parser.parse_args_into_dataclasses(["--foo", "titi"]		)[0]
          self.assertEqual(enum_ex.foo    ,	MixedTypeEnum.titi		)

          lowerCAmelCase__   :List[str]      							=				parser.parse_args(["--foo", "42"]		)
          self.assertEqual(args.foo    ,	42		)
          lowerCAmelCase__   :Optional[Any]      							=				parser.parse_args_into_dataclasses(["--foo", "42"]		)[0]
          self.assertEqual(enum_ex.foo    ,	MixedTypeEnum.fourtytwo		)
      def 			snake_case_      (   self		):



          '''simple docstring'''







          @dataclass
          class 	_UpperCAmelCase  :




                """simple docstring"""



                A          =    "toto"





          lowerCAmelCase__   :Union[str, Any]      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Dict      							=				argparse.ArgumentParser()
          expected.add_argument(
              "--foo"    ,	default="toto"    ,	choices=("titi", "toto", 42)    ,	type=make_choice_type_function(["titi", "toto", 42]		)    ,	)
          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)

          lowerCAmelCase__   :Any      							=				parser.parse_args([]		)
          self.assertEqual(args.foo    ,	"toto"		)

          lowerCAmelCase__   :int      							=				parser.parse_args(["--foo", "titi"]		)
          self.assertEqual(args.foo    ,	"titi"		)

          lowerCAmelCase__   :Tuple      							=				parser.parse_args(["--foo", "42"]		)
          self.assertEqual(args.foo    ,	42		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[Any]      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[Any]      							=				argparse.ArgumentParser()
          expected.add_argument("--foo_int"    ,	nargs="+"    ,	default=[]    ,	type=_lowerCAmelCase		)
          expected.add_argument("--bar_int"    ,	nargs="+"    ,	default=[1, 2, 3]    ,	type=_lowerCAmelCase		)
          expected.add_argument("--foo_str"    ,	nargs="+"    ,	default=["Hallo", "Bonjour", "Hello"]    ,	type=_lowerCAmelCase		)
          expected.add_argument("--foo_float"    ,	nargs="+"    ,	default=[0.1, 0.2, 0.3]    ,	type=_lowerCAmelCase		)

          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)

          lowerCAmelCase__   :Any      							=				parser.parse_args([]		)
          self.assertEqual(
              _lowerCAmelCase    ,	Namespace(foo_int=[]    ,	bar_int=[1, 2, 3]    ,	foo_str=["Hallo", "Bonjour", "Hello"]    ,	foo_float=[0.1, 0.2, 0.3]		)    ,	)

          lowerCAmelCase__   :Dict      							=				parser.parse_args("--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7".split()		)
          self.assertEqual(_lowerCAmelCase    ,	Namespace(foo_int=[1]    ,	bar_int=[2, 3]    ,	foo_str=["a", "b", "c"]    ,	foo_float=[0.1, 0.7]		)		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Any      							=				argparse.ArgumentParser()
          expected.add_argument("--foo"    ,	default=_lowerCAmelCase    ,	type=_lowerCAmelCase		)
          expected.add_argument("--bar"    ,	default=_lowerCAmelCase    ,	type=_lowerCAmelCase    ,	help="help message"		)
          expected.add_argument("--baz"    ,	default=_lowerCAmelCase    ,	type=_lowerCAmelCase		)
          expected.add_argument("--ces"    ,	nargs="+"    ,	default=[]    ,	type=_lowerCAmelCase		)
          expected.add_argument("--des"    ,	nargs="+"    ,	default=[]    ,	type=_lowerCAmelCase		)

          lowerCAmelCase__   :Tuple      							=				[OptionalExample]
          if is_python_no_less_than_3_10:
              dataclass_types.append(_lowerCAmelCase		)

          for dataclass_type in dataclass_types:
              lowerCAmelCase__   :str      							=				HfArgumentParser(_lowerCAmelCase		)

              self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)

              lowerCAmelCase__   :List[Any]      							=				parser.parse_args([]		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=_lowerCAmelCase    ,	bar=_lowerCAmelCase    ,	baz=_lowerCAmelCase    ,	ces=[]    ,	des=[]		)		)

              lowerCAmelCase__   :List[Any]      							=				parser.parse_args("--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3".split()		)
              self.assertEqual(_lowerCAmelCase    ,	Namespace(foo=12    ,	bar=3.14    ,	baz="42"    ,	ces=["a", "b", "c"]    ,	des=[1, 2, 3]		)		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[int]      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[int]      							=				argparse.ArgumentParser()
          expected.add_argument("--required_list"    ,	nargs="+"    ,	type=_lowerCAmelCase    ,	required=_lowerCAmelCase		)
          expected.add_argument("--required_str"    ,	type=_lowerCAmelCase    ,	required=_lowerCAmelCase		)
          expected.add_argument(
              "--required_enum"    ,	type=make_choice_type_function(["titi", "toto"]		)    ,	choices=["titi", "toto"]    ,	required=_lowerCAmelCase    ,	)
          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Optional[Any]      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :List[Any]      							=				argparse.ArgumentParser()
          expected.add_argument("--foo"    ,	type=_lowerCAmelCase    ,	required=_lowerCAmelCase		)
          expected.add_argument(
              "--required_enum"    ,	type=make_choice_type_function(["titi", "toto"]		)    ,	choices=["titi", "toto"]    ,	required=_lowerCAmelCase    ,	)
          expected.add_argument("--opt"    ,	type=_lowerCAmelCase    ,	default=_lowerCAmelCase		)
          expected.add_argument("--baz"    ,	default="toto"    ,	type=_lowerCAmelCase    ,	help="help message"		)
          expected.add_argument("--foo_str"    ,	nargs="+"    ,	default=["Hallo", "Bonjour", "Hello"]    ,	type=_lowerCAmelCase		)
          self.argparsersEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :List[str]      							=				{
              "foo": 12,
              "bar": 3.14,
              "baz": "42",
              "flag": True,
          }

          lowerCAmelCase__   :Tuple      							=				parser.parse_dict(_lowerCAmelCase		)[0]
          lowerCAmelCase__   :List[Any]      							=				BasicExample(**_lowerCAmelCase		)
          self.assertEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Any      							=				{
              "foo": 12,
              "bar": 3.14,
              "baz": "42",
              "flag": True,
              "extra": 42,
          }

          self.assertRaises(_lowerCAmelCase    ,	parser.parse_dict    ,	_lowerCAmelCase    ,	allow_extra_keys=_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :str      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :Optional[int]      							=				{
              "foo": 12,
              "bar": 3.14,
              "baz": "42",
              "flag": True,
          }
          with tempfile.TemporaryDirectory() as tmp_dir:
              lowerCAmelCase__   :Tuple      							=				os.path.join(_lowerCAmelCase    ,	"temp_json"		)
              os.mkdir(_lowerCAmelCase		)
              with open(temp_local_path + ".json"    ,	"w+"		) as f:
                  json.dump(_lowerCAmelCase    ,	_lowerCAmelCase		)
              lowerCAmelCase__   :Optional[Any]      							=				parser.parse_yaml_file(Path(temp_local_path + ".json"		)		)[0]

          lowerCAmelCase__   :Union[str, Any]      							=				BasicExample(**_lowerCAmelCase		)
          self.assertEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)
      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :Dict      							=				HfArgumentParser(_lowerCAmelCase		)

          lowerCAmelCase__   :List[str]      							=				{
              "foo": 12,
              "bar": 3.14,
              "baz": "42",
              "flag": True,
          }
          with tempfile.TemporaryDirectory() as tmp_dir:
              lowerCAmelCase__   :List[str]      							=				os.path.join(_lowerCAmelCase    ,	"temp_yaml"		)
              os.mkdir(_lowerCAmelCase		)
              with open(temp_local_path + ".yaml"    ,	"w+"		) as f:
                  yaml.dump(_lowerCAmelCase    ,	_lowerCAmelCase		)
              lowerCAmelCase__   :List[Any]      							=				parser.parse_yaml_file(Path(temp_local_path + ".yaml"		)		)[0]
          lowerCAmelCase__   :List[Any]      							=				BasicExample(**_lowerCAmelCase		)
          self.assertEqual(_lowerCAmelCase    ,	_lowerCAmelCase		)



      def 			snake_case_      (   self		):



          '''simple docstring'''
          lowerCAmelCase__   :List[str]      							=				HfArgumentParser(_lowerCAmelCase		)
          self.assertIsNotNone(_lowerCAmelCase		)











from collections.abc import Callable
from math import pi, sqrt
from random import uniform
from statistics import mean


def 				snake_case__							(							UpperCAmelCase							:  int       ):

    # A local function to see if a dot lands in the circle.
    def is_in_circle(UpperCAmelCase							:  float    ,			UpperCAmelCase							:  float       ) -> bool:
        lowerCAmelCase__   :Optional[int]      							=				sqrt((x**2) + (y**2)       )
        # Our circle has a radius of 1, so a distance
        # greater than 1 would land outside the circle.
        return distance_from_centre <= 1

    # The proportion of guesses that landed in the circle
    lowerCAmelCase__   :Union[str, Any]      							=				mean(
        int(is_in_circle(uniform(-1.0    ,			1.0       )    ,			uniform(-1.0    ,			1.0       )       )       )
        for _ in range(UpperCAmelCase       )       )
    # The ratio of the area for circle to square is pi/4.
    lowerCAmelCase__   :List[Any]      							=				proportion * 4
    print(F'''The estimated value of pi is {pi_estimate}'''       )
    print(F'''The numpy value of pi is {pi}'''       )
    print(F'''The total error is {abs(pi - pi_estimate       )}'''       )


def 				snake_case__							(							UpperCAmelCase							:  int    ,			UpperCAmelCase							:  Callable[[float], float]    ,			UpperCAmelCase							:  float = 0.0    ,			UpperCAmelCase							:  float = 1.0    ,			):
    return mean(
        function_to_integrate(uniform(UpperCAmelCase    ,			UpperCAmelCase       )       ) for _ in range(UpperCAmelCase       )       ) * (max_value - min_value)


def 				snake_case__							(							UpperCAmelCase							:  int    ,			UpperCAmelCase							:  float = 0.0    ,			UpperCAmelCase							:  float = 1.0       ):

    def identity_function(UpperCAmelCase							:  float       ) -> float:
        return x

    lowerCAmelCase__   :List[str]      							=				area_under_curve_estimator(
        UpperCAmelCase    ,			UpperCAmelCase    ,			UpperCAmelCase    ,			UpperCAmelCase       )
    lowerCAmelCase__   :Tuple      							=				(max_value * max_value - min_value * min_value) / 2

    print("******************"       )
    print(F'''Estimating area under y=x where x varies from {min_value} to {max_value}'''       )
    print(F'''Estimated value is {estimated_value}'''       )
    print(F'''Expected value is {expected_value}'''       )
    print(F'''Total error is {abs(estimated_value - expected_value       )}'''       )
    print("******************"       )


def 				snake_case__							(							UpperCAmelCase							:  int       ):

    def function_to_integrate(UpperCAmelCase							:  float       ) -> float:
        return sqrt(4.0 - x * x       )

    lowerCAmelCase__   :int      							=				area_under_curve_estimator(
        UpperCAmelCase    ,			UpperCAmelCase    ,			0.0    ,			2.0       )

    print("******************"       )
    print("Estimating pi using area_under_curve_estimator"       )
    print(F'''Estimated value is {estimated_value}'''       )
    print(F'''Expected value is {pi}'''       )
    print(F'''Total error is {abs(estimated_value - pi       )}'''       )
    print("******************"       )


if __name__ == "__main__":
 import doctest

 doctest.testmod()



