U
    ç¥¡c  ã                   @   sz   d Z ddlmZmZmZ deee edœdd„Zdeeeedœd	d
„Z	deee
f eedœdd„Zee
 edœdd„ZdS )z
String utility functions.
é    )ÚAnyÚOptionalÚUnionN)ÚobjÚclipÚreturnc                 C   sX   zDt | ƒ}|rt|ƒ|kr |W S |d|d … d |dd…  W S W n   Y dS X dS )af  
    Convert object to string representation, yielding the same result a `repr`
    but catches all exceptions and returns 'N/A' instead of raising the
    exception. Strings may be truncated by providing `clip`.

    >>> safe_repr(42)
    '42'
    >>> safe_repr('Clipped text', clip=8)
    'Clip..xt'
    >>> safe_repr([1,2,3,4], clip=8)
    '[1,2..4]'
    Né   z..éþÿÿÿzN/A)ÚreprÚlen)r   r   Ús© r   ú</tmp/pip-unpacked-wheel-8ad_c8mj/pympler/util/stringutils.pyÚ	safe_repr   s    &r   F)r   ÚmaxÚleftr   c                 C   s^   t | ƒ}| dd¡}t|ƒ|krV|r@d|t|ƒ| d d…  S |d|d … d S n|S dS )aÂ  
    Convert `obj` to string, eliminate newlines and truncate the string to
    `max` characters. If there are more characters in the string add ``...`` to
    the string. With `left=True`, the string can be truncated at the beginning.

    @note: Does not catch exceptions when converting `obj` to string with
        `str`.

    >>> trunc('This is a long text.', 8)
    This ...
    >>> trunc('This is a long text.', 8, left=True)
    ...text.
    Ú
ú|z...é   N)ÚstrÚreplacer   )r   r   r   r   r   r   r   Útrunc   s    r   é   )ÚiÚbaser   c                 C   sJ   d}d}| |kr*d}| t |ƒ } |d7 }qddddd	d
g}|| || f S )zO
    Pretty-print the integer `i` as a human-readable size representation.
    r   z
%4d     %sz%7.2f %sé   ÚBZKBZMBZGBZTBZEB)Úfloat)r   r   ZdegreeÚpatternZscalesr   r   r   Úpp8   s    
r   )Útr   c                 C   sB   | dkrdS t | d ƒt | d d ƒ| d   }}}d|||f S )z;
    Get a friendly timestamp represented as a string.
    NÚ i  é<   z%02d:%02d:%05.2f)Úint)r    ÚhÚmr   r   r   r   Úpp_timestampF   s    (r&   )N)F)r   )Ú__doc__Útypingr   r   r   r#   r   r   Úboolr   r   r   r&   r   r   r   r   Ú<module>   s
   