
    .Ph                         d 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 G d	 d
          Z
dS )z[This module provides useful math functions on top of Python's
built-in :mod:`math` module.
    )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      Q/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/boltons/mathutils.pyclampr   (   sK    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   H   s`      QxxWooG7A&&ACLL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   a   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                    t          |          t          u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          ur$t          d	t          |          j
                   |dk     rt          d
          |t          |d          }|d|z  k    rt          d| d| 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: 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_s      r   __init__zBits.__init__   s   99CCyyD  gg>>#>>>??CyyE!!jj))CyyC<s88D~~d++ . $qA~>>$''  c2,,CC  !#qkkCyy## Rd3ii>P R RSSS77DEEE<#zz??Dd??RcRRtRRRSSS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_binr1   r   
IndexErrorboolr    r8   )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 rE   )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 rE   r0   rJ   r   r    r
   r   rK   s     r   __or__zBits.__or__   F    ::T%[[((!!DHuy(#dh	*B*BCCCr   c                     t          |           t          |          urt          S t          | j        |j        z  t	          | j        |j                            S rE   rO   rK   s     r   __and__zBits.__and__   rQ   r   c                 B    t          | j        |z  | j        |z             S rE   r   r    r   rK   s     r   
__lshift__zBits.__lshift__        DH%tx%'7888r   c                 B    t          | j        |z	  | j        |z
            S rE   rU   rK   s     r   
__rshift__zBits.__rshift__   rW   r   c                 *    t          | j                  S rE   )hashr    rF   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   )r?   rF   s    r   as_listzBits.as_list   s    00$++--0000r   c                 H    d| j          d                    | j                  S )Nz{0:0zb})r   formatr    rF   s    r   r?   zBits.as_bin   s%    $tx$$$++DH555r   c                 Z    dd| j         dz  | j         dz  dk    z   z   d}|| j        z  }|S )Nz%0r,      r   X)r   r    )r:   tmplrets      r   as_hexzBits.as_hex   s=    BAQ48a<A*=>?BBBTXo
r   c                     | j         S rE   )r    rF   s    r   as_intzBits.as_int   rH   r   c                 N    t          j        |                                           S rE   )binascii	unhexlifyrh   rF   s    r   as_byteszBits.as_bytes   s    !$++--000r   c                      | |          S rE   r&   )clslist_s     r   	from_listzBits.from_list   s    s5zzr   c                      | |          S rE   r&   )rp   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   )rp   hexs     r   from_hexzBits.from_hex   sO    c5!! 	&**W%%C~~d## 	*Cs3xxr   c                      | ||          S rE   r&   )rp   int_r;   s      r   from_intzBits.from_int   s    s4r   c                 P    |                      t          j        |                    S rE   )ry   rl   hexlify)rp   bytes_s     r   
from_byteszBits.from_bytes   s    ||H,V44555r   c                 N    | j         j        }| d|                                  dS )Nz('z'))	__class__r9   r?   )r:   cns     r   __repr__zBits.__repr__   s+    ^$))))))r   )r   NrE   )r9   
__module____qualname____doc__	__slots__r<   rC   rG   rM   rP   rS   rV   rY   r\   r`   r?   rh   rj   rn   classmethodrr   ru   ry   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6 6 6    1 1 1   [   [   [    [ 6 6 [6* * * * *r   r   rE   )r   mathr   r   r   r   r   rl   floatr   r   r&   r   r   <module>r      s   >  0 / / / / / / /   5==e % % % %@   2   6}* }* }* }* }* }* }* }* }* }*r   