
    DhJ                        d Z ddlZddlZddlZddlmZ ej                  rddlZ ej                  d      Z	 ej                  d      Z
ej                  e	e
f   Zej                  ej                  de	f      Zej                  ej                  de
f      Z ej                  dej"                  	      Z ej                  d
      Zej(                  ej*                  e	e
f   ej,                  ej.                  e	e
f      ej,                  ej*                  e	e
f      df   Zej2                  d   Z G d dej                  e	e
f   ej6                        Z G d dee	e
f         Z G d dee	e
f         Z G d dej>                  e         Z  G d dejB                  e   ejD                  e         Z#e$dk(  rddl%Z% e%jL                          yy)aB  
This module provides custom container classes with enhanced functionality.

Classes:
    CastedDictBase: Abstract base class for dictionaries that cast keys and
        values.
    CastedDict: Dictionary that casts keys and values to specified types.
    LazyCastedDict: Dictionary that lazily casts values to specified types upon
        access.
    UniqueList: List that only allows unique values, with configurable behavior
        on duplicates.
    SliceableDeque: Deque that supports slicing and enhanced equality checks.

Type Aliases:
    KT: Type variable for dictionary keys.
    VT: Type variable for dictionary values.
    DT: Type alias for a dictionary with keys of type KT and values of type VT.
    KT_cast: Type alias for a callable that casts dictionary keys.
    VT_cast: Type alias for a callable that casts dictionary values.
    HT: Type variable for hashable values in UniqueList.
    T: Type variable for generic types.
    DictUpdateArgs: Union type for arguments that can be used to update a
        dictionary.
    OnDuplicate: Literal type for handling duplicate values in UniqueList.

Usage:
    - CastedDict and LazyCastedDict can be used to create dictionaries with
        automatic type casting.
    - UniqueList ensures all elements are unique and can raise an error on
        duplicates.
    - SliceableDeque extends deque with slicing support and enhanced equality
        checks.

Examples:
    >>> d = CastedDict(int, int)
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, 3: 4, 5: 6, 7: 8}

    >>> l = UniqueList(1, 2, 3)
    >>> l.append(4)
    >>> l.append(4)
    >>> l.insert(0, 4)
    >>> l.insert(0, 5)
    >>> l[1] = 10
    >>> l
    [5, 10, 2, 3, 4]

    >>> d = SliceableDeque([1, 2, 3, 4, 5])
    >>> d[1:4]
    SliceableDeque([2, 3, 4])
    N   )typesKTVT.HT)boundTz(_typeshed.SupportsKeysAndGetItem[KT, VT])ignoreraisec                       e Zd ZU dZee   ed<   ee   ed<   	 	 ddee   dee   de	eef   ded	df
d
Z
de	ej                  ej                  f   dej                  d	dfdZdej                  dej                  d	df fdZ xZS )CastedDictBasea4  
    Abstract base class for dictionaries that cast keys and values.

    Attributes:
        _key_cast (KT_cast[KT]): Callable to cast dictionary keys.
        _value_cast (VT_cast[VT]): Callable to cast dictionary values.

    Methods:
        __init__(key_cast: KT_cast[KT] = None, value_cast: VT_cast[VT] = None,
            *args: DictUpdateArgs[KT, VT], **kwargs: VT) -> None:
            Initializes the dictionary with optional key and value casting
            callables.
        update(*args: DictUpdateArgs[types.Any, types.Any],
            **kwargs: types.Any) -> None:
            Updates the dictionary with the given arguments.
        __setitem__(key: types.Any, value: types.Any) -> None:
            Sets the item in the dictionary, casting the key if a key cast
            callable is provided.
    	_key_cast_value_castNkey_cast
value_castargskwargsreturnc                 D    || _         || _         | j                  |i | y)a  
        Initializes the CastedDictBase with optional key and value
        casting callables.

        Args:
            key_cast (KT_cast[KT], optional): Callable to cast
                dictionary keys. Defaults to None.
            value_cast (VT_cast[VT], optional): Callable to cast
                dictionary values. Defaults to None.
            *args (DictUpdateArgs[KT, VT]): Arguments to initialize
                the dictionary.
            **kwargs (VT): Keyword arguments to initialize the
                dictionary.
        N)r   r   update)selfr   r   r   r   s        y/var/www/fastuser/data/www/generator.snapmosaic.io/flask_app/venv/lib/python3.12/site-packages/python_utils/containers.py__init__zCastedDictBase.__init__u   s&    * &!T$V$    c                 f    |r |j                   |  |r|j                         D ]
  \  }}|| |<    yy)a  
        Updates the dictionary with the given arguments.

        Args:
            *args (DictUpdateArgs[types.Any, types.Any]): Arguments to update
                the dictionary.
            **kwargs (types.Any): Keyword arguments to update the dictionary.
        N)r   items)r   r   r   keyvalues        r   r   zCastedDictBase.update   s?     FMM4 $lln "
U!S	" r   r   r   c                 ^    | j                   | j                  |      }t        | 	  ||      S )
        Sets the item in the dictionary, casting the key if a key cast
        callable is provided.

        Args:
            key (types.Any): The key to set in the dictionary.
            value (types.Any): The value to set in the dictionary.
        r   super__setitem__r   r   r   	__class__s      r   r#   zCastedDictBase.__setitem__   s/     >>%..%Cw"3..r   )NN)__name__
__module____qualname____doc__KT_castr   __annotations__VT_castr   DictUpdateArgsr   r   Anyr   r#   __classcell__r%   s   @r   r   r   ]   s    ( r{ !%"&%"+% BK% b"f%	%
 % 
%2"#EIIuyy$89"EJYY"	"$/uyy / /t / /r   r   c                   X     e Zd ZdZdej
                  dej
                  ddf fdZ xZS )
CastedDicta7  
    Custom dictionary that casts keys and values to the specified typing.

    Note that you can specify the types for mypy and type hinting with:
    CastedDict[int, int](int, int)

    >>> d: CastedDict[int, int] = CastedDict(int, int)
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, 3: 4, 5: 6, 7: 8}
    >>> list(d.keys())
    [1, 3, 5, 7]
    >>> list(d)
    [1, 3, 5, 7]
    >>> list(d.values())
    [2, 4, 6, 8]
    >>> list(d.items())
    [(1, 2), (3, 4), (5, 6), (7, 8)]
    >>> d[3]
    4

    # Casts are optional and can be disabled by passing None as the cast
    >>> d = CastedDict()
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, '3': '4', '5': '6', '7': '8'}
    r   r   r   Nc                 `    | j                   | j                  |      }t        | 	  ||       y)z.Sets `key` to `cast(value)` in the dictionary.N)r   r"   r#   r$   s      r   r#   zCastedDict.__setitem__   s.    '$$U+EC'r   )r&   r'   r(   r)   typingr.   r#   r/   r0   s   @r   r2   r2      s0     D(vzz (&** ( ( (r   r2   c                        e Zd ZdZdej
                  dej
                  ddf fdZdej
                  def fdZdej                  ej                  eef   ddf   f fdZdej                  eddf   f fd	Z xZS )
LazyCastedDicta  
    Custom dictionary that casts keys and lazily casts values to the specified
    typing. Note that the values are cast only when they are accessed and
    are not cached between executions.

    Note that you can specify the types for mypy and type hinting with:
    LazyCastedDict[int, int](int, int)

    >>> d: LazyCastedDict[int, int] = LazyCastedDict(int, int)
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, 3: '4', 5: '6', 7: '8'}
    >>> list(d.keys())
    [1, 3, 5, 7]
    >>> list(d)
    [1, 3, 5, 7]
    >>> list(d.values())
    [2, 4, 6, 8]
    >>> list(d.items())
    [(1, 2), (3, 4), (5, 6), (7, 8)]
    >>> d[3]
    4

    # Casts are optional and can be disabled by passing None as the cast
    >>> d = LazyCastedDict()
    >>> d[1] = 2
    >>> d['3'] = '4'
    >>> d.update({'5': '6'})
    >>> d.update([('7', '8')])
    >>> d
    {1: 2, '3': '4', '5': '6', '7': '8'}
    >>> list(d.keys())
    [1, '3', '5', '7']
    >>> list(d.values())
    [2, '4', '6', '8']

    >>> list(d.items())
    [(1, 2), ('3', '4'), ('5', '6'), ('7', '8')]
    >>> d['3']
    '4'
    r   r   r   Nc                 `    | j                   | j                  |      }t        | 	  ||       y)r    Nr!   r$   s      r   r#   zLazyCastedDict.__setitem__  s,     >>%..%CC'r   c                     | j                   | j                  |      }t        | 	  |      }| j                  | j                  |      }|S )a  
        Gets the item from the dictionary, casting the value if a value cast
        callable is provided.

        Args:
            key (types.Any): The key to get from the dictionary.

        Returns:
            VT: The value from the dictionary.
        )r   r"   __getitem__r   r$   s      r   r9   zLazyCastedDict.__getitem__  sM     >>%..%C#C('$$U+Er   c              #      K   | j                   t        | 	         E d{    yt        | 	         D ]  \  }}|| j                  |      f  y7 1w)a  
        Returns a generator of the dictionary's items, casting the values if a
        value cast callable is provided.

        Yields:
            types.Generator[types.Tuple[KT, VT], None, None]: A generator of
                the dictionary's items.
        N)r   r"   r   r$   s      r   r   zLazyCastedDict.items+  sX      #w}&&#gmo 3
U4++E2223 's   AA2Ac              #      K   | j                   t        | 	         E d{    yt        | 	         D ]  }| j                  |        y7 ,w)z
        Returns a generator of the dictionary's values, casting the values if a
        value cast callable is provided.

        Yields:
            types.Generator[VT, None, None]: A generator of the dictionary's
                values.
        N)r   r"   valuesr   r   r%   s     r   r<   zLazyCastedDict.values<  sQ      #w~''') .&&u--. (s   AA-A)r&   r'   r(   r)   r   r.   r#   r   r9   	GeneratorTupler   r   r<   r/   r0   s   @r   r6   r6      s    +Z(uyy ( (t (uyy R *3	RV,dD8	93".D$7 . .r   r6   c                       e Zd ZU dZej
                  e   ed<   dddedef fdZ	dej                  d	ed
df fdZd	ed
df fdZded
efdZej                   dej                  ded
dfd       Zej                   dedej&                  e   d
dfd       Zdej(                  eej                  f   dej(                  ej&                  e   ef   d
df fdZdej(                  ej                  ef   d
df fdZ xZS )
UniqueLista  
    A list that only allows unique values. Duplicate values are ignored by
    default, but can be configured to raise an exception instead.

    >>> l = UniqueList(1, 2, 3)
    >>> l.append(4)
    >>> l.append(4)
    >>> l.insert(0, 4)
    >>> l.insert(0, 5)
    >>> l[1] = 10
    >>> l
    [5, 10, 2, 3, 4]

    >>> l = UniqueList(1, 2, 3, on_duplicate='raise')
    >>> l.append(4)
    >>> l.append(4)
    Traceback (most recent call last):
    ...
    ValueError: Duplicate value: 4
    >>> l.insert(0, 4)
    Traceback (most recent call last):
    ...
    ValueError: Duplicate value: 4
    >>> 4 in l
    True
    >>> l[0]
    1
    >>> l[1] = 4
    Traceback (most recent call last):
    ...
    ValueError: Duplicate value: 4
    _setr
   )on_duplicater   rC   c                ~    || _         t               | _        t        |           |D ]  }| j                  |        y)a  
        Initializes the UniqueList with optional duplicate handling behavior.

        Args:
            *args (HT): Initial values for the list.
            on_duplicate (OnDuplicate, optional): Behavior on duplicates.
                Defaults to 'ignore'.
        N)rC   setrB   r"   r   append)r   rC   r   argr%   s       r   r   zUniqueList.__init__p  s<     )E	 	CKK	r   indexr   r   Nc                     || j                   v r| j                  dk(  rt        d|       y| j                   j                  |       t        |   ||       y)aP  
        Inserts a value at the specified index, ensuring uniqueness.

        Args:
            index (types.SupportsIndex): The index to insert the value at.
            value (HT): The value to insert.

        Raises:
            ValueError: If the value is a duplicate and `on_duplicate` is set
                to 'raise'.
        r   Duplicate value: N)rB   rC   
ValueErroraddr"   insertr   rH   r   r%   s      r   rM   zUniqueList.insert  sS     DII  G+ #4UG!<==		eue$r   c                     || j                   v r| j                  dk(  rt        d|       y| j                   j                  |       t        |   |       y)z
        Appends a value to the list, ensuring uniqueness.

        Args:
            value (HT): The value to append.

        Raises:
            ValueError: If the value is a duplicate and `on_duplicate` is set
                to 'raise'.
        r   rJ   N)rB   rC   rK   rL   r"   rF   r=   s     r   rF   zUniqueList.append  sQ     DII  G+ #4UG!<==		eur   itemc                     || j                   v S )z
        Checks if the list contains the specified item.

        Args:
            item (HT): The item to check for.

        Returns:
            bool: True if the item is in the list, False otherwise.
        )rB   )r   rP   s     r   __contains__zUniqueList.__contains__  s     tyy  r   indicesr<   c                      y N r   rS   r<   s      r   r#   zUniqueList.__setitem__       r   c                      y rU   rV   rW   s      r   r#   zUniqueList.__setitem__  rX   r   c                    t        |t              rt        j                  t        j                  t
           |      }| j                  dk(  rt        d      t        |      | j                  z  }|r|t        | |         k7  rt        d|       | j                  j                  |       nit        j                  t
        |      }|| j                  v r&|| |   k7  r| j                  dk(  rt        d|       y| j                  j                  |       t        | =  t        j                  t        |      t        j                  t        j                   t
           |             y)a  
        Sets the item(s) at the specified index/indices, ensuring uniqueness.

        Args:
            indices (types.Union[slice, types.SupportsIndex]): The index or
                slice to set the value(s) at.
            values (types.Union[types.Iterable[HT], HT]): The value(s) to set.

        Raises:
            RuntimeError: If `on_duplicate` is 'ignore' and setting slices.
            ValueError: If the value(s) are duplicates and `on_duplicate` is
                set to 'raise'.
        r
   z^ignore mode while setting slices introduces ambiguous behaviour and is therefore not supportedzDuplicate values: r   rJ   N)
isinstanceslicer   castIterabler   rC   RuntimeErrorrE   rB   listrK   r   rL   r"   r#   List)r   rS   r<   
duplicatesr%   s       r   r#   zUniqueList.__setitem__  s   $ gu%ZZr 2F;F  H,"? 
 ),Fdii(?JfT'](;; #5j\!BCCIIV$ZZF+F"vg'>$$/$'8%ABBIIMM&!JJug&

5::b>6(J	
r   c                     t        |t              r&| |   D ]  }| j                  j                  |        n| j                  j                  | |          t        |   |       y)z
        Deletes the item(s) at the specified index/indices.

        Args:
            index (types.Union[types.SupportsIndex, slice]): The index or slice
                to delete the item(s) at.
        N)r[   r\   rB   remover"   __delitem__rN   s      r   re   zUniqueList.__delitem__  sY     eU#e (		  '( IIT%[)E"r   )r&   r'   r(   r)   r   Setr   r+   OnDuplicater   SupportsIndexrM   rF   boolrR   r4   overloadr#   r\   r^   Unionre   r/   r0   s   @r   rA   rA   L  se   B ))B-
 %- "&%E// % %t %*B 4 (
! 
! 
! __**46	  __&+nnR&8	 +
UE$7$778+
 ENN2.23+
 
	+
Z#[[!4!4e!;<#	# #r   rA   c                   *    e Zd ZdZej
                  dej                  defd       Z	ej
                  de
ddfd       Z	dej                  ej                  e
f   dej                  edf   f fdZ	dej                  def fd	Zddedef fd
Z xZS )SliceableDequea9  
    A deque that supports slicing and enhanced equality checks.

    Methods:
        __getitem__(index: types.Union[types.SupportsIndex, slice]) ->
            types.Union[T, 'SliceableDeque[T]']:
            Returns the item or slice at the given index.
        __eq__(other: types.Any) -> bool:
            Checks equality with another object, allowing for comparison with
             lists, tuples, and sets.
        pop(index: int = -1) -> T:
            Removes and returns the item at the given index. Only supports
            index 0 and the last index.
    rH   r   c                      y rU   rV   r   rH   s     r   r9   zSliceableDeque.__getitem__  s    <?r   zSliceableDeque[T]c                      y rU   rV   ro   s     r   r9   zSliceableDeque.__getitem__  s    @Cr   c                      t        |t              rC|j                  t                     \  }}} j	                   fdt        |||      D              S t           |      S )a-  
        Return the item or slice at the given index.

        Args:
            index (types.Union[types.SupportsIndex, slice]): The index or
             slice to retrieve.

        Returns:
            types.Union[T, 'SliceableDeque[T]']: The item or slice at the
            given index.

        Examples:
            >>> d = SliceableDeque[int]([1, 2, 3, 4, 5])
            >>> d[1:4]
            SliceableDeque([2, 3, 4])

            >>> d = SliceableDeque[str](['a', 'b', 'c'])
            >>> d[-2:]
            SliceableDeque(['b', 'c'])
        c              3   (   K   | ]	  }|     y wrU   rV   ).0ir   s     r   	<genexpr>z-SliceableDeque.__getitem__.<locals>.<genexpr>3  s     !La$q'!Ls   )r[   r\   rS   lenr%   ranger"   r9   )r   rH   startstopstepr%   s   `    r   r9   zSliceableDeque.__getitem__  sV    . eU# %c$i 8E4>>!L5d3K!LLL7&u--r   otherc                     t        |t              rt        |       |k(  S t        |t              rt        |       |k(  S t        |t              rt        |       |k(  S t        |   |      S )a  
        Checks equality with another object, allowing for comparison with
        lists, tuples, and sets.

        Args:
            other (types.Any): The object to compare with.

        Returns:
            bool: True if the objects are equal, False otherwise.
        )r[   r`   tuplerE   r"   __eq__)r   r{   r%   s     r   r~   zSliceableDeque.__eq__7  s_     eT":&&u%;%''s#t9%%7>%((r   c                     |dk(  rt         |          S |dt        |       dz
  hv rt         |          S t	        d      )a  
        Removes and returns the item at the given index. Only supports index 0
        and the last index.

        Args:
            index (int, optional): The index of the item to remove. Defaults to
            -1.

        Returns:
            T: The removed item.

        Raises:
            IndexError: If the index is not 0 or the last index.

        Examples:
            >>> d = SliceableDeque([1, 2, 3])
            >>> d.pop(0)
            1
            >>> d.pop()
            3
        r   r   z=Only index 0 and the last index (`N-1` or `-1`) are supported)r"   popleftrv   pop
IndexError)r   rH   r%   s     r   r   zSliceableDeque.popK  sL    , A:7?$$r3t9q=))7;=   r   )r   )r&   r'   r(   r)   r4   rj   r   rh   r	   r9   r\   rk   r.   ri   r~   intr   r/   r0   s   @r   rm   rm     s     __?!4!4?? ?__CC+>C C.[[!4!4e!;<.	Q++	,.:)EII )$ )( a  r   rm   __main__)'r)   abccollectionsr4    r   TYPE_CHECKING	_typeshedTypeVarr   r   DictDTOptionalCallabler*   r,   Hashabler   r	   rk   Mappingr^   r?   r-   Literalrg   ABCr   r2   r6   ra   rA   Genericdequerm   r&   doctesttestmodrV   r   r   <module>r      s  6r    	 U]]4U]]4
ZZB
..R0
1
..R0
1U]]4u~~.EMM# 	MM"b&	NN5;;r2v&'	NN5==R().0 mm-.O/UZZB' O/d((B' ((Vo.^BF+ o.ds#B s#peU]]1%{'8'8'; eP zGOO r   