o
    _�¿j:  ã                   @  sF   d Z ddlmZ ddlmZ ddd	„Zddd„Zddd„Zg d¢ZdS )zÝSafe type-coercion utilities.

Used by parsers to convert raw exchange response values (which may be
strings with commas, ``None``, empty strings, or unexpected types) into
typed Python values without raising exceptions.
é    )Úannotations)ÚAnyÚvaluer   Úreturnú
int | Nonec              	   C  sx   | du rdS t | tƒr| S t | tƒrt| ƒS t| ƒ ¡  dd¡}|r&|dv r(dS ztt|ƒƒW S  ttfy;   Y dS w )uÈ  Coerce *value* to ``int``, returning ``None`` on failure.

    Handles common cases returned by NSE/BSE APIs:
    - ``None`` â†’ ``None``
    - ``""`` â†’ ``None``
    - ``"1,23,456"`` â†’ ``123456`` (comma-separated Indian numerals)
    - ``"23.0"`` â†’ ``23``
    - ``-999`` â†’ ``-999``

    Examples::

        >>> safe_int("1,23,456")
        123456
        >>> safe_int(None) is None
        True
        >>> safe_int("N/A") is None
        True
    Nú,Ú )ú-úN/AÚNAr	   )Ú
isinstanceÚintÚfloatÚstrÚstripÚreplaceÚ
ValueErrorÚOverflowError©r   Ús© r   úP/home/dinkstrade/pdmp/venv/lib/python3.10/site-packages/indiaopt/utils/coerce.pyÚsafe_int   s   

ÿr   úfloat | Nonec              	   C  st   | du rdS t | tƒr| S t | tƒrt| ƒS t| ƒ ¡  dd¡}|r&|dv r(dS zt|ƒW S  ttfy9   Y dS w )záCoerce *value* to ``float``, returning ``None`` on failure.

    Examples::

        >>> safe_float("45.67")
        45.67
        >>> safe_float("") is None
        True
        >>> safe_float(None) is None
        True
    Nr   r   )r	   r
   r   )r   r   r   r   r   r   r   r   r   r   r   r   Ú
safe_float0   s   


ÿr   r   c                 C  s   t | ƒ}|dur
|S dS )z<Like :func:`safe_int` but returns ``0`` instead of ``None``.Nr   )r   )r   Úresultr   r   r   Úsafe_int_or_zeroK   s   r   )r   r   r   N)r   r   r   r   )r   r   r   r   )r   r   r   r   )	Ú__doc__Ú
__future__r   Útypingr   r   r   r   Ú__all__r   r   r   r   Ú<module>   s    

#
