B
    q\                 @   s   d Z ddlZddlZddlZddlmZ ddlZddlmZ ee	Z
e	dde
j  Ze	dde
j  Zdd Zd	d
 Zdd Zdd Zdd Zdd ZdddZdd ZdddZdS )z}
Miscellaneous utilities for `astropy.units`.

None of the functions in the module are meant for use outside of the
package.
    N)Fraction)finfog      ?g      @c             C   s*   t d| }|dk	r|d} | ddS )zS
    Get the first sentence from a string and remove any carriage
    returns.
    z	.*?\S\.\sNr   
 )rematchgroupreplace)sx r   2lib/python3.7/site-packages/astropy/units/utils.py_get_first_sentence   s    
r   c       
      c   s   ddl m} g }t }xZ|  D ]N\}}t||js6q ||jkrBq t||jrd||j	j
d j q || q W |jdd d xx|D ]p}t|j }d}t||jrd|j	d	dd
 }ddd |jD }	||||	|j|krdndfV  qW dS )z
    Generates the ``(unit, doc, represents, aliases, prefixes)``
    tuple used to format the unit summary docs in `generate_unit_summary`.
       )corer   c             S   s
   | j  S )N)namelower)r   r   r   r   <lambda>D   s    z$_iter_unit_summary.<locals>.<lambda>)key z:math:`{0}`Zlatexz, c             s   s   | ]}d  |V  qdS )z``{0}``N)format).0r   r   r   r   	<genexpr>L   s    z%_iter_unit_summary.<locals>.<genexpr>ZYesZNoN)r   r   setitems
isinstanceZUnitBaser   
PrefixUnitaddZ_representsbasesappendsortr   __doc__stripZUnitr   Z	to_stringjoinaliases)
	namespacer   ZunitsZhas_prefixesr   valunitdoc
representsr%   r   r   r   _iter_unit_summary'   s(    

r+   c             C   s<   t  }|d x t| D ]}|dj|  qW | S )a`  
    Generates a summary of units from a given namespace.  This is used
    to generate the docstring for the modules that define the actual
    units.

    Parameters
    ----------
    namespace : dict
        A namespace containing units.

    Returns
    -------
    docstring : str
        A docstring containing a summary table of the units.
    z
.. list-table:: Available Units
   :header-rows: 1
   :widths: 10 20 20 20 1

   * - Unit
     - Description
     - Represents
     - Aliases
     - SI Prefixes
z<
   * - ``{0}``
     - {1}
     - {2}
     - {3}
     - {4}
)ioStringIOwriter+   r   getvalue)r&   	docstringunit_summaryr   r   r   generate_unit_summaryQ   s    
r2   c             C   sx   ddl m} i }x4|  D ](\}}t||r|jjd }|||j< qW t }x t	|D ]}|
dj|  qXW | S )a  
    Generates table entries for units in a namespace that are just prefixes
    without the base unit.  Note that this is intended to be used *after*
    `generate_unit_summary` and therefore does not include the table header.

    Parameters
    ----------
    namespace : dict
        A namespace containing units that are prefixes but do *not* have the
        base unit in their namespace.

    Returns
    -------
    docstring : str
        A docstring containing a summary table of the units.
    r   )r   r   zS
   * - Prefixes for ``{0}``
     - {1} prefixes
     - {2}
     - {3}
     - Only
)r   r   r   r   r*   r   r   r,   r-   r+   r.   r   r/   )r&   r   Zfaux_namespaceZnmr(   Z	base_unitr0   r1   r   r   r    generate_prefixonly_unit_summary|   s    
r3   c             C   sf   yt |   kotkS   S  tk
r`   t | j  ko<tkn  o^t | jd   koZtkS   S X d S )Nr   )_JUST_BELOW_UNITY_JUST_ABOVE_UNITY	TypeErrorrealimag)valuer   r   r   is_effectively_unity   s
    r:   c             C   s~   t | rdS | jtkr| S | jrtt| jt| jkrPt | j| j d rp| jS n t | j| j d rptd| jS | S | jS d S )Ng      ?r   g        )r:   	__class__floatr8   absr7   complex)Zscaler   r   r   sanitize_scale   s    
r?   Fc             C   s   t | dd}|dkryt| } W n* tk
rJ   t| sDtdn Y nX | d dkrbt| } q| d d dkrtqxvdD ]@}| t| }|dtj  d dtj k rzt	tt
||} P qzW n,|d	krt| j} n||d	 @ d
krt| } | S )a  Convert a power to a floating point value, an integer, or a Fraction.

    If a fractional power can be represented exactly as a floating point
    number, convert it to a float, to make the math much faster; otherwise,
    retain it as a `fractions.Fraction` object to avoid losing precision.
    Conversely, if the value is indistinguishable from a rational number with a
    low-numbered denominator, convert to a Fraction object.

    Parameters
    ----------
    p : float, int, Rational, Fraction
        Power to be converted
    denominatorNz9Quantities and Units may only be raised to a scalar powerg      ?g        g       @)
   	         g      @r   r   )getattrr<   	Exceptionnpisscalar
ValueErrorint_float_finfoepsr   round	numerator)pZsupport_tuplesZdenomiZscaledr   r   r   validate_power   s.    



rQ   c             C   sf   | j tk	o| j tk	ot| t}|j tk	o:|j tk	o:t|t}|rN|sNt|}n|s^|r^t| } | |fS )z
    If either input is a Fraction, convert the other to a Fraction.
    This ensures that any operation involving a Fraction will use
    rational arithmetic and preserve precision.
    )r;   rJ   r<   r   r   )abZa_is_fractionZb_is_fractionr   r   r   resolve_fractions   s    


rT   c                sV   ddl m  t| tjsDt| sDt fdd| D rD | |dS tj| |dS d S )Nr   )Quantityc             3   s   | ]}t | V  qd S )N)r   )r   r   )rU   r   r   r     s    z&quantity_asanyarray.<locals>.<genexpr>)dtype)ZquantityrU   r   rG   ZndarrayrH   anyZ
asanyarray)rR   rV   r   )rU   r   quantity_asanyarray  s    ,rX   )F)N)r"   Znumbersr,   r   Z	fractionsr   ZnumpyrG   r   r<   rK   Zepsnegr4   rL   r5   r   r+   r2   r3   r:   r?   rQ   rT   rX   r   r   r   r   <module>   s$   *+'

6