3
O6b(                 @   sV  d Z ddlZddlZddlZddlmZmZ er8e	j
ZnddlZe	ejfddZejdjZejdjZdZd	Zd
ZdZdZdZdZdZdZee dfZedfZee dfZedfZee dfZ edfZ!eej"eedg ej#ej$ej%gdZ&yej'dd de&d< W n e(k
r    de&d< Y nX ej'f e&j) Z*dd Z+dd Z,G dd de-Z.dS )zTools for working with the BSON decimal128 type.

.. versionadded:: 3.4

.. note:: The Decimal128 BSON type requires MongoDB 3.4+.
    N)PY3string_typec             C   s   ||| dS )z3An implementation of int.from_bytes for python 2.x.    )valuedummy_intZ_hexlifyr   r   N/var/www/html/sandeepIITI/myenv/lib/python3.6/site-packages/bson/decimal128.py_from_bytes"   s    r
   z<Q   =   i   i   i  "   l          @ l          ` l          p l               )precroundingEminEmaxcapitalsflagstraps)clampr   Z_clampc              C   s   t j } g | d< tjf | S )zReturns an instance of :class:`decimal.Context` appropriate
    for working with IEEE-754 128-bit decimal floating point values.
    r   )_CTX_OPTIONScopydecimalContext)optsr   r   r	   create_decimal128_contextS   s    r   c             C   sd  t jt}|j| } W dQ R X | j r8| j r4tS tS | j \}}}| j	 r|rZt
d| j rr| j rntS tS | j r~tS tS tdjdd |D }|j }d}d}x.ttd|D ]}	|d|	> @ r|d|	> O }qW x.td|D ] }	|d|	> @ r|d|	d > O }qW |t }
|d	? dkrB|d
@ }|tO }||
d@ d> O }n||
d	> O }|r\|tO }||fS )zConverts a decimal.Decimal to BID (high bits, low bits).

    :Parameters:
      - `value`: An instance of decimal.Decimal
    Nz'NaN with debug payload is not supported c             S   s   g | ]}t |qS r   )str).0digitr   r   r	   
<listcomp>q   s    z#_decimal_to_128.<locals>.<listcomp>r   @   r   1   l    i?  /   )r   localcontext_DEC128_CTXcreate_decimalis_infinite	is_signed_NINF_PINFas_tupleis_nan
ValueErroris_snan_NSNAN_PSNAN_NNAN_PNANintjoin
bit_lengthrangemin_EXPONENT_BIAS_EXPONENT_MASK_SIGN)r   ctxsigndigitsexponentZsignificandr6   highlowiZbiased_exponentr   r   r	   _decimal_to_128\   s<    rC   c               @   sp   e Zd ZdZdZdZdd Zdd Zed	d
 Z	e
dd Zdd Zdd Zdd Zdd Zdd Zdd ZdS )
Decimal128a  BSON Decimal128 type::

      >>> Decimal128(Decimal("0.0005"))
      Decimal128('0.0005')
      >>> Decimal128("0.0005")
      Decimal128('0.0005')
      >>> Decimal128((3474527112516337664, 5))
      Decimal128('0.0005')

    :Parameters:
      - `value`: An instance of :class:`decimal.Decimal`, string, or tuple of
        (high bits, low bits) from Binary Integer Decimal (BID) format.

    .. note:: :class:`~Decimal128` uses an instance of :class:`decimal.Context`
      configured for IEEE-754 Decimal128 when validating parameters.
      Signals like :class:`decimal.InvalidOperation`, :class:`decimal.Inexact`,
      and :class:`decimal.Overflow` are trapped and raised as exceptions::

        >>> Decimal128(".13.1")
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          ...
        decimal.InvalidOperation: [<class 'decimal.ConversionSyntax'>]
        >>>
        >>> Decimal128("1E-6177")
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          ...
        decimal.Inexact: [<class 'decimal.Inexact'>]
        >>>
        >>> Decimal128("1E6145")
        Traceback (most recent call last):
          File "<stdin>", line 1, in <module>
          ...
        decimal.Overflow: [<class 'decimal.Overflow'>, <class 'decimal.Rounded'>]

      To ensure the result of a calculation can always be stored as BSON
      Decimal128 use the context returned by
      :func:`create_decimal128_context`::

        >>> import decimal
        >>> decimal128_ctx = create_decimal128_context()
        >>> with decimal.localcontext(decimal128_ctx) as ctx:
        ...     Decimal128(ctx.create_decimal(".13.3"))
        ...
        Decimal128('NaN')
        >>>
        >>> with decimal.localcontext(decimal128_ctx) as ctx:
        ...     Decimal128(ctx.create_decimal("1E-6177"))
        ...
        Decimal128('0E-6176')
        >>>
        >>> with decimal.localcontext(DECIMAL128_CTX) as ctx:
        ...     Decimal128(ctx.create_decimal("1E6145"))
        ...
        Decimal128('Infinity')

      To match the behavior of MongoDB's Decimal128 implementation
      str(Decimal(value)) may not match str(Decimal128(value)) for NaN values::

        >>> Decimal128(Decimal('NaN'))
        Decimal128('NaN')
        >>> Decimal128(Decimal('-NaN'))
        Decimal128('NaN')
        >>> Decimal128(Decimal('sNaN'))
        Decimal128('NaN')
        >>> Decimal128(Decimal('-sNaN'))
        Decimal128('NaN')

      However, :meth:`~Decimal128.to_decimal` will return the exact value::

        >>> Decimal128(Decimal('NaN')).to_decimal()
        Decimal('NaN')
        >>> Decimal128(Decimal('-NaN')).to_decimal()
        Decimal('-NaN')
        >>> Decimal128(Decimal('sNaN')).to_decimal()
        Decimal('sNaN')
        >>> Decimal128(Decimal('-sNaN')).to_decimal()
        Decimal('-sNaN')

      Two instances of :class:`Decimal128` compare equal if their Binary
      Integer Decimal encodings are equal::

        >>> Decimal128('NaN') == Decimal128('NaN')
        True
        >>> Decimal128('NaN').bid == Decimal128('NaN').bid
        True

      This differs from :class:`decimal.Decimal` comparisons for NaN::

        >>> Decimal('NaN') == Decimal('NaN')
        False
    __high__low   c             C   sd   t |ttjfr"t|\| _| _n>t |ttfrRt	|dkrDt
d|\| _| _ntd|f d S )N   zYInvalid size for creation of Decimal128 from list or tuple. Must have exactly 2 elements.zCannot convert %r to Decimal128)
isinstance_string_typer   DecimalrC   _Decimal128__high_Decimal128__lowlisttuplelenr.   	TypeError)selfr   r   r   r	   __init__   s    zDecimal128.__init__c       
      C   s  | j }| j}|t@ rdnd}|t@ tkr8tj|f dfS |t@ tkrTtj|f dfS |t@ tkrptj|f dfS |t@ tkr|d@ d? t	 }tj|d|fS |d@ d	? t	 }t
d
}d}x4tdddD ]$}||@ d| d> ? ||< |d> }qW d}x6tdddD ]&}||@ d| d> ? ||< |d> }q W d}||@ d? |d< tdd tt|dD }tjt}	|	j|||fS Q R X dS )z^Returns an instance of :class:`decimal.Decimal` for this
        :class:`Decimal128`.
        r   r   NnFl          r$   l          r#               r      l          0   c             s   s   | ]}t |V  qd S )N)r4   )r   r    r   r   r	   	<genexpr>   s    z(Decimal128.to_decimal.<locals>.<genexpr>bigN)r   r_   )rL   rM   r;   _SNANr   rK   _NAN_INFr:   r9   	bytearrayr7   rO   r   r
   r%   r&   r'   )
rR   r@   rA   r=   r?   ZarrmaskrB   r>   r<   r   r   r	   
to_decimal   s8    zDecimal128.to_decimalc             C   sR   t |tstdt|dkr&td| t|dd d t|dd d fS )zCreate an instance of :class:`Decimal128` from Binary Integer
        Decimal string.

        :Parameters:
          - `value`: 16 byte string (128-bit IEEE 754-2008 decimal floating
            point in Binary Integer Decimal (BID) format).
        z"value must be an instance of bytesr   zvalue must be exactly 16 bytesr[   Nr   )rI   bytesrQ   rP   r.   
_UNPACK_64)clsr   r   r   r	   from_bid%  s
    	
zDecimal128.from_bidc             C   s   t | jt | j S )z;The Binary Integer Decimal (BID) encoding of this instance.)_PACK_64rM   rL   )rR   r   r   r	   bid4  s    zDecimal128.bidc             C   s   | j  }|j rdS t|S )NNaN)re   r-   r   )rR   decr   r   r	   __str__9  s    zDecimal128.__str__c             C   s   dt | f S )NzDecimal128('%s'))r   )rR   r   r   r	   __repr__@  s    zDecimal128.__repr__c             C   s   |\| _ | _d S )N)rL   rM   )rR   r   r   r   r	   __setstate__C  s    zDecimal128.__setstate__c             C   s   | j | jfS )N)rL   rM   )rR   r   r   r	   __getstate__F  s    zDecimal128.__getstate__c             C   s   t |tr| j|jkS tS )N)rI   rD   rk   NotImplemented)rR   otherr   r   r	   __eq__I  s    
zDecimal128.__eq__c             C   s
   | |k S )Nr   )rR   rs   r   r   r	   __ne__N  s    zDecimal128.__ne__N)rE   rF   )__name__
__module____qualname____doc__	__slots__Z_type_markerrS   re   classmethodri   propertyrk   rn   ro   rp   rq   rt   ru   r   r   r   r	   rD      s   ]*rD   l            i)/ry   r   structsysZbson.py3compatr   Z_PY3r   rJ   r4   
from_bytesr
   binasciihexlifyStructpackrj   unpackrg   r:   r9   Z_EXPONENT_MAXZ_EXPONENT_MINZ_MAX_DIGITSrb   ra   r`   r;   r*   r+   r2   r3   r0   r1   ROUND_HALF_EVENInvalidOperationOverflowInexactr   r   rQ   r   r&   r   rC   objectrD   r   r   r   r	   <module>   sV   	1