o
    Me  ã                   @   s^   d dl Z d dlZg ZeZeZddd„Zdd„ Zddd	„Zd
d„ Z	ddd„Z
dd„ Zdd„ ZdS )é    Núutf-8Fc                    s&  | du r| S t | tƒr,|r#tj t| ƒ¡D ]}t| | ˆ ƒ| |< q| S ‡ fdd„| D ƒS t | tƒrR|rG| D ]}|  |¡ |  	t|ˆ ƒ¡ q5| S t‡ fdd„| D ƒƒS t | t
ƒrŽ|rvi }t | ¡D ]\}}t|ˆ ƒ|t|ˆ ƒ< q`|  |¡ | S i }t | ¡D ]\}}t|ˆ ƒ|t|ˆ ƒ< q}|S t| ˆ ƒS )a­  
    All string in PaddlePaddle should be represented as a literal string.
    
    This function will convert object to a literal string without any encoding.
    Especially, if the object type is a list or set container, we will iterate
    all items in the object and convert them to literal string.

    In Python3:
        Decode the bytes type object to str type with specific encoding

    In Python2:
        Decode the str type object to unicode type with specific encoding

    Args:
        obj(unicode|str|bytes|list|set) : The object to be decoded.
        encoding(str) : The encoding format to decode a string
        inplace(bool) : If we change the original object or we create a new one

    Returns:
        Decoded result of obj
    
    Examples:

        .. code-block:: python

            import paddle

            data = "paddlepaddle"
            data = paddle.compat.to_text(data)
            # paddlepaddle

    Nc                    ó   g | ]}t |ˆ ƒ‘qS © ©Ú_to_text©Ú.0Úitem©Úencodingr   ú=D:\Projects\ConvertPro\env\Lib\site-packages\paddle/compat.pyÚ
<listcomp>C   ó    zto_text.<locals>.<listcomp>c                    r   r   r   r   r
   r   r   r   K   r   )Ú
isinstanceÚlistÚsixÚmovesÚxrangeÚlenr   ÚsetÚremoveÚaddÚdictÚ	iteritemsÚupdate)Úobjr   ÚinplaceÚir	   Znew_objÚkeyÚvaluer   r
   r   Úto_text   s6   !





r    c                 C   sN   | du r| S t | tjƒr|  |¡S t | tjƒr| S t | ttfƒr"| S t | ¡S )aŽ  
    In Python3:
        Decode the bytes type object to str type with specific encoding

    In Python2:
        Decode the str type object to unicode type with specific encoding,
        or we just return the unicode string of object

    Args:
        obj(unicode|str|bytes) : The object to be decoded.
        encoding(str) : The encoding format

    Returns:
        decoded result of obj
    N)r   r   Úbinary_typeÚdecodeÚ	text_typeÚboolÚfloatÚu©r   r   r   r   r   r   \   s   

r   c                    s®   | du r| S t | tƒr,|r#tj t| ƒ¡D ]}t| | ˆ ƒ| |< q| S ‡ fdd„| D ƒS t | tƒrR|rG| D ]}|  |¡ |  	t|ˆ ƒ¡ q5| S t‡ fdd„| D ƒƒS t| ˆ ƒS )a×  
    All string in PaddlePaddle should be represented as a literal string.
    
    This function will convert object to a bytes with specific encoding.
    Especially, if the object type is a list or set container, we will iterate
    all items in the object and convert them to bytes.

    In Python3:
        Encode the str type object to bytes type with specific encoding

    In Python2:
        Encode the unicode type object to str type with specific encoding,
        or we just return the 8-bit string of object

    Args:
        obj(unicode|str|bytes|list|set) : The object to be encoded.
        encoding(str) : The encoding format to encode a string
        inplace(bool) : If we change the original object or we create a new one

    Returns:
        Decoded result of obj
    
    Examples:

        .. code-block:: python

            import paddle

            data = "paddlepaddle"
            data = paddle.compat.to_bytes(data)
            # b'paddlepaddle'

    Nc                    r   r   ©Ú	_to_bytesr   r
   r   r   r   ¤   r   zto_bytes.<locals>.<listcomp>c                    r   r   r(   r   r
   r   r   r   ¬   r   )
r   r   r   r   r   r   r)   r   r   r   )r   r   r   r   r	   r   r
   r   Úto_bytesy   s    "



r*   c                 C   sH   | du r| S |dusJ ‚t | tjƒr|  |¡S t | tjƒr| S t | ¡S )aŒ  
    In Python3:
        Encode the str type object to bytes type with specific encoding

    In Python2:
        Encode the unicode type object to str type with specific encoding,
        or we just return the 8-bit string of object

    Args:
        obj(unicode|str|bytes) : The object to be encoded.
        encoding(str) : The encoding format

    Returns:
        encoded result of obj
    N)r   r   r#   Úencoder!   Úbr'   r   r   r   r)   ±   s   

r)   c                 C   sŠ   t jr;| dkrd| }tt | | t d| ¡ ¡ƒ| S | dk r5d| }tt | | t d| ¡ ¡ƒ| S t d| ¡S ddl}| | |¡S )z¦
    Compatible round which act the same behaviour in Python3.

    Args:
        x(float) : The number to round halfway.

    Returns:
        round result of x
    g        é
   g      à?r   N)	r   ÚPY3r%   ÚmathÚfloorÚcopysignÚceilÚ__builtin__Úround)ÚxÚdÚpr3   r   r   r   r4   Î   s   
""r4   c                 C   s   | | S )aO  
    Compatible division which act the same behaviour in Python3 and Python2,
    whose result will be a int value of floor(x / y) in Python3 and value of
    (x / y) in Python2.

    Args:
        x(int|float) : The number to divide.
        y(int|float) : The number to be divided

    Returns:
        division result of x // y
    r   )r5   Úyr   r   r   Úfloor_divisionè   s   r9   c                 C   s   | dusJ ‚t | ƒS )z°
    Get the error message of a specific exception

    Args:
        exec(Exception) : The exception to get error message.

    Returns:
        the error message of exec
    N)Ústr)Úexcr   r   r   Úget_exception_messageù   s   
r<   )r   F)r   )r   r/   Ú__all__ÚintZint_typeZ	long_typer    r   r*   r)   r4   r9   r<   r   r   r   r   Ú<module>   s   
C
8
