
    fch                     v    S 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 " S	 S
5      r
g)z[This module provides useful math functions on top of Python's
built-in :mod:`math` module.
    )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      1lib/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   H   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   a   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      [        L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      [        La!  [        S
[        U5      R                   35      eUS	:  a  [        S5      eUc  [        US 5      nUSU-  :  a  [        SU SU S35      eXl        X l        g s  snf )N 10ascii0x         r   zinitialized with bad type: z%Bits cannot represent negative valuesbzvalue z cannot be represented with z bits)typeintlistjoinbytesdecodestrr   
startswith	TypeError__name__r   r!   )selfr!   len_es       r   __init__Bits.__init__   s?   9CCyD gg#>#QasS0#>?CyE!jj)CyC<s8D~~d++ $qA~>>$''c2,C!#qkCy#"=d3i>P>P=Q RSS7DEE<#a?Dd?vcU*FtfERSS1 ?s   E&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_binr-   r   
IndexErrorboolr!   r4   )r6   ks     r   __getitem__Bits.__getitem__   sw    7ea())7c>HH} m#txx!|a/0DHH<==Q  r   c                     U R                   $ N)r   r6   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  $ rD   )r,   NotImplementedr!   r   r6   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      $ rD   r,   rJ   r   r!   r
   r   rK   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      $ rD   rP   rK   s     r   __and__Bits.__and__   rS   r   c                 N    [        U R                  U-  U R                  U-   5      $ rD   r   r!   r   rK   s     r   
__lshift__Bits.__lshift__   !    DHH%txx%'788r   c                 N    [        U R                  U-	  U R                  U-
  5      $ rD   rX   rK   s     r   
__rshift__Bits.__rshift__   r[   r   c                 ,    [        U R                  5      $ rD   )hashr!   rE   s    r   __hash__Bits.__hash__   s    DHH~r   c                 R    U R                  5        Vs/ s H  oS:H  PM	     sn$ s  snf )Nr$   )r=   )r6   cs     r   as_listBits.as_list   s"    "&++-0-QS-000s   $c                 T    SU R                    S3R                  U R                  5      $ )Nz{0:0zb})r   formatr!   rE   s    r   r=   Bits.as_bin   s$    txxj$++DHH55r   c                 t    SSU R                   S-  U R                   S-  S:g  -   -   S3nXR                  -  nU$ )Nz%0r(      r   Xr   r!   )r6   tmplrets      r   as_hexBits.as_hex   s@    AQ488a<A*=>?@BXXo
r   c                     U R                   $ rD   )r!   rE   s    r   as_intBits.as_int   rH   r   c                 J    [         R                  " U R                  5       5      $ rD   )binascii	unhexlifyrp   rE   s    r   as_bytesBits.as_bytes   s    !!$++-00r   c                     U " U5      $ rD    )clslist_s     r   	from_listBits.from_list   s    5zr   c                     U " U5      $ rD   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'   )
isinstancer0   r1   r3   )r|   hexs     r   from_hexBits.from_hex   s>    c5!!**W%C~~d##*C3xr   c                     U " X5      $ rD   r{   )r|   int_r7   s      r   from_intBits.from_int   s    4r   c                 L    U R                  [        R                  " U5      5      $ rD   )r   rv   hexlify)r|   bytes_s     r   
from_bytesBits.from_bytes   s    ||H,,V455r   c                 Z    U R                   R                  nU SU R                  5        S3$ )Nz('z'))	__class__r5   r=   )r6   cns     r   __repr__Bits.__repr__   s*    ^^$$Rb))r   rm   )r   NrD   )r5   
__module____qualname____firstlineno____doc__	__slots__r9   rA   rF   rM   rQ   rU   rY   r]   ra   re   r=   rp   rs   rx   classmethodr~   r   r   r   r   r   __static_attributes__r{   r   r   r   r   |   s     I:!?
D
D
99161         6 6*r   r   rD   )r   mathr   r   r   r   r   rv   floatr   r   r{   r   r   <module>r      s@   > 0   =e %@26}* }*r   