U
    dy?                     @  s  U d Z ddlmZ ddlZddl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d	l
mZ dd
l
mZ ddl
mZmZ e	rejdkrddlmZ nddlmZ dZded< dddddZdFddddddZdGddddddZdd d D Zed!d!ed"d"ed#d#ed$d$ed%d%ed&d&ed'd'ed(d(ed)d)ed*d*ed+d+ed,d,fZdHdddd.d/d0Zdddd1d2Zdddd3d4Z dIdd6dd7d8d9Z!dJddd=d=dddd>d?d@Z"dKddd6ddCdDdEZ#dS )Lz!Humanizing functions for numbers.    )annotationsN)Fraction)TYPE_CHECKING   )_gettext)	_ngettext)_ngettext_noop)	_pgettext)decimal_separatorthousands_separator)   
   )	TypeAliaszfloat | strr   NumberOrStringfloatstr)valuereturnc                 C  s>   t | rdS t | r$| dk r$dS t | r:| dkr:dS dS )z2Utility function to handle infinite and nan cases.NaNr   z-Infz+Inf )mathisnanisinfr    r   3/tmp/pip-unpacked-wheel-kd4adnh7/humanize/number.py_format_not_finite   s    
r   male)r   genderr   c                 C  s.  z(t t| stt| W S t| } W n  ttfk
rH   t|  Y S X |dkrtddtddtddtdd	td
dtddtddtddtddtddf
}nTtddtddtddtdd	tddtddtddtddtddtddf
}| d dkr|  |d  S |  || d   S )aj  Converts an integer to its ordinal as a string.

    For example, 1 is "1st", 2 is "2nd", 3 is "3rd", etc. Works for any integer or
    anything `int()` will turn into an integer. Anything else will return the output
    of str(value).

    Examples:
        ```pycon
        >>> ordinal(1)
        '1st'
        >>> ordinal(1002)
        '1002nd'
        >>> ordinal(103)
        '103rd'
        >>> ordinal(4)
        '4th'
        >>> ordinal(12)
        '12th'
        >>> ordinal(101)
        '101st'
        >>> ordinal(111)
        '111th'
        >>> ordinal("something else")
        'something else'
        >>> ordinal([1, 2, 3]) == "[1, 2, 3]"
        True

        ```
    Args:
        value (int, str, float): Integer to convert.
        gender (str): Gender for translations. Accepts either "male" or "female".

    Returns:
        str: Ordinal string.
    r   z0 (male)thz1 (male)stz2 (male)Zndz3 (male)rdz4 (male)z5 (male)z6 (male)z7 (male)z8 (male)z9 (male)z
0 (female)z
1 (female)z
2 (female)z
3 (female)z
4 (female)z
5 (female)z
6 (female)z
7 (female)z
8 (female)z
9 (female)d   )         r   r   )	r   isfiniter   r   int	TypeError
ValueErrorr   P_)r   r   tr   r   r   ordinal(   s@    $r,   z
int | None)r   ndigitsr   c              	   C  s  t  }t }z~t| trd| |d|d} tt| sHtt| W S d| krZt| } qt	| } n$tt| stt| W S t|  W n  t
tfk
r   t|  Y S X |dk	rd| |}nt| }|d|}tdd| d|}||kr|S |}qdS )a  Converts an integer to a string containing commas every three digits.

    For example, 3000 becomes "3,000" and 45000 becomes "45,000". To maintain some
    compatibility with Django's `intcomma`, this function also accepts floats.

    Examples:
        ```pycon
        >>> intcomma(100)
        '100'
        >>> intcomma("1000")
        '1,000'
        >>> intcomma(1_000_000)
        '1,000,000'
        >>> intcomma(1_234_567.25)
        '1,234,567.25'
        >>> intcomma(1234.5454545, 2)
        '1,234.55'
        >>> intcomma(14308.40, 1)
        '14,308.4'
        >>> intcomma("14308.40", 1)
        '14,308.4'
        >>> intcomma(None)
        'None'

        ```
    Args:
        value (int, float, str): Integer or float to convert.
        ndigits (int, None): Digits of precision for rounding after the decimal point.

    Returns:
        str: String containing commas every three digits.
    r   .Nz	{0:.{1}f}z^(-?\d+)(\d{3})z\g<1>z\g<2>)r   r
   
isinstancer   replacer   r&   r   r   r'   r(   r)   formatresub)r   r-   thousands_sepZdecimal_seporignewr   r   r   intcommaq   s.    !


r7   c                 C  s   g | ]}d | qS )r   r   ).0xr   r   r   
<listcomp>   s     r:   )r      	   r$                     !   r"   ZthousandZmillionZbillionZtrillionZquadrillionZquintillionZ
sextillionZ
septillionZ	octillionZ	nonillionZ	decillionZgoogol%.1f)r   r1   r   c           	      C  sb  z(t t| stt| W S t| } W n  ttfk
rH   t|  Y S X | dk r`| d9 } d}nd}| td k r||t|  S t	tdd dD ]\}}| |k r| tt|d   }t| t|d   }t|| |kr| tt|  }t
| \}}|d|t||t |g |   S t
|d  \}}|d|t||t |g |   S q|t|  S )a  Converts a large integer to a friendly text representation.

    Works best for numbers over 1 million. For example, 1_000_000 becomes "1.0 million",
    1200000 becomes "1.2 million" and "1_200_000_000" becomes "1.2 billion". Supports up
    to decillion (33 digits) and googol (100 digits).

    Examples:
        ```pycon
        >>> intword("100")
        '100'
        >>> intword("12400")
        '12.4 thousand'
        >>> intword("1000000")
        '1.0 million'
        >>> intword(1_200_000_000)
        '1.2 billion'
        >>> intword(8100000000000000000000000000000000)
        '8.1 decillion'
        >>> intword(None)
        'None'
        >>> intword("1234000", "%0.3f")
        '1.234 million'

        ```
    Args:
        value (int, float, str): Integer to convert.
        format (str): To change the number of decimal or general format of the number
            portion.

    Returns:
        str: Friendly text representation as a string, unless the value passed could not
        be coaxed into an `int`.
    r   -r   r   N )r   r&   r   r   r'   r(   r)   r   powers	enumeratehuman_powersjoinr   ceil)	r   r1   Znegative_prefixordinal_powerZchoppedZpowers_differenceZsingularpluralr   r   r   intword   sB    "
rP   c                 C  s   z(t t| stt| W S t| } W n  ttfk
rH   t|  Y S X d|   kr^dk shn t| S tdtdtdtdtdtdtd	td
tdtdf
|  S )aA  Converts an integer to Associated Press style.

    Examples:
      ```pycon
      >>> apnumber(0)
      'zero'
      >>> apnumber(5)
      'five'
      >>> apnumber(10)
      '10'
      >>> apnumber("7")
      'seven'
      >>> apnumber("foo")
      'foo'
      >>> apnumber(None)
      'None'

      ```
    Args:
        value (int, float, str): Integer to convert.

    Returns:
        str: For numbers 0-9, the number spelled out. Otherwise, the number. This always
        returns a string unless the value was not `int`-able, then `str(value)`
        is returned.
    r   r   ZzeroZoneZtwoZthreeZfourZfivesixZsevenZeightZnine)	r   r&   r   r   r'   r(   r)   r   _r   r   r   r   apnumber
  s*    rS   c              	   C  s   z t | }t|st|W S W n  ttfk
r@   t|  Y S X t|}t|| 	d}|j
}|j}|r|s|dkr|dS |s|dd|dS |dd|dd|dS )a  Convert to fractional number.

    There will be some cases where one might not want to show ugly decimal places for
    floats and decimals.

    This function returns a human-readable fractional number in form of fractions and
    mixed fractions.

    Pass in a string, or a number or a float, and this function returns:

    * a string representation of a fraction
    * or a whole number
    * or a mixed fraction
    * or the str output of the value, if it could not be converted

    Examples:
        ```pycon
        >>> fractional(0.3)
        '3/10'
        >>> fractional(1.3)
        '1 3/10'
        >>> fractional(float(1/3))
        '1/3'
        >>> fractional(1)
        '1'
        >>> fractional("ten")
        'ten'
        >>> fractional(None)
        'None'

        ```
    Args:
        value (int, float, str): Integer to convert.

    Returns:
        str: Fractional number as a string.
    i  r   z.0f/rG   )r   r   r&   r   r(   r)   r   r'   r   Zlimit_denominator	numeratordenominator)r   numberZwhole_numberfracrU   rV   r   r   r   
fractional;  s    &
rY      r'   )r   	precisionr   c           
      C  s   ddddddddd	d
dd}z t | } t| s:t| W S W n  ttfk
r\   t|  Y S X dtt| }|| }|	d\}}t
dd|}g }|D ]}|||  q|d d| }	|	S )u  Return number in string scientific notation z.wq x 10ⁿ.

    Examples:
        ```pycon
        >>> scientific(float(0.3))
        '3.00 x 10⁻¹'
        >>> scientific(int(500))
        '5.00 x 10²'
        >>> scientific(-1000)
        '-1.00 x 10³'
        >>> scientific(1000, 1)
        '1.0 x 10³'
        >>> scientific(1000, 3)
        '1.000 x 10³'
        >>> scientific("99")
        '9.90 x 10¹'
        >>> scientific("foo")
        'foo'
        >>> scientific(None)
        'None'

        ```

    Args:
        value (int, float, str): Input number.
        precision (int): Number of decimal for first part of the number.

    Returns:
        str: Number in scientific notation z.wq x 10ⁿ.
    u   ⁰   ¹   ²   ³u   ⁴u   ⁵u   ⁶u   ⁷u   ⁸u   ⁹u   ⁻)0123456789rF   z{:.%se}ez^\+?(\-?)0*(.+)$z\1\2z x 10r   )r   r   r&   r   r)   r(   r   r'   r1   splitr2   r3   appendrK   )
r   r[   Z	exponentsfmtnZpart1Zpart2Z	new_part2charZ	final_strr   r   r   
scientificv  s6     

ro   {:}<>zfloat | None)r   r1   floorrL   floor_token
ceil_tokenr   c                 C  s   | dkrdS t | st| S |dk	r8| |k r8|} |}n|dk	rR| |krR|} |}nd}t|trn|||  S t|r|||  S d}t|dS )a  Returns number with the specified format, clamped between floor and ceil.

    If the number is larger than ceil or smaller than floor, then the respective limit
    will be returned, formatted and prepended with a token specifying as such.

    Examples:
        ```pycon
        >>> clamp(123.456)
        '123.456'
        >>> clamp(0.0001, floor=0.01)
        '<0.01'
        >>> clamp(0.99, format="{:.0%}", ceil=0.99)
        '99%'
        >>> clamp(0.999, format="{:.0%}", ceil=0.99)
        '>99%'
        >>> clamp(1, format=intword, floor=1e6, floor_token="under ")
        'under 1.0 million'
        >>> clamp(None) is None
        True

        ```

    Args:
        value (int, float): Input number.
        format (str OR callable): Can either be a formatting string, or a callable
            function that receives value and returns a string.
        floor (int, float): Smallest value before clamping.
        ceil (int, float): Largest value before clamping.
        floor_token (str): If value is smaller than floor, token will be prepended
            to output.
        ceil_token (str): If value is larger than ceil, token will be prepended
            to output.

    Returns:
        str: Formatted number. The output is clamped between the indicated floor and
        ceil. If the number is larger than ceil or smaller than floor, the output will
        be prepended with a token indicating as such.

    Nr   zpInvalid format. Must be either a valid formatting string, or a function that accepts value and returns a string.)r   r&   r   r/   r   r1   callabler)   )r   r1   rs   rL   rt   ru   tokenmsgr   r   r   clamp  s$    /

ry   r   r   )r   unitr[   r   c                 C  s   t | st| S | dkr2tt t t| nd}|dksF|dk rXt| |d | S | d|d d   } |dkrd|d d  }n |dk rd| d d  }nd	}t| d
||d  d  }|s|r|dkrd	}nd}| | | | S )u  Return a value with a metric SI unit-prefix appended.

    Examples:
        ```pycon
        >>> metric(1500, "V")
        '1.50 kV'
        >>> metric(2e8, "W")
        '200 MW'
        >>> metric(220e-6, "F")
        '220 μF'
        >>> metric(1e-14, precision=4)
        '10.00 f'

        ```

    The unit prefix is always chosen so that non-significant zero digits are required.
    i.e. `123,000` will become `123k` instead of `0.123M` and `1,230,000` will become
    `1.23M` instead of `1230K`. For numbers that are either too huge or too tiny to
    represent without resorting to either leading or trailing zeroes, it falls back to
    `scientific()`.
    ```pycon
    >>> metric(1e40)
    '1.00 x 10⁴⁰'

    ```

    Args:
        value (int, float): Input number.
        unit (str): Optional base unit.
        precision (int): The number of digits the output should contain.

    Returns:
        str:
    r   rC   ir   r   r   Z
kMGTPEZYRQu   mμnpfazyrqr   z.%if)   °u   ′u   ″rG   )	r   r&   r   r'   rs   log10absro   r1   )r   rz   r[   exponentrM   Zvalue_spacer   r   r   metric  s     #
$r   )r   )N)rD   )rZ   )rp   NNrq   rr   )r   r   )$__doc__
__future__r   r   r2   sysZ	fractionsr   typingr   Zi18nr   rR   r   r   ZNS_r	   r*   r
   r   version_infor   Ztyping_extensionsr   __annotations__r   r,   r7   rH   rJ   rP   rS   rY   ro   ry   r   r   r   r   r   <module>   sX   
I?I1;C     K