
    IR-e                     V    d Z ddlZddlmZ ddlmZ d ZddZd Zd	 Z	d
 Z
d Zd ZdS )z,
Utilities shared by the different formats.
    N)maybe_simple_fractiondid_you_meanc                     g }g }t          | |          D ]P\  }}|dk     r|                    || f           $|dk    r|                    ||f           Bt          d          ||fS )a  
    Groups the powers and bases in the given
    `~astropy.units.CompositeUnit` into positive powers and
    negative powers for easy display on either side of a solidus.

    Parameters
    ----------
    bases : list of `astropy.units.UnitBase` instances

    powers : list of int

    Returns
    -------
    positives, negatives : tuple of lists
       Each element in each list is tuple of the form (*base*,
       *power*).  The negatives have the sign of their power reversed
       (i.e. the powers are all positive).
    r   zUnit with 0 power)zipappend
ValueError)basespowerspositivenegativebasepowers         :lib/python3.11/site-packages/astropy/units/format/utils.pyget_grouped_by_powersr      s    & HH5&)) 2 2e199OOTE6N++++QYYOOT5M****0111X    .8gc                    t          | |                              d          }|d         ddt          |d                   dz
  z  z   k    r	|d         }nd}t          |          dk    r[|d                             d          }t          |          dk    r,|d         d	k    r d	|dd
                             d          z   }nd}||fS )aq  
    Given a number, split it into its mantissa and base 10 exponent
    parts, each as strings.  If the exponent is too small, it may be
    returned as the empty string.

    Parameters
    ----------
    v : float

    format_spec : str, optional
        Number representation formatting string

    Returns
    -------
    mantissa, exponent : tuple of strings
    er   z1.0       z0+-N)formatsplitlenlstrip)vformat_specxmexs        r   split_mantissa_exponentr$   -   s    " 	q+$$S))AttcS1YY]++++aD
1vv{{qT[[r77Q;;2a5C<<r!""v}}S)))Bb5Lr   c                    ddl m} t          | |j                  rR|                    | j                  }t          | j        | j                  D ]\  }}|t          ||          |z  z  }|S t          | |j
                  rJ	  ||            n;# t          $ r. t          | |j                  rt          | j        |          cY S  w xY w| S t          dt          |                      )a  
    Partially decomposes a unit so it is only composed of units that
    are "known" to a given format.

    Parameters
    ----------
    unit : `~astropy.units.UnitBase` instance

    func : callable
        This function will be called to determine if a given unit is
        "known".  If the unit is not known, this function should raise a
        `ValueError`.

    Returns
    -------
    unit : `~astropy.units.UnitBase` instance
        A flattened unit.
    r   )corez<unit argument must be a 'NamedUnit' or 'CompositeUnit', not )astropy.unitsr&   
isinstanceCompositeUnitUnitscaler   r
   r   decompose_to_known_units	NamedUnitr	   _represents	TypeErrortype)unitfuncr&   new_unitr   r   s         r   r,   r,   N   s!   & #"""""$*++ 
99TZ((tz4;77 	P 	PKD%":4"F"F%"OOHH	D$.	)	) 
	DJJJJ 	 	 	$	** H/0@$GGGGG	 W4PT::WW
 
 	
s   B 4CCc                     t          | d          s+t          |           } t          | dd          dk    r| j        } t	          |           S )z
    Converts a value for a power (which may be floating point or a
    `fractions.Fraction` object), into a string looking like either
    an integer or a fraction, if the power is close to that.
    denominatordenonimatorNr   )hasattrr   getattr	numeratorstr)r   s    r   format_powerr;   v   sM     5-(( $%e,,5-..!33OEu::r   c                     t          | dd           }|	  ||          }|S # t          $ r Y nw xY w|                                 }|| ur	  ||          }|S # t          $ r Y nw xY wd S )Nr.   )r8   r	   	decompose)r1   format_decomposed
representsrepresents_string
decomposeddecompose_strings         r   _try_decomposedrC      s    }d33J	% 1 1* = = %$  	 	 	D	
 !!J	$00<< $#  	 	 	D	
 4s   " 
//A 
A%$A%c                 :    fd}t          | |          S )ac  
    A wrapper around `astropy.utils.misc.did_you_mean` that deals with
    the display of deprecated units.

    Parameters
    ----------
    s : str
        The invalid unit string

    all_units : dict
        A mapping from valid unit names to unit objects.

    deprecated_units : sequence
        The deprecated unit names

    format_decomposed : callable
        A function to turn a decomposed version of the unit into a
        string.  Should return `None` if not possible

    Returns
    -------
    msg : str
        A string message with a list of alternatives, or the empty
        string.
    c                 |    | v r5| dz   g}t          |                    }||                    |           |S | fS )Nz (deprecated))rC   r   )r!   resultsrA   	all_unitsdeprecated_unitsr>   s      r   fix_deprecatedz*did_you_mean_units.<locals>.fix_deprecated   sT       ?*+G(17HIIJ%z***Ntr   )fixr   )srG   rH   r>   rI   s    ``` r   did_you_mean_unitsrL      s?    6       9.9999r   c                     ddl m} d|  d| d}t          ||          }|	|d| dz  }t          j        ||           dS )	a  
    Raises a UnitsWarning about a deprecated unit in a given format.
    Suggests a decomposed alternative if one is available.

    Parameters
    ----------
    s : str
        The deprecated unit name.

    unit : astropy.units.core.UnitBase
        The unit object.

    standard_name : str
        The name of the format for which the unit is deprecated.

    format_decomposed : callable
        A function to turn a decomposed version of the unit into a
        string.  Should return `None` if not possible
    r   )UnitsWarningz
The unit 'z' has been deprecated in the z
 standard.Nz Suggested: .)astropy.units.corerN   rC   warningswarn)rK   r1   standard_namer>   rN   messagerA   s          r   unit_deprecation_warningrU      sr    ( 0/////T1TT=TTTG '899J/*////M'<(((((r   )r   )__doc__rQ   astropy.units.utilsr   astropy.utils.miscr   r   r$   r,   r;   rC   rL   rU    r   r   <module>rZ      s    
  5 5 5 5 5 5 + + + + + +  >   B%
 %
 %
P    ,$: $: $:N) ) ) ) )r   