U
    e»{j�  ã                   @   sb   d Z ddlZddlmZ ddlmZ dd„ Zdd	d
„Zdd„ Zdd„ Z	dd„ Z
dd„ Zdd„ ZdS )z,
Utilities shared by the different formats.
é    N©Údid_you_meané   )Úmaybe_simple_fractionc                 C   s^   g }g }t | |ƒD ]B\}}|dk r4| || f¡ q|dkrL| ||f¡ qtdƒ‚q||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ÚpowersZpositiveÚnegativeÚbaseÚpower© r   úY/var/www/html/bwave/python/venv/lib/python3.8/site-packages/astropy/units/format/utils.pyÚget_grouped_by_powers   s    
r   ú.8gc                 C   s”   t | |ƒ d¡}|d ddt|d ƒd   kr:|d }nd}t|ƒdkrˆ|d  d¡}t|ƒdkrŒ|d d	krŒd	|dd
…  d¡ }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.Ú0r   Ú é   z0+ú-N)ÚformatÚsplitÚlenÚlstrip)ÚvÚformat_specÚxÚmÚexr   r   r   Úsplit_mantissa_exponent.   s     
r    c                 C   s¸   ddl m} t| |jƒrR| | j¡}t| j| jƒD ]\}}|t	||ƒ|  }q2|S t| |j
ƒr¢z|| ƒ W n2 tk
rœ   t| |jƒr–t	| j|ƒ Y S ‚ Y nX | S tdt| ƒ› �ƒ‚dS )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 N)Zastropy.unitsr!   Ú
isinstanceZCompositeUnitZUnitÚscaler   r	   r
   Údecompose_to_known_unitsZ	NamedUnitr   Ú_representsÚ	TypeErrorÚtype)ÚunitÚfuncr!   Znew_unitr   r   r   r   r   r$   O   s"    ÿr$   c                 C   s0   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.
    ÚdenominatorZdenonimatorNr   )Úhasattrr   ÚgetattrZ	nominatorÚstr)r   r   r   r   Úformat_powerw   s
    
r.   c                 C   st   t | dd ƒ}|d k	r:z||ƒ}W n tk
r4   Y nX |S |  ¡ }|| k	rpz||ƒ}W n tk
rj   Y nX |S d S )Nr%   )r,   r   Z	decompose)r(   Úformat_decomposedZ
representsZrepresents_stringÚ
decomposedZdecompose_stringr   r   r   Ú_try_decomposed…   s    r1   c                    s   ‡ ‡‡fdd„}t | ˆ |d�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                    s<   | ˆkr6| d g}t ˆ |  ˆƒ}|d k	r2| |¡ |S | fS )Nz (deprecated))r1   r   )r   Úresultsr0   ©Ú	all_unitsÚdeprecated_unitsr/   r   r   Úfix_deprecated¶   s    

z*did_you_mean_units.<locals>.fix_deprecated)Zfixr   )Úsr4   r5   r/   r6   r   r3   r   Údid_you_mean_units›   s    	r8   c                 C   sP   ddl m} d| › d|› d�}t||ƒ}|dk	r@|d|› d�7 }t ||¡ 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: Ú.)Zastropy.units.corer9   r1   ÚwarningsÚwarn)r7   r(   Zstandard_namer/   r9   Úmessager0   r   r   r   Úunit_deprecation_warningÂ   s    
r>   )r   )Ú__doc__r;   Zastropy.utils.miscr   Úutilsr   r   r    r$   r.   r1   r8   r>   r   r   r   r   Ú<module>   s   
!('