o
    ]i                     @   s>   d Z ddlZddlZddlmZ G dd dejZdd ZdS )z4Custom parser to override tuples in the config dict.    N)flagsc                   @   s    e Zd ZdZdd Zdd ZdS )TupleParseraJ  Parser for tuple arguments.

  Custom flag parser for Tuple objects that is based on the existing parsers
  in `absl.flags`. This parser can be used to read in and override tuple
  arguments. It outputs a `tuple` object from an existing `tuple` or `str`.
  The ony requirement is that the overriding parameter should be a `tuple`
  as well. The overriding parameter can have a different number of elements
  of different type than the original. For a detailed list of what `str`
  arguments are supported for overriding, look at `ast.literal_eval` from the
  Python Standard Library.
  c                 C   s2   t |tr|S t |trt|S |du rdS |fS )a  Returns a `tuple` representing the input `argument`.

    Args:
      argument: The argument to be parsed. Valid types are `tuple` and
        `str` or a single object. `str` arguments are parsed and converted to a
        tuple, a single object is converted to a tuple of length 1, and an empty
        `tuple` is returned for arguments `NoneType`. 

    Returns:
      A `TupleType` representing the input argument as a `tuple`.
    N )
isinstancetuplestr_convert_str_to_tuple)selfargumentr   r   \/home/ubuntu/.local/lib/python3.10/site-packages/ml_collections/config_flags/tuple_parser.pyparse#   s   

zTupleParser.parsec                 C   s   dS )Nr   r   )r	   r   r   r   	flag_type8   s   zTupleParser.flag_typeN)__name__
__module____qualname____doc__r   r   r   r   r   r   r      s    r   c              
   C   s   zt | }W nA ty   | }Y n7 tyH } z+d| vr$d| vr$| }nd|  dtt|  d}d| v r9|d7 }t||W Y d}~nd}~ww t|trP|S t|tjj	r`t|t
s`t|S |fS )zFunction to convert a Python `str` object to a `tuple`.

  Args:
    string: The `str` to be converted.

  Returns:
    A `tuple` version of the string.

  Raises:
    ValueError: If the string is not a well formed `tuple`.
  , zError while parsing string: z` as tuple. If you intended to pass the argument as a single element, use quotes such as `--flag z., otherwise insert quotes around each element.zD Use commas instead of whitespace as the separator between elements.N)astliteral_eval
ValueErrorSyntaxErrorreprr   r   collectionsabcIterabler   )stringvalueexcmsgr   r   r   r   <   s4   


r   )	r   r   collections.abcr   abslr   ArgumentParserr   r   r   r   r   r   <module>   s   &