
    qc                          d Z ddlmZ ddlmZmZ ddlZddl	Z	 e
d           e
d          fdZddZdd	Z	 eefZeZn# e$ r efZeZY nw xY w G d
 de          ZdS )z[This module provides useful math functions on top of Python's
built-in :mod:`math` module.
    )division)ceilfloorNz-infinfc                 v    ||k     rt          d|d|d          t          t          | |          |          S )a\  Limit a value to a given range.

    Args:
        x (int or float): Number to be clamped.
        lower (int or float): Minimum value for x.
        upper (int or float): Maximum value for x.

    The returned value is guaranteed to be between *lower* and
    *upper*. Integers, floats, and other comparable types can be
    mixed.

    >>> clamp(1.0, 0, 5)
    1.0
    >>> clamp(-1.0, 0, 5)
    0
    >>> clamp(101.0, 0, 5)
    5
    >>> clamp(123, upper=5)
    5

    Similar to `numpy's clip`_ function.

    .. _numpy's clip: http://docs.scipy.org/doc/numpy/reference/generated/numpy.clip.html

    zexpected upper bound (z) >= lower bound ())
ValueErrorminmax)xloweruppers      1lib/python3.11/site-packages/boltons/mathutils.pyclampr   +   sN    4 u} +j!EE555* + + 	+s1e}}e$$$    c                     |t          |           S t          |          }t          j        ||           }|t	          |          k    rt          d| z            ||         S )a  Return the ceiling of *x*. If *options* is set, return the smallest
    integer or float from *options* that is greater than or equal to
    *x*.

    Args:
        x (int or float): Number to be tested.
        options (iterable): Optional iterable of arbitrary numbers
          (ints or floats).

    >>> VALID_CABLE_CSA = [1.5, 2.5, 4, 6, 10, 25, 35, 50]
    >>> ceil(3.5, options=VALID_CABLE_CSA)
    4
    >>> ceil(4, options=VALID_CABLE_CSA)
    4
    Nz,no ceil options greater than or equal to: %r)_ceilsortedbisectbisect_leftlenr	   r   optionsis      r   r   r   K   sf       QxxWooG7A&&ACLL MG!KLLL1:r   c                     |t          |           S t          |          }t          j        ||           }|st	          d| z            ||dz
           S )a  Return the floor of *x*. If *options* is set, return the largest
    integer or float from *options* that is less than or equal to
    *x*.

    Args:
        x (int or float): Number to be tested.
        options (iterable): Optional iterable of arbitrary numbers
          (ints or floats).

    >>> VALID_CABLE_CSA = [1.5, 2.5, 4, 6, 10, 25, 35, 50]
    >>> floor(3.5, options=VALID_CABLE_CSA)
    2.5
    >>> floor(2.5, options=VALID_CABLE_CSA)
    2.5

    Nz*no floor options less than or equal to: %r   )_floorr   r   bisect_rightr	   r   s      r   r   r   d   s`    "  ayyWooGGQ''A KEIJJJ1q5>r   c                       e Zd ZdZdZddZd Zd Zd Zd	 Z	d
 Z
d Zd Zd Zd Zd Zd Zd Zd Zed             Zed             Zed             Zedd            Zed             Zd ZdS )Bitsa  
    An immutable bit-string or bit-array object.
    Provides list-like access to bits as bools,
    as well as bitwise masking and shifting operators.
    Bits also make it easy to convert between many
    different useful representations:

    * bytes -- good for serializing raw binary data
    * int -- good for incrementing (e.g. to try all possible values)
    * list of bools -- good for iterating over or treating as flags
    * hex/bin string -- good for human readability

    )valr   r   Nc                 l   t          |          t          vr)t          |          t          u rd                    d |D                       }t          |          t          u r|                    d          }t          |          t          u ri|,t          |          }|                    d          r|dz
  dz  }|                    d          rt          |d          }n|rt          |d          }nd}t          |          t          vr4t          d	                    t          |          j                            |dk     rt          d
          |"t          d                    |                    }|d|z  k    r#t          d                    ||                    || _        || _        d S )N c                     g | ]}|rd nd	S )10 ).0es     r   
<listcomp>z!Bits.__init__.<locals>.<listcomp>   s!    >>>Qa0ssS>>>r   ascii0x         r   zinitialized with bad type: {0}z%Bits cannot represent negative valuesz{0:b}z-value {0} cannot be represented with {1} bits)type
_int_typeslistjoinbytesdecodeunicoder   
startswithint	TypeErrorformat__name__r	   r!   )selfr!   len_s      r   __init__zBits.__init__   s   99J& 	]CyyD  @gg>>#>>>??CyyE! *jj))CyyG#   .s88D~~d++ . $qA~>>$''  c2,,CC  !#qkkCyy
* ] @ G GS		HZ [ [\\\7 	FDEEE 	,w~~c**++Dd? 	`LSSTWY]^^___r   c                 ^   t          |          t          u r't          |                                 |                   S t          |          t          u r?|| j        k    rt          |          t          d| j        |z
  dz
  z  | j        z            S t          t          |                    )Nr   )
r0   slicer    as_binr8   r   
IndexErrorboolr!   r9   )r<   ks     r   __getitem__zBits.__getitem__   s    77e 	*a()))77c> 	>DH} $ mm#tx!|a/0DH<===Q   r   c                     | j         S N)r   r<   s    r   __len__zBits.__len__   	    xr   c                     t          |           t          |          urt          S | j        |j        k    o| j        |j        k    S rG   )r0   NotImplementedr!   r   r<   others     r   __eq__zBits.__eq__   s>    ::T%[[( 	"!!x59$>UY)>>r   c                     t          |           t          |          urt          S t          | j        |j        z  t	          | j        |j                            S rG   r0   rL   r    r!   r   r   rM   s     r   __or__zBits.__or__   H    ::T%[[( 	"!!DHuy(#dh	*B*BCCCr   c                     t          |           t          |          urt          S t          | j        |j        z  t	          | j        |j                            S rG   rQ   rM   s     r   __and__zBits.__and__   rS   r   c                 B    t          | j        |z  | j        |z             S rG   r    r!   r   rM   s     r   
__lshift__zBits.__lshift__        DH%tx%'7888r   c                 B    t          | j        |z	  | j        |z
            S rG   rW   rM   s     r   
__rshift__zBits.__rshift__   rY   r   c                 *    t          | j                  S rG   )hashr!   rH   s    r   __hash__zBits.__hash__   s    DH~~r   c                 >    d |                                  D             S )Nc                     g | ]}|d k    	S )r%   r'   )r(   cs     r   r*   z Bits.as_list.<locals>.<listcomp>   s    000QS000r   )rA   rH   s    r   as_listzBits.as_list   s    00$++--0000r   c                 f    d                     | j                                       | j                  S )Nz{{0:0{0}b}}r:   r   r!   rH   s    r   rA   zBits.as_bin   s(    ##DH--44TX>>>r   c                 x    d                     d| j        dz  | j        dz  dk    z   z            }|| j        z  }|S )Nz%0{0}Xr-      r   rd   )r<   tmplrets      r   as_hexzBits.as_hex   s>    qDHMdhlq5H$IJKKTXo
r   c                     | j         S rG   )r!   rH   s    r   as_intzBits.as_int   rJ   r   c                 N    t          j        |                                           S rG   )binascii	unhexlifyri   rH   s    r   as_byteszBits.as_bytes   s    !$++--000r   c                      | |          S rG   r'   )clslist_s     r   	from_listzBits.from_list   s    s5zzr   c                      | |          S rG   r'   )rq   bins     r   from_binzBits.from_bin   s    s3xxr   c                     t          |t                    r|                    d          }|                    d          sd|z   } | |          S )Nr+   r,   )
isinstancer4   r5   r7   )rq   hexs     r   from_hexzBits.from_hex   sO    c5!! 	&**W%%C~~d## 	*Cs3xxr   c                      | ||          S rG   r'   )rq   int_r=   s      r   from_intzBits.from_int   s    s4r   c                 P    |                      t          j        |                    S rG   )rz   rm   hexlify)rq   bytes_s     r   
from_byteszBits.from_bytes   s    ||H,V44555r   c                 j    | j         j        }d                    ||                                           S )Nz
{0}('{1}'))	__class__r;   r:   rA   )r<   cns     r   __repr__zBits.__repr__  s*    ^$""2t{{}}555r   )r   NrG   )r;   
__module____qualname____doc__	__slots__r>   rE   rI   rO   rR   rU   rX   r[   r^   rb   rA   ri   rk   ro   classmethodrs   rv   rz   r}   r   r   r'   r   r   r    r       s         I   :! ! !  ? ? ?
D D D
D D D
9 9 99 9 9  1 1 1? ? ?    1 1 1   [   [   [    [ 6 6 [66 6 6 6 6r   r    rG   )r   
__future__r   mathr   r   r   r   r   rm   floatr   r8   longr1   strr4   	NameErrorr6   objectr    r'   r   r   <module>r      s   B         / / / / / / / /   5==e % % % %@   2   6tJEE   JGGG}6 }6 }6 }6 }66 }6 }6 }6 }6 }6s   A 
AA