
    Vi%                          S r SSKJr  SSKJrJr  SSKrSSK	r	\
" S5      \
" S5      4S jrSS jrSS	 jr \\4r\r " S
 S\5      rg! \ a    \4r\r Nf = f)z[This module provides useful math functions on top of Python's
built-in :mod:`math` module.
    )division)ceilfloorNz-infinfc                 ^    X!:  a  [        SU< SU< S35      e[        [        X5      U5      $ )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      d/home/james-whalen/.local/share/pipx/venvs/semgrep/lib/python3.13/site-packages/boltons/mathutils.pyclampr   +   s1    4 }!5* + 	+s1}e$$    c                     Uc  [        U 5      $ [        U5      n[        R                  " X5      nU[	        U5      :X  a  [        SU -  5      eX   $ )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
z,no ceil options greater than or equal to: %r)_ceilsortedbisectbisect_leftlenr	   r   optionsis      r   r   r   K   sP      QxWoG7&ACLG!KLL:r   c                     Uc  [        U 5      $ [        U5      n[        R                  " X5      nU(       d  [	        SU -  5      eX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

z*no floor options less than or equal to: %r   )_floorr   r   bisect_rightr	   r   s      r   r   r   d   sL    " ayWoGG'AEIJJq5>r   c                       \ rS rSrSrSrSS jrS rS rS r	S	 r
S
 rS rS rS rS rS rS rS rS r\S 5       r\S 5       r\S 5       r\SS j5       r\S 5       rS rSrg)Bits   a  
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   Nc                    [        U5      [        ;  Ga  [        U5      [        L a*  SR                  U Vs/ s H  o3(       a  SOSPM     sn5      n[        U5      [        L a  UR                  S5      n[        U5      [        L ae  Uc)  [        U5      nUR                  S5      (       a  US-
  S-  nUR                  S5      (       a  [        US5      nOU(       a  [        US5      nOS	n[        U5      [        ;  a-  [        S
R                  [        U5      R                  5      5      eUS	:  a  [        S5      eUc  [        SR                  U5      5      nUSU-  :  a  [        SR                  X5      5      eXl        X l        g s  snf )N 10ascii0x         r   zinitialized with bad type: {0}z%Bits cannot represent negative values{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_es       r   __init__Bits.__init__   sE   9J&CyD gg#>#QasS0#>?CyE!jj)CyG#<s8D~~d++ $qA~>>$''c2,C!#qkCy
* @ G GS	HZHZ [\\7DEE<w~~c*+Dd?LSSTW^__1 ?s   F	c                 4   [        U5      [        L a  [        U R                  5       U   5      $ [        U5      [        L aE  XR
                  :  a  [        U5      e[        SU R
                  U-
  S-
  -  U R                  -  5      $ [        [        U5      5      e)Nr   )
r-   slicer    as_binr5   r   
IndexErrorboolr"   r6   )r9   ks     r   __getitem__Bits.__getitem__   sw    7ea())7c>HH} m#txx!|a/0DHH<==Q  r   c                     U R                   $ N)r   r9   s    r   __len__Bits.__len__       xxr   c                     [        U 5      [        U5      La  [        $ U R                  UR                  :H  =(       a    U R                  UR                  :H  $ rG   )r-   NotImplementedr"   r   r9   others     r   __eq__Bits.__eq__   s=    :T%[(!!xx599$>UYY)>>r   c                     [        U 5      [        U5      La  [        $ [        U R                  UR                  -  [	        U R
                  UR
                  5      5      $ rG   r-   rM   r    r"   r   r   rN   s     r   __or__Bits.__or__   A    :T%[(!!DHHuyy(#dhh		*BCCr   c                     [        U 5      [        U5      La  [        $ [        U R                  UR                  -  [	        U R
                  UR
                  5      5      $ rG   rS   rN   s     r   __and__Bits.__and__   rV   r   c                 N    [        U R                  U-  U R                  U-   5      $ rG   r    r"   r   rN   s     r   
__lshift__Bits.__lshift__   !    DHH%txx%'788r   c                 N    [        U R                  U-	  U R                  U-
  5      $ rG   r[   rN   s     r   
__rshift__Bits.__rshift__   r^   r   c                 ,    [        U R                  5      $ rG   )hashr"   rH   s    r   __hash__Bits.__hash__   s    DHH~r   c                 h    SR                  U R                  5       Vs/ s H  oS:H  PM	     sn$ s  snf )Nr,   r%   )r7   r"   )r9   cs     r   as_listBits.as_list   s+    ")..":;":QS":;;;s   /c                 j    SR                  U R                  5      R                  U R                  5      $ )Nz{{0:0{0}b}}r7   r   r"   rH   s    r   r@   Bits.as_bin   s&    ##DHH-44TXX>>r   c                     SR                  SU R                  S-  U R                  S-  S:g  -   -  5      nXR                  -  nU$ )Nz%0{0}Xr)      r   rk   )r9   tmplrets      r   as_hexBits.as_hex   s?    qDHHMdhhlq5H$IJKXXo
r   c                     U R                   $ rG   )r"   rH   s    r   as_intBits.as_int   rK   r   c                 J    [         R                  " U R                  5       5      $ rG   )binascii	unhexlifyrq   rH   s    r   as_bytesBits.as_bytes   s    !!$++-00r   c                     U " U5      $ rG    )clslist_s     r   	from_listBits.from_list   s    5zr   c                     U " U5      $ rG   r|   )r}   bins     r   from_binBits.from_bin   s    3xr   c                     [        U[        5      (       a  UR                  S5      nUR                  S5      (       d  SU-   nU " U5      $ )Nr'   r(   )
isinstancer1   r2   r4   )r}   hexs     r   from_hexBits.from_hex   s>    c5!!**W%C~~d##*C3xr   c                     U " X5      $ rG   r|   )r}   int_r:   s      r   from_intBits.from_int   s    4r   c                 L    U R                  [        R                  " U5      5      $ rG   )r   rw   hexlify)r}   bytes_s     r   
from_bytesBits.from_bytes   s    ||H,,V455r   c                 l    U R                   R                  nSR                  XR                  5       5      $ )Nz
{0}('{1}'))	__class__r8   r7   r@   )r9   cns     r   __repr__Bits.__repr__  s(    ^^$$""2{{}55r   )r   r"   )r   NrG   )r8   
__module____qualname____firstlineno____doc__	__slots__r<   rD   rI   rP   rT   rX   r\   r`   rd   rh   r@   rq   rt   ry   classmethodr   r   r   r   r   r   __static_attributes__r|   r   r   r    r       s     I:!?
D
D
99<?1         6 66r   r    rG   )r   
__future__r   mathr   r   r   r   r   rw   floatr   r5   longr.   strr1   	NameErrorr3   objectr    r|   r   r   <module>r      sv   B   /   =e %@26tJE}66 }6  JGs   A	 	AA