
    &g!)              	      "   S r SSKJr  SSKrSSKrSSKJrJrJrJ	r	J
r
  \R                  " S5      R                  r\R                  " S5      R                  rSrSrSrS	rS
rSrSrSrSr\\-   S4r\S4r\\-   S4r\S4r\\-   S4r\S4r\\R>                  \\S/ \R@                  \RB                  \RD                  /SS.r#\RH                  " S0 \#RK                  5       D6r&\
\RN                  \(\)\\*\\*   \*4   4   r+SS jr,SS jr- " S S5      r.g)zHTools for working with the BSON decimal128 type.

.. versionadded:: 3.4
    )annotationsN)AnySequenceTupleTypeUnionz<Ql            i   i   i"   l          @ l          ` l          p l               )precroundingEminEmaxcapitalsflagstrapsclampc                 `    [         R                  5       n / U S'   [        R                  " S0 U D6$ )z}Returns an instance of :class:`decimal.Context` appropriate
for working with IEEE-754 128-bit decimal floating point values.
r    )_CTX_OPTIONScopydecimalContext)optss    TC:\Suresh\moveshuttle\MDcreated\moveengine\venv\Lib\site-packages\bson/decimal128.pycreate_decimal128_contextr   =   s-     DDM??"T""    c           
         [         R                  " [        5       nUR                  U 5      n SSS5        U R	                  5       (       a!  U R                  5       (       a  [        $ [        $ U R                  5       u  p#nU R                  5       (       ai  U(       a  [        S5      eU R                  5       (       a!  U R                  5       (       a  [        $ [        $ U R                  5       (       a  [        $ [        $ [!        SR#                  U Vs/ s H  n[%        U5      PM     sn5      5      nUR'                  5       nSnSn	[)        [+        SU5      5       H  n
USU
-  -  (       d  M  U	SU
-  -  n	M     [)        SU5       H  n
USU
-  -  (       d  M  USU
S-
  -  -  nM     U[,        -   nUS-	  S:X  a  US-  nU[.        -  nXS	-  S
-  -  nOXS-  -  nU(       a	  U[0        -  nX4$ ! , (       d  f       GN= fs  snf )zgConverts a decimal.Decimal to BID (high bits, low bits).

:param value: An instance of decimal.Decimal
Nz'NaN with debug payload is not supported 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str
bit_lengthrangemin_EXPONENT_BIAS_EXPONENT_MASK_SIGN)valuectxsigndigitsexponentdigitsignificandr4   highlowibiased_exponents               r   _decimal_to_128rE   F   s   
 
		k	*c""5) 
+ ))u4u4"^^-D(||~~FGG==??"__..6:F:))u4u4bggv>ves5zv>?@K'')JD
C3r:&'!q&!!16MC ( 2z"!q&!!A!b&M!D # /OrzQn$6)b002%%9Q 
+	* ?s   G)G;)
G8c                      \ rS rSrSrSrSrSS jrSS jr\	SS j5       r
\SS j5       rSS	 jrSS
 jrSS jrSS jrSS jrSS jrSrg)
Decimal128v   a  BSON Decimal128 type::

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

:param 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                (   [        U[        [        R                  45      (       a  [	        U5      u  U l        U l        g [        U[        [        45      (       a*  [        U5      S:w  a  [        S5      eUu  U l        U l        g [        SU< S35      e)N   zYInvalid size for creation of Decimal128 from list or tuple. Must have exactly 2 elements.zCannot convert z to Decimal128)
isinstancer3   r   DecimalrE   _Decimal128__high_Decimal128__lowlisttuplelenr+   	TypeErrorselfr:   s     r   __init__Decimal128.__init__   s}    ec7??344&5e&<#DKe}--5zQ   
 ',#DKoeYnEFFr   c           	        U R                   nU R                  nU[        -  (       a  SOSnU[        -  [        :X  a  [        R
                  " USS45      $ U[        -  [        :X  a  [        R
                  " USS45      $ U[        -  [        :X  a  [        R
                  " USS45      $ U[        -  [        :X  a(  US-  S-	  [        -
  n[        R
                  " US	U45      $ US
-  S-	  [        -
  n[        S5      nSn[        SSS5       H  nX&-  SU-
  S-  -	  XW'   US-  nM     Sn[        SSS5       H  nX-  SU-
  S-  -	  XW'   US-  nM     SnX-  S-	  US'   [        S [        [        R                  US5      5       5       5      n[        R                   " ["        5       n	U	R%                  X8U45      sSSS5        $ ! , (       d  f       g= f)zNReturns an instance of :class:`decimal.Decimal` for this
:class:`Decimal128`.
r
   r   r   NnFl          r!   )r   l          r                      l          0   c              3  8   #    U  H  n[        U5      v   M     g 7fN)r1   ).0r?   s     r   	<genexpr>(Decimal128.to_decimal.<locals>.<genexpr>
  s     O/Nes5zz/Ns   bigN)rP   rQ   r9   _SNANr   rO   _NAN_INFr8   r7   	bytearrayr5   rS   r3   r1   
from_bytesr"   r#   r$   )
rW   rA   rB   r<   r>   arrmaskrC   r=   r;   s
             r   
to_decimalDecimal128.to_decimal   s    {{jjE\q5LU"??D"c?33Tkd"??D"c?33Tkd"??D"c?33>!n4 22r9^KH??D$#9:: 22r9^KHm!r1b!AjrAv!m4CF19D " "q!RAkA!|4CF19D ! "+"$A Os3>>#u3M/NOO!!+.#%%tX&>? /..s   %G
Gc                    [        U[        5      (       d  [        S[        U5       35      e[	        U5      S:w  a  [        S5      eU " [        USS 5      S   [        USS 5      S   45      $ )zCreate an instance of :class:`Decimal128` from Binary Integer
Decimal string.

:param 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 bytes, not    zvalue must be exactly 16 bytesrd   Nr   )rN   bytesrU   typerT   r+   
_UNPACK_64)clsr:   s     r   from_bidDecimal128.from_bid  sp     %''FtE{mTUUu:=>>JuQRy)!,jr.CA.FGHHr   c                X    [        U R                  5      [        U R                  5      -   $ )z;The Binary Integer Decimal (BID) encoding of this instance.)_PACK_64rQ   rP   rW   s    r   bidDecimal128.bid  s!     

#ht{{&;;;r   c                d    U R                  5       nUR                  5       (       a  g[        U5      $ )NNaN)rs   r*   r3   )rW   decs     r   __str__Decimal128.__str__"  s%    oo::<<3xr   c                    SU < S3$ )NzDecimal128('z')r   r   s    r   __repr__Decimal128.__repr__)  s    dXR((r   c                "    Uu  U l         U l        g rg   rP   rQ   rV   s     r   __setstate__Decimal128.__setstate__,  s    "'TZr   c                2    U R                   U R                  4$ rg   r   r   s    r   __getstate__Decimal128.__getstate__/  s    {{DJJ&&r   c                j    [        U[        5      (       a  U R                  UR                  :H  $ [        $ rg   )rN   rG   r   NotImplementedrW   others     r   __eq__Decimal128.__eq__2  s(    eZ((88uyy((r   c                    X:X  + $ rg   r   r   s     r   __ne__Decimal128.__ne__7  s      r   N)r:   _VALUE_OPTIONSreturnNone)r   zdecimal.Decimal)rz   zType[Decimal128]r:   rw   r   rG   )r   rw   )r   r3   )r:   Tuple[int, int]r   r   )r   r   )r   r   r   bool)__name__
__module____qualname____firstlineno____doc__	__slots___type_markerrX   rs   classmethodr{   propertyr   r   r   r   r   r   r   __static_attributes__r   r   r   rG   rG   v   sj    [z $ILG'@R I I < <)('
!r   rG   r   )r   zdecimal.Context)r:   r   r   r   )/r   
__future__r   r   structtypingr   r   r   r   r   Structpackr~   unpackry   r8   r7   _EXPONENT_MAX_EXPONENT_MIN_MAX_DIGITSrn   rm   rl   r9   r'   r(   r/   r0   r-   r.   ROUND_HALF_EVENInvalidOperationOverflowInexactr   r   r   r#   rO   floatr3   r1   r   r   rE   rG   r   r   r   <module>r      sM   #   4 4==##]]4 ''
	q	q		q	q	
%-	
 ''&&(8(8'//J	 oo4 1 1 34wsE#x}c:Q4RRS#-`B! B!r   