o
    C©Ìh,  ã                	   @  sn  d Z ddlmZ ddlZddlZddlmZ ddlmZmZm	Z	m
Z
mZ ddlmZmZ e d¡jZe 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dœZ'ej(d"i e' )¡ ¤ŽZ*eeje+e,e	e-ee- e-f f Z.G dd„ deƒZ/G dd„ deƒZ0d#dd„Z1d$dd„Z2G d d!„ d!ƒZ3dS )%zHTools for working with the BSON decimal128 type.

.. versionadded:: 3.4
é    )ÚannotationsN©ÚDecimal)ÚAnyÚSequenceÚTupleÚTypeÚUnion)ÚTypeDecoderÚTypeEncoderz<Ql            i   i   ièÿÿé"   l          @ l          ` l          p l            é   )ÚprecÚroundingÚEminÚEmaxÚcapitalsÚflagsÚtrapsÚclampc                   @  ó(   e Zd ZdZeddd„ƒZdd	d
„ZdS )ÚDecimalEncodera  Converts Python :class:`decimal.Decimal` to BSON :class:`Decimal128`.

    For example::
        opts = CodecOptions(type_registry=TypeRegistry([DecimalEncoder()]))
        bson.encode({"d": decimal.Decimal('1.0')}, codec_options=opts)

    .. versionadded:: 4.15
    ÚreturnúType[Decimal]c                 C  ó   t S ©Nr   ©Úself© r   ú^/var/www/html/Moveonwheels/move_on_wheels/venv/lib/python3.10/site-packages/bson/decimal128.pyÚpython_typeJ   ó   zDecimalEncoder.python_typeÚvaluer   Ú
Decimal128c                 C  s   t |ƒS r   ©r#   ©r   r"   r   r   r   Útransform_pythonN   ó   zDecimalEncoder.transform_pythonN)r   r   )r"   r   r   r#   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úpropertyr    r&   r   r   r   r   r   @   ó
    	r   c                   @  r   )ÚDecimalDecoderzóConverts BSON :class:`Decimal128` to Python :class:`decimal.Decimal`.

    For example::
        opts = CodecOptions(type_registry=TypeRegistry([DecimalDecoder()]))
        bson.decode(data, codec_options=opts)

    .. versionadded:: 4.15
    r   úType[Decimal128]c                 C  r   r   r$   r   r   r   r   Ú	bson_type\   r!   zDecimalDecoder.bson_typer"   r   údecimal.Decimalc                 C  s   |  ¡ S r   )Ú
to_decimalr%   r   r   r   Útransform_bson`   r'   zDecimalDecoder.transform_bsonN)r   r/   )r"   r   r   r1   )r(   r)   r*   r+   r,   r0   r3   r   r   r   r   r.   R   r-   r.   r   údecimal.Contextc                  C  s    t  ¡ } g | d< tjdi | ¤ŽS )z…Returns an instance of :class:`decimal.Context` appropriate
    for working with IEEE-754 128-bit decimal floating point values.
    r   Nr   )Ú_CTX_OPTIONSÚcopyÚdecimalÚContext)Úoptsr   r   r   Úcreate_decimal128_contextd   s   r:   r"   Ú_VALUE_OPTIONSúTuple[int, int]c                 C  sl  t  t¡�}| | ¡} W d  ƒ n1 sw   Y  |  ¡ r&|  ¡ r$tS tS |  ¡ \}}}|  	¡ rK|r7t
dƒ‚|  ¡ rC|  ¡ rAtS tS |  ¡ rItS tS td dd„ |D ƒ¡ƒ}| ¡ }d}d}ttd|ƒƒD ]}	|d|	> @ rt|d|	> O }qftd|ƒD ]}	|d|	> @ rŠ|d|	d > O }qz|t }
|d	? dkr¦|d
@ }|tO }||
d@ d> O }n||
d	> O }|r²|tO }||fS )zoConverts a decimal.Decimal to BID (high bits, low bits).

    :param 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>�   s    z#_decimal_to_128.<locals>.<listcomp>r   é@   r   é1   l   ÿÿÿ iÿ?  é/   )r7   Ú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ÚexponentÚsignificandrW   ÚhighÚlowÚiÚbiased_exponentr   r   r   Ú_decimal_to_128m   sB   ÿ€€rf   c                   @  s„   e Zd ZdZdZdZd&dd	„Zd'dd„Zed(dd„ƒZ	e
d)dd„ƒZd*dd„Zd*dd„Zd+dd„Zd,dd„Zd-d!d"„Zd-d#d$„Zd%S ).r#   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é   r"   r;   r   ÚNonec                 C  sf   t |ttjfƒrt|ƒ\| _| _d S t |ttfƒr+t	|ƒdkr#t
dƒ‚|\| _| _d S td|›d�ƒ‚)Né   zYInvalid size for creation of Decimal128 from list or tuple. Must have exactly 2 elements.zCannot convert z to Decimal128)Ú
isinstancer>   r7   r   rf   Ú_Decimal128__highÚ_Decimal128__lowÚlistÚtupleÚlenrO   Ú	TypeErrorr%   r   r   r   Ú__init__ÿ   s   ÿzDecimal128.__init__r1   c           
      C  s�  | j }| j}|t@ rdnd}|t@ tkrt |ddf¡S |t@ tkr*t |ddf¡S |t@ tkr8t |ddf¡S |t@ tkrN|d@ d? t	 }t |d	|f¡S |d
@ d? t	 }t
dƒ}d}tdddƒD ]}||@ d| d> ? ||< |d> }qbd}tdddƒD ]}||@ d| d> ? ||< |d> }q}d}||@ d? |d< tdd„ tt |d¡ƒD ƒƒ}t t¡�}	|	 |||f¡W  d  ƒ S 1 sÁw   Y  dS )z^Returns an instance of :class:`decimal.Decimal` for this
        :class:`Decimal128`.
        r   r   r   ÚNÚnÚFl         ÿ rE   )r   l         ü rD   é   éÿ   é   é   éÿÿÿÿé   é   l          é0   c                 s  s   � | ]}t |ƒV  qd S r   )rU   r?   r   r   r   Ú	<genexpr>1  s   € z(Decimal128.to_decimal.<locals>.<genexpr>ÚbigN)rm   rn   r\   Ú_SNANr7   r   Ú_NANÚ_INFr[   rZ   Ú	bytearrayrX   rp   r>   rU   Ú
from_bytesrF   rG   rH   )
r   rb   rc   r^   r`   ÚarrÚmaskrd   r_   r]   r   r   r   r2     s8   

$ÿzDecimal128.to_decimalÚclsr/   Úbytesc                 C  s\   t |tƒstdt|ƒ› �ƒ‚t|ƒdkrtdƒ‚| t|dd… ƒd t|dd… ƒd fƒS )zçCreate 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 bytesr}   Nr   )rl   r‰   rr   Útyperq   rO   Ú
_UNPACK_64)rˆ   r"   r   r   r   Úfrom_bid6  s
   
,zDecimal128.from_bidc                 C  s   t | jƒt | jƒ S )z;The Binary Integer Decimal (BID) encoding of this instance.)Ú_PACK_64rn   rm   r   r   r   r   ÚbidD  s   zDecimal128.bidr>   c                 C  s   |   ¡ }| ¡ r
dS t|ƒS )NÚNaN)r2   rN   r>   )r   Údecr   r   r   Ú__str__I  s   zDecimal128.__str__c                 C  s   d| ›d�S )NzDecimal128('z')r   r   r   r   r   Ú__repr__P  ó   zDecimal128.__repr__r<   c                 C  s   |\| _ | _d S r   ©rm   rn   r%   r   r   r   Ú__setstate__S  s   zDecimal128.__setstate__c                 C  s   | j | jfS r   r•   r   r   r   r   Ú__getstate__V  r”   zDecimal128.__getstate__Úotherr   Úboolc                 C  s   t |tƒr| j|jkS tS r   )rl   r#   r�   ÚNotImplemented©r   r˜   r   r   r   Ú__eq__Y  s   
zDecimal128.__eq__c                 C  s
   | |k S r   r   r›   r   r   r   Ú__ne__^  s   
zDecimal128.__ne__N)r"   r;   r   rj   )r   r1   )rˆ   r/   r"   r‰   r   r#   )r   r‰   )r   r>   )r"   r<   r   rj   )r   r<   )r˜   r   r   r™   )r(   r)   r*   r+   Ú	__slots__Ú_type_markerrs   r2   Úclassmethodr�   r,   r�   r’   r“   r–   r—   rœ   r�   r   r   r   r   r#   �   s     ]

)




r#   r   )r   r4   )r"   r;   r   r<   )4r+   Ú
__future__r   r7   Ústructr   Útypingr   r   r   r   r	   Úbson.codec_optionsr
   r   ÚStructÚpackrŽ   ÚunpackrŒ   r[   rZ   Ú_EXPONENT_MAXÚ_EXPONENT_MINÚ_MAX_DIGITSrƒ   r‚   r�   r\   rK   rL   rS   rT   rQ   rR   ÚROUND_HALF_EVENÚInvalidOperationÚOverflowÚInexactr5   r8   r6   rG   Úfloatr>   rU   r;   r   r.   r:   rf   r#   r   r   r   r   Ú<module>   sP   ø 

	0