o
    ¨Ýjç  ã                   @   sH   d dl mZmZ d dlmZ d
dd„Zeeffdd„ZG dd	„ d	ƒZ	dS )é    )ÚdefaultdictÚdeque)ÚfilterfalseNc                 c   sf   � t ƒ }|j}|du rt|j| ƒD ]	}||ƒ |V  qdS | D ]}||ƒ}||vr0||ƒ |V  qdS )zHList unique elements, preserving order. Remember all elements ever seen.N)ÚsetÚaddr   Ú__contains__)ÚiterableÚkeyÚseenÚseen_addÚelementÚk© r   úX/var/www/html/chatgpt/venv/lib/python3.10/site-packages/importlib_metadata/_itertools.pyÚunique_everseen   s   €þ€ür   c                 C   sT   | du rt dƒS |durt| |ƒrt | fƒS zt | ƒW S  ty)   t | fƒ Y S w )ax  If *obj* is iterable, return an iterator over its items::

        >>> obj = (1, 2, 3)
        >>> list(always_iterable(obj))
        [1, 2, 3]

    If *obj* is not iterable, return a one-item iterable containing *obj*::

        >>> obj = 1
        >>> list(always_iterable(obj))
        [1]

    If *obj* is ``None``, return an empty iterable:

        >>> obj = None
        >>> list(always_iterable(None))
        []

    By default, binary and text strings are not considered iterable::

        >>> obj = 'foo'
        >>> list(always_iterable(obj))
        ['foo']

    If *base_type* is set, objects for which ``isinstance(obj, base_type)``
    returns ``True`` won't be considered iterable.

        >>> obj = {'a': 1}
        >>> list(always_iterable(obj))  # Iterate over the dict's keys
        ['a']
        >>> list(always_iterable(obj, base_type=dict))  # Treat dicts as a unit
        [{'a': 1}]

    Set *base_type* to ``None`` to avoid any special handling and treat objects
    Python considers iterable as iterable:

        >>> obj = 'foo'
        >>> list(always_iterable(obj, base_type=None))
        ['f', 'o', 'o']
    Nr   )ÚiterÚ
isinstanceÚ	TypeError)ÚobjZ	base_typer   r   r   Úalways_iterable   s   )

ÿr   c                   @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )Úbucketaâ  Wrap *iterable* and return an object that buckets the iterable into
    child iterables based on a *key* function.

        >>> iterable = ['a1', 'b1', 'c1', 'a2', 'b2', 'c2', 'b3']
        >>> s = bucket(iterable, key=lambda x: x[0])  # Bucket by 1st character
        >>> sorted(list(s))  # Get the keys
        ['a', 'b', 'c']
        >>> a_iterable = s['a']
        >>> next(a_iterable)
        'a1'
        >>> next(a_iterable)
        'a2'
        >>> list(s['b'])
        ['b1', 'b2', 'b3']

    The original iterable will be advanced and its items will be cached until
    they are used by the child iterables. This may require significant storage.

    By default, attempting to select a bucket to which no items belong  will
    exhaust the iterable and cache all values.
    If you specify a *validator* function, selected buckets will instead be
    checked against it.

        >>> from itertools import count
        >>> it = count(1, 2)  # Infinite sequence of odd numbers
        >>> key = lambda x: x % 10  # Bucket by last digit
        >>> validator = lambda x: x in {1, 3, 5, 7, 9}  # Odd digits only
        >>> s = bucket(it, key=key, validator=validator)
        >>> 2 in s
        False
        >>> list(s[2])
        []

    Nc                 C   s,   t |ƒ| _|| _ttƒ| _|pdd„ | _d S )Nc                 S   s   dS )NTr   )Úxr   r   r   Ú<lambda>v   s    z!bucket.__init__.<locals>.<lambda>)r   Ú_itÚ_keyr   r   Ú_cacheÚ
_validator)Úselfr   r	   Ú	validatorr   r   r   Ú__init__r   s   

zbucket.__init__c                 C   sH   |   |¡sdS zt| | ƒ}W n
 ty   Y dS w | j|  |¡ dS )NFT)r   ÚnextÚStopIterationr   Ú
appendleft)r   ÚvalueÚitemr   r   r   r   x   s   
ÿzbucket.__contains__c                 c   s~   � 	 | j | r| j |  ¡ V  n.	 zt| jƒ}W n
 ty"   Y dS w |  |¡}||kr0|V  n|  |¡r=| j |  |¡ qq)z²
        Helper to yield items from the parent iterator that match *value*.
        Items that don't match are stored in the local cache as they
        are encountered.
        TN)r   Úpopleftr    r   r!   r   r   Úappend)r   r#   r$   Ú
item_valuer   r   r   Ú_get_values…   s$   €
ÿ

öøzbucket._get_valuesc                 c   sF   � | j D ]}|  |¡}|  |¡r| j|  |¡ q| j ¡ E d H  d S ©N)r   r   r   r   r&   Úkeys)r   r$   r'   r   r   r   Ú__iter__Ÿ   s   €


€zbucket.__iter__c                 C   s   |   |¡s	tdƒS |  |¡S )Nr   )r   r   r(   )r   r#   r   r   r   Ú__getitem__§   s   

zbucket.__getitem__r)   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r+   r,   r   r   r   r   r   N   s    
#r   r)   )
Úcollectionsr   r   Ú	itertoolsr   r   ÚstrÚbytesr   r   r   r   r   r   Ú<module>   s
    
6