Your IP : 216.73.216.213


Current Path : /opt/alt/python35/lib/python3.5/site-packages/google/apputils/__pycache__/
Upload File :
Current File : //opt/alt/python35/lib/python3.5/site-packages/google/apputils/__pycache__/humanize.cpython-35.pyc



C��]�;�@s�dZddlZddlZddlZeddddddg�Zejd	�jZd
ddd
ddiZ	ed�Z
dd�Zddd�Zddd�Z
ddd�Zdd�Zddddd�Zddd �Zd!d"�ZdYZdZZd4Zddd5d6�Zd7d8�Zddd9d:�Zd;d[d>d\d4d]iZdJeej��dKZdJeZd+dLdM�ZdNdOdP�ZdNdQdR�Z dSdT�Z!dUdV�Z"dWdX�Z#dS)^aQLightweight routines for producing more friendly output.

Usage examples:

  'New messages: %s' % humanize.Commas(star_count)
    -> 'New messages: 58,192'

  'Found %s.' % humanize.Plural(error_count, 'error')
    -> 'Found 2 errors.'

  'Found %s.' % humanize.Plural(error_count, 'ox', 'oxen')
    -> 'Found 2 oxen.'

  'Copied at %s.' % humanize.DecimalPrefix(rate, 'bps', precision=3)
    -> 'Copied at 42.6 Mbps.'

  'Free RAM: %s' % humanize.BinaryPrefix(bytes_free, 'B')
    -> 'Free RAM: 742 MiB'

  'Finished all tasks in %s.' % humanize.Duration(elapsed_time)
    -> 'Finished all tasks in 34m 5s.'

These libraries are not a substitute for full localization.  If you
need localization, then you will have to think about translating
strings, formatting numbers in different ways, and so on.  Use
ICU if your application is user-facing.  Use these libraries if
your application is an English-only internal tool, and you are
tired of seeing "1 results" or "3450134804 bytes used".

Compare humanize.*Prefix() to C++ utilites HumanReadableNumBytes and
HumanReadableInt in strings/human_readable.h.
�N�sh�ssZtchZaxZix�exz\d+|\D+�index�indicesZmatrixZmatricesZvertexZverticesZ
AEIOUaeioucCs�|dkrd}|}nd}g}x/|dkrY|jd|d�|d}q+W|jd|�|djt|��S)zqFormats an integer with thousands-separating commas.

  Args:
    value: An integer.

  Returns:
    A string.
  r�-�i�z%03dz%d�,)�append�join�reversed)�value�sign�result�r�I/opt/alt/python35/lib/python3.5/site-packages/google/apputils/humanize.py�Commas8s	
rcCsd|t|||�fS)a8Formats an integer and a string into a single pluralized string.

  Args:
    quantity: An integer.
    singular: A string, the singular form of a noun.
    plural: A string, the plural form.  If not specified, then simple
        English rules of regular pluralization will be used.

  Returns:
    A string.
  z%d %s)�
PluralWord)�quantity�singular�pluralrrr�PluralNsrcCs�|dkr|S|r|S|tkr.t|Sx%tD]}|j|�r5d|Sq5W|jd�r�|d	d
�tkr�d|S|jd�r�|dd�tkr�d|dd
�Sd|S)a0Builds the plural of an English word.

  Args:
    quantity: An integer.
    singular: A string, the singular form of a noun.
    plural: A string, the plural form.  If not specified, then simple
        English rules of regular pluralization will be used.

  Returns:
    the plural form of the word.
  �z%ses�o��yz%siesNz%ss������rrr)�SPECIAL_PLURALS�SIBILANT_ENDINGS�endswith�VOWELS)rrrZendingrrrr]s
%%r�andcCsyt|�}|dkrdS|dkr0|dS|dkrMd|j|�Sddj|dd	��||d
fSdS)a7Convert a list of words to an English-language word series.

  Args:
    words: A list of word strings.
    conjunction: A coordinating conjunction.

  Returns:
    A single string containing all the words in the list separated by commas,
    the coordinating conjunction, and a serial comma, as appropriate.
  rrrrz %s z	%s, %s %sz, Nrr)�lenr)�wordsZconjunctionZ	num_wordsrrr�
WordSeries�sr%cCs?|stdj|���|dtkr3d|Sd|SdS)z�Formats a noun with an appropriate indefinite article.

  Args:
    noun: A string representing a noun.

  Returns:
    A string containing noun prefixed with an indefinite article, e.g.,
    "a thing" or "an object".
  zargument must be a word: {!r}rzan za N)�
ValueError�formatr!)Znounrrr�AddIndefiniteArticle�s

r(rc	Cst|||td|d|�S)a�Formats an integer and a unit into a string, using decimal prefixes.

  The unit will be prefixed with an appropriate multiplier such that
  the formatted integer is less than 1,000 (as long as the raw integer
  is less than 10**27).  For example:

    DecimalPrefix(576012, 'bps') -> '576 kbps'
    DecimalPrefix(576012, '') -> '576 k'
    DecimalPrefix(576, '') -> '576'
    DecimalPrefix(1574215, 'bps', 2) -> '1.6 Mbps'

  Only the SI prefixes which are powers of 10**3 will be used, so
  DecimalPrefix(100, 'thread') is '100 thread', not '1 hthread'.

  See also:
    BinaryPrefix()

  Args:
    quantity: A number.
    unit: A string, the dimension for quantity, with no multipliers (e.g.
        "bps").  If quantity is dimensionless, the empty string.
    precision: An integer, the minimum number of digits to display.
    min_scale: minimum power of 1000 to scale to, (None = unbounded).
    max_scale: maximum power of 1000 to scale to, (None = unbounded).

  Returns:
    A string, composed by the decimal (scaled) representation of quantity at the
    required precision, possibly followed by a space, the appropriate multiplier
    and the unit.
  �	min_scale�	max_scale)�_Prefix�DecimalScale)r�unit�	precisionr)r*rrr�
DecimalPrefix�sr/cCst|||t�S)aFormats an integer and a unit into a string, using binary prefixes.

  The unit will be prefixed with an appropriate multiplier such that
  the formatted integer is less than 1,024 (as long as the raw integer
  is less than 2**90).  For example:

    BinaryPrefix(576012, 'B') -> '562 KiB'
    BinaryPrefix(576012, '') -> '562 Ki'

  See also:
    DecimalPrefix()

  Args:
    quantity: A number.
    unit: A string, the dimension for quantity, with no multipliers (e.g.
        "B").  If quantity is dimensionless, the empty string.
    precision: An integer, the minimum number of digits to display.

  Returns:
    A string, composed by the decimal (scaled) representation of quantity at the
    required precision, possibly followed by a space, the appropriate multiplier
    and the unit.
  )r+�BinaryScale)rr-r.rrr�BinaryPrefix�sr1c	Ks�|rdnd}|s&d||fS|td�td�gksStj|�rdd|||fS||||�\}}|r�d}dtd|ttjt|�d	��d
�}||||fS)a�Formats an integer and a unit into a string.

  Args:
    quantity: A number.
    unit: A string, the dimension for quantity, with no multipliers (e.g.
        "bps").  If quantity is dimensionless, the empty string.
    precision: An integer, the minimum number of digits to display.
    scale_callable: A callable, scales the number and units.
    **args: named arguments passed to scale_callable.

  Returns:
    A string.
  � rz0%s%s�infz-infz%f%s%sz%%.%df%%s%%sr�
r)�float�math�isnan�max�int�log�abs)	rr-r.Zscale_callable�args�	separatorZscaled_quantityZscaled_unitZ
print_patternrrrr+�s-	*r+r�z�a�f�p�n�µ�mr�k�M�G�T�P�E�Z�Y�cCsn|dks|tkrt}|dks6|tkr<t}t|t|td�}t||d||�S)a�Get the scaled value and decimal prefixed unit in a tupple.

    DecimalScale(576012, 'bps') -> (576.012, 'kbps')
    DecimalScale(1574215, 'bps') -> (1.574215, 'Mbps')

  Args:
    quantity: A number.
    unit: A string.
    min_scale: minimum power of 1000 to normalize to (None = unbounded)
    max_scale: maximum power of 1000 to normalize to (None = unbounded)

  Returns:
    A tuple of a scaled quantity (float) and BinaryPrefix for the
    units (string).
  Nri�)�DECIMAL_MIN_SCALE�DECIMAL_MAX_SCALE�DECIMAL_PREFIXES�_Scale)rr-r)r*Zpowersrrrr,sr,cCst||dd
�S)aGet the scaled value and binary prefixed unit in a tupple.

    BinaryPrefix(576012, 'B') -> (562.51171875, 'KiB')

  Args:
    quantity: A number.
    unit: A string.

  Returns:
    A tuple of a scaled quantity (float) and BinaryPrefix for the
    units (string).
  i�Ki�Mi�Gi�Ti�Pi�Ei�Zi�Yi)rRrSrTrUrVrWrXrY)rQ)rr-rrrr0's
r0cCs�|s;|s;tj|�s;|td�td�gkrKt|�|fS|dkrmd}dt|�}|d}}xEt||�D]4\}}t|�||}t|�|kr�Pq�W|||fS)a�Returns the formatted quantity and unit into a tuple.

  Args:
    quantity: A number.
    unit: A string
    multiplier: An integer, the ratio between prefixes.
    prefixes: A sequence of strings.
        If empty or None, no scaling is done.
    min_scale: minimum power of multiplier corresponding to the first prefix.
        If None assumes prefixes are for positive powers only.

  Returns:
    A tuple containing the raw scaled quantity (float) and the prefixed unit.
  r3z-infNrr)r)r6r7r5�tuple�	enumerater;)rr-Z
multiplier�prefixesr)r
�prefixZpowerrrrrQ8s
rQ��⅓�⅔��⅕�⅖�⅗�⅘�⅛�¼�⅜�½�⅝�¾�⅞g�?g@cCs|tkrdt|�St|�}t|�}||}|tkrXt|d�Sg}xmttj��D]Y\}}tt	||��}t|t
|�t
|��}|j|||f�qqWt|�\}	}
|r�|
r�d|||
fS|r
t|�S|
r|
SdS)a�Convert a number into a string that might include a unicode fraction.

  This method returns the integer representation followed by the closest
  fraction of a denominator 2, 3, 4, 5 or 8.
  For instance, 0.33 will be converted to 1/3.
  The resulting representation should be less than 1/16 off.

  Args:
    number: a python number
    spacer: an optional string to insert between the integer and the fraction
        default is an empty string.

  Returns:
    a unicode string representing the number.
  z-%srz%d%s%s�0)
�FRACTION_ROUND_DOWN�PrettyFractionr;r9�FRACTION_ROUND_UP�str�list�	FRACTIONS�items�roundr5r
�min)�numberZspacerZroundedZfractZerrors_fractions�denominatorZfraction_elements�	numerator�errorZunused_errorZ
fraction_textrrrrobs(

 
ror2cCsOytjd|�}Wn&tk
r>dttjj�SYnXt|d|�S)aFormats a nonnegative number of seconds into a human-readable string.

  Args:
    duration: A float duration in seconds.
    separator: A string separator between days, hours, minutes and seconds.

  Returns:
    Formatted string like '5d 12h 30m 45s'.
  �secondsz>=r=)�datetime�	timedelta�
OverflowError�	TimeDeltar8)Zdurationr=�deltarrr�Duration�s


r�cCs�g}|j}|jr,|jd|j�|dkrW|jd|d�|d;}|dkr�|jd|d�|d;}||jd7}|s�|r�|jd|�|j|�S)z�Format a datetime.timedelta into a human-readable string.

  Args:
    delta: The datetime.timedelta to format.
    separator: A string separator between days, hours, minutes and seconds.

  Returns:
    Formatted string like '5d 12h 30m 45s'.
  z%ddiz%dh�<z%dmg��.Az%gs)r{�daysr
�microsecondsr)r�r=�partsr{rrrr�s
		


rcCsIt|�}x6t|�D](\}}|j�rt|�||<qW|S)aIKey function for "natural sort" ordering.

  This key function results in a lexigraph sort. For example:
  - ['1, '3', '20'] (not ['1', '20', '3']).
  - ['Model 9', 'Model 70 SE', 'Model 70 SE2']
    (not ['Model 70 SE', 'Model 70 SE2', 'Model 9']).

  Usage:
    new_list = sorted(old_list, key=humanize.NaturalSortKey)
    or
    list_sort_in_place.sort(key=humanize.NaturalSortKey)

  Based on code by Steven Bazyl <sbazyl@google.com>.

  Args:
    data: str, The key being compared in a sort.

  Returns:
    A list which is comparable to other lists for the purpose of sorting.
  )�DIGIT_SPLITTERr[�isdigitr9)�data�segments�ir
rrr�NaturalSortKey�s
r�cCs"tjj||�}|jd�S)a�Format a UNIX timestamp into a human-readable string.

  Args:
    unix_ts: UNIX timestamp (number of seconds since epoch). May be a floating
        point number.
    tz: datetime.tzinfo object, timezone to use when formatting. Typical uses
        might want to rely on datelib or pytz to provide the tzinfo object, e.g.
        use datelib.UTC, datelib.US_PACIFIC, or pytz.timezone('Europe/Dublin').

  Returns:
    Formatted string like '2013-11-17 11:08:27.720000 PST'.
  z%Y-%m-%d %H:%M:%S.%f %Z)r|�
fromtimestamp�strftime)Zunix_ts�tz�	date_timerrr�
UnixTimestamp�s
r�cCs�|dks|t|�kr.td|��|ddkrGd}nO|d}|d	krfd
}n0|dkr{d}n|d
kr�d}nd}t|�|S)z�Adds an ordinal suffix to a non-negative integer (e.g. 1 -> '1st').

  Args:
    value: A non-negative integer.

  Returns:
    A string containing the integer with a two-letter ordinal suffix.
  rz+argument must be a non-negative integer: %s�d���
�thr4r�strZndr^Zrd)r�r�r�)r9r&rq)r
�suffix�remrrr�AddOrdinalSuffix�s		
			r�)rr>r?r@rArBrCrDrrErFrGrHrIrJrKrLi����)Nr_r`N)NrbrcrdreN)	NrfrgrhrirjrkrlN)$�__doc__r|r6�re�	frozensetr�compile�findallr�rr!rrrr%r(r/r1r+rPrNrOr,r0rQrsr8�keysrnrpror�rr�r�r�rrrr�<module>#sF##"!
'