
    BVh@                      j    d Z ddlZddlZddlZddlZ G d de      Z G d dej                  e      Z	y)zRun doctests for tensorflow.    Nc                   v    e Zd ZdZ ej
                  dj                  dddd      ej                        Zd Z	y	)
_FloatExtractorzClass for extracting floats from a string.

  For example:

  >>> text_parts, floats = _FloatExtractor()("Text 1.0 Text")
  >>> text_parts
  ["Text ", " Text"]
  >>> floats
  np.array([1.0])
  a  
      (                          # Captures the float value.
        (?:
           [-+]|                 # Start with a sign is okay anywhere.
           (?:                   # Otherwise:
               ^|                # Start after the start of string
               (?<=[^\w.])       # Not after a word char, or a .
           )
        )
        (?:                      # Digits and exponent - something like:
          {digits_dot_maybe_digits}{exponent}?|   # "1.0" "1." "1.0e3", "1.e3"
          {dot_digits}{exponent}?|                # ".1" ".1e3"
          {digits}{exponent}|                     # "1e3"
          {digits}(?=j)                           # "300j"
        )
      )
      j?                         # Optional j for cplx numbers, not captured.
      (?=                        # Only accept the match if
        $|                       # * At the end of the string, or
        [^\w.]                   # * Next char is not a word char or "."
      )
      z(?:[0-9]+\.(?:[0-9]*))z(?:\.[0-9]+)z
(?:[0-9]+)z(?:[eE][-+]?[0-9]+))digits_dot_maybe_digits
dot_digitsdigitsexponentc                     g }g }t        | j                  j                  |            D ]9  \  }}|dz  dk(  r|j                  |        |j                  t	        |             ; |t        j                  |      fS )a  Extracts floats from a string.

    >>> text_parts, floats = _FloatExtractor()("Text 1.0 Text")
    >>> text_parts
    ["Text ", " Text"]
    >>> floats
    np.array([1.0])

    Args:
      string: the string to extract floats from.

    Returns:
      A (string, array) pair, where `string` has each float replaced by "..."
      and `array` is a `float32` `numpy.array` containing the extracted floats.
       r   )	enumerate	_FLOAT_REsplitappendfloatnparray)selfstringtextsfloatsiparts         T/home/dcms/DCMS/lib/python3.12/site-packages/tensorflow/tools/docs/tf_doctest_lib.py__call__z_FloatExtractor.__call__H   sr      EFT^^11&9: #4	
Q!TeDk"	# "((6"""    N)
__name__
__module____qualname____doc__recompileformatVERBOSEr   r    r   r   r   r      sM    	 bjj
* &";$ *  	+ 	jjA )D#r   r   c                        e Zd ZdZ fdZ ej                  d      Z ej                  dej                        Z	d
dZ
d Z ej                  d      Z fdZ fd	Z xZS )TfDoctestOutputCheckerzACustomizes how `want` and `got` are compared, see `check_output`.c                 f    t        t        | 
  |i | t               | _        d | _        d | _        y )N)superr%   __init__r   extract_floats	text_goodfloat_size_good)r   argskwargs	__class__s      r   r(   zTfDoctestOutputChecker.__init__f   s2    	
 $0$A&A)+DDNDr   z\bat 0x[0-9a-f]*?>z<tf.Tensor.*?numpy=(.*?)>c                 4    t        j                  ||||      S )N)rtolatol)r   allclose)r   wantgotr0   r1   s        r   	_allclosez TfDoctestOutputChecker._allcloseq   s    ;;tSt$77r   c                 H    | j                   j                  d|      }|||k7  fS )Nz\1)_NUMPY_OUTPUT_REsub)r   r   modified_strings      r   _tf_tensor_numpy_outputz.TfDoctestOutputChecker._tf_tensor_numpy_outputt   s*    ++//v>OOv555r   a  

        #############################################################
        Check the documentation (https://www.tensorflow.org/community/contribute/docs_ref) on how to
        write testable docstrings.
        #############################################################c                    |r|sy|d}||k(  ry| j                   j                  d|      }| j                  |      \  }}|r| j                  |      \  }}| j                  |      \  }| _        |D cg c]  }|j                  d       }}dj                  |      }d|v rt        j                  dd|      }| j                  |      \  }| _        t        t        | /  |||      | _        | j                  sy	| j                  j                  d
k(  ry| j                  j                  | j                  j                  k(  | _        | j                  r&| j                  | j                  | j                        S y	c c}w )a{  Compares the docstring output to the output gotten by running the code.

    Python addresses in the output are replaced with wildcards.

    Float values in the output compared as using `np.allclose`:

      * Float values are extracted from the text and replaced with wildcards.
      * The wildcard text is compared to the actual output.
      * The float values are compared using `np.allclose`.

    The method returns `True` if both the text comparison and the numeric
    comparison are successful.

    The numeric comparison will fail if either:

      * The wrong number of floats are found.
      * The float values are not within tolerence.

    Args:
      want: The output in the docstring.
      got: The output generated after running the snippet.
      optionflags: Flags passed to the doctest.

    Returns:
      A bool, indicating if the check was successful or not.
    T zat ...> z...z....z	\.\.\.\.+)r3   r4   optionflagsFr   )_ADDRESS_REr8   r:   r)   want_floatsstripjoinr   
got_floatsr'   r%   check_outputr*   sizer+   r5   )
r   r3   r4   r>   want_changed_want_text_partsr   want_text_wildr.   s
            r   rD   z#TfDoctestOutputChecker.check_output~   sx   > 4|ds{ 	40D 55d;D,++C0fc1 )-(;(;D(A%OT%
 4CC4tzz#COCZZ0N vvlE>Bn ,,S1At14E+ F ?DN>>!  ,,11T__5I5IID^^D,,doo>>3 Ds   4E:c                     |g}| j                   r| j                  s|j                  d       |j                  | j                         dj	                  |      }t
        t        |   |||      S )NzZ

CAUTION: tf_doctest doesn't work if *some* of the *float output* is hidden with a "...".
)r*   r+   r   MESSAGErB   r'   r%   output_difference)r   exampler4   r>   r.   s       r   rM   z(TfDoctestOutputChecker.output_difference   sj    %C ~~!!

 > 	? JJt||
))C.C(**13DFr   )MbP?rO   )r   r   r   r   r(   r   r    r?   DOTALLr7   r5   r:   textwrapdedentrL   rD   rM   __classcell__)r.   s   @r   r%   r%   c   sn    I  

01+  RZZ <biiH86 HOO I J'QfF Fr   r%   )
r   doctestr   rQ   numpyr   objectr   OutputCheckerr%   r#   r   r   <module>rX      s=    #  	  H#f H#V}FW22F }Fr   