
    c                          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 G d
 de      Zy# e$ r efZeZY w xY w)z[This module provides useful math functions on top of Python's
built-in :mod:`math` module.
    )division)ceilfloorNz-infinfc                 \    ||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.12/site-packages/boltons/mathutils.pyclampr   +   s5    4 u}!5* + 	+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
    z,no ceil options greater than or equal to: %r)_ceilsortedbisectbisect_leftlenr	   r   optionsis      r   r   r   K   sT      QxWoG7A&ACLG!K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

    z*no floor options less than or equal to: %r   )_floorr   r   bisect_rightr	   r   s      r   r   r   d   sP    " ayWoGGQ'AEI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y)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   Nc                    t        |      t        vrt        |      t        u r#dj                  |D cg c]  }|rdnd
 c}      }t        |      t        u r|j                  d      }t        |      t        u rU|$t        |      }|j                  d      r|dz
  dz  }|j                  d      rt        |d      }n|rt        |d      }nd	}t        |      t        vr-t        d
j                  t        |      j                              |d	k  rt        d      |t        dj                  |            }|d|z  kD  rt        dj                  ||            || _        || _        y c c}w )N 10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_es       r   __init__zBits.__init__   sB   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STWY]^__1 ?s   E*c                 0   t        |      t        u rt        | j                         |         S t        |      t        u rE|| j
                  k\  rt        |      t        d| j
                  |z
  dz
  z  | j                  z        S t        t        |            )Nr   )
r+   slicer    as_binr3   r   
IndexErrorboolr!   r4   )r7   ks     r   __getitem__zBits.__getitem__   sy    7ea())7c>DHH} m#txx!|a/0DHH<==Q  r   c                     | j                   S N)r   r7   s    r   __len__zBits.__len__       xxr   c                     t        |       t        |      urt        S | j                  |j                  k(  xr | j                  |j                  k(  S rC   )r+   NotImplementedr!   r   r7   others     r   __eq__zBits.__eq__   s=    :T%[(!!xx599$>UYY)>>r   c                     t        |       t        |      urt        S t        | j                  |j                  z  t	        | j
                  |j
                              S rC   r+   rH   r    r!   r   r   rI   s     r   __or__zBits.__or__   A    :T%[(!!DHHuyy(#dhh		*BCCr   c                     t        |       t        |      urt        S t        | j                  |j                  z  t	        | j
                  |j
                              S rC   rM   rI   s     r   __and__zBits.__and__   rO   r   c                 N    t        | j                  |z  | j                  |z         S rC   r    r!   r   rI   s     r   
__lshift__zBits.__lshift__   !    DHH%txx%'788r   c                 N    t        | j                  |z	  | j                  |z
        S rC   rS   rI   s     r   
__rshift__zBits.__rshift__   rU   r   c                 ,    t        | j                        S rC   )hashr!   rD   s    r   __hash__zBits.__hash__   s    DHH~r   c                 N    | j                         D cg c]  }|dk(  	 c}S c c}w )Nr$   )r=   )r7   cs     r   as_listzBits.as_list   s     "&++-0QS000s   "c                 j    dj                  | j                        j                  | j                        S )Nz{{0:0{0}b}}r5   r   r!   rD   s    r   r=   zBits.as_bin   s&    ##DHH-44TXX>>r   c                     dj                  d| j                  dz  | j                  dz  dk7  z   z        }|| j                  z  }|S )Nz%0{0}Xr(      r   r_   )r7   tmplrets      r   as_hexzBits.as_hex   sA    qDHHMdhhlq5H$IJKTXXo
r   c                     | j                   S rC   )r!   rD   s    r   as_intzBits.as_int   rF   r   c                 H    t        j                  | j                               S rC   )binascii	unhexlifyrd   rD   s    r   as_byteszBits.as_bytes   s    !!$++-00r   c                      | |      S rC    )clslist_s     r   	from_listzBits.from_list   s    5zr   c                      | |      S rC   rl   )rm   bins     r   from_binzBits.from_bin   s    3xr   c                     t        |t              r|j                  d      }|j                  d      sd|z   } | |      S )Nr&   r'   )
isinstancer/   r0   r2   )rm   hexs     r   from_hexzBits.from_hex   s8    c5!**W%C~~d#*C3xr   c                      | ||      S rC   rl   )rm   int_r8   s      r   from_intzBits.from_int   s    4r   c                 J    | j                  t        j                  |            S rC   )rv   rh   hexlify)rm   bytes_s     r   
from_byteszBits.from_bytes   s    ||H,,V455r   c                 n    | j                   j                  }dj                  || j                               S )Nz
{0}('{1}'))	__class__r6   r5   r=   )r7   cns     r   __repr__zBits.__repr__  s*    ^^$$""2t{{}55r   )r   NrC   )r6   
__module____qualname____doc__	__slots__r:   rA   rE   rK   rN   rQ   rT   rW   rZ   r]   r=   rd   rf   rj   classmethodro   rr   rv   ry   r}   r   rl   r   r   r    r       s     I:!?
D
D
991?1         6 66r   r    rC   )r   
__future__r   mathr   r   r   r   r   rh   floatr   r3   longr,   strr/   	NameErrorr1   objectr    rl   r   r   <module>r      sv   B   /   =e %@26tJE}66 }6  JGs   A 
AA