
[c           @` s   d  Z  d d l m Z m Z m Z m Z d d l Z d d l m Z d d l	 m
 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(   u,   
Utilities shared by the different formats.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   Fractioni   (   t   six(   t   zip(   t   did_you_meanc         C` s   g  } g  } xn t  |  |  D]] \ } } | d k  rK | j | | f  q | d k rm | j | | f  q t d   q W| | f S(   u  
    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 ints

    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).
    i    u   Unit with 0 power(   R   t   appendt
   ValueError(   t   basest   powerst   positivet   negativet   baset   power(    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   get_grouped_by_powers   s    u   .8gc         C` s   t  |  |  j d  } | d d d t | d  d k rK | d } n d } t |  d k r | d j d  } t |  d k r | d d	 k r d	 | d j d  } q n d } | | f S(
   uq  
    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
    u   ei    u   1.u   0i   u    i   u   0+u   -(   t   formatt   splitt   lent   lstrip(   t   vt   format_spect   xt   mt   ex(    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   split_mantissa_exponent1   s    &"c         C` s   d d l  m } t |  | j  ru | j |  j  } x: t |  j |  j  D]# \ } } | t	 | |  | } qJ W| St |  | j
  r y | |   Wn6 t k
 r t |  | j  r t	 |  j |  S  n X|  Sd S(   u  
    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.
    i   (   t   coreN(   t    R   t
   isinstancet   CompositeUnitt   Unitt   scaleR   R
   R   t   decompose_to_known_unitst	   NamedUnitR	   t   _represents(   t   unitt   funcR   t   new_unitR   R   (    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyR!   R   s    "c         C` s}   t  |  t  sp |  d d k ra t j |   } | j d  }  |  j d k rm t |  j  }  qm qp t |   }  n  t j |   S(   u   
    Converts a value for a power (which may be floating point or a
    `fractions.Fraction` object), into a string either looking like
    an integer or a fraction.
    g      ?g        i
   i   (	   R   R   t
   from_floatt   limit_denominatort   denominatort   intt	   numeratorR   t	   text_type(   R   t   frac(    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   format_poweru   s    c         C` s   t  |  d d   } | d  k	 rI y | |  } Wn t k
 rA qI X| Sn  |  j   } | |  k	 r y | |  } Wn t k
 r q X| Sn  d  S(   Nu   _represents(   t   getattrt   NoneR	   t	   decompose(   R$   t   format_decomposedt
   representst   represents_stringt
   decomposedt   decompose_string(    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   _try_decomposed   s    c         ` s(       f d   } t  |    d | S(   uc  
    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         ` sS   |   k rL |  d g } t    |    } | d  k	 rH | j |  n  | S|  f S(   Nu    (deprecated)(   R7   R0   R   (   R   t   resultsR5   (   t	   all_unitst   deprecated_unitsR2   (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   fix_deprecated   s    t   fix(   R   (   t   sR9   R:   R2   R;   (    (   R9   R:   R2   s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   did_you_mean_units   s    
c         C` sg   d d l  m } d j |  |  } t | |  } | d k	 rS | d j |  7} n  t j | |  d S(   u  
    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
    i   (   t   UnitsWarningu7   The unit '{0}' has been deprecated in the {1} standard.u    Suggested: {0}.N(   R   R?   R   R7   R0   t   warningst   warn(   R=   R$   t   standard_nameR2   R?   t   messageR5   (    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   unit_deprecation_warning   s    (   t   __doc__t
   __future__R    R   R   R   R@   t	   fractionsR   t   externR   t   extern.six.movesR   t
   utils.miscR   R   R   R!   R.   R7   R>   RD   (    (    (    s9   lib/python2.7/site-packages/astropy/units/format/utils.pyt   <module>   s   "	!	#			'