File manager - Edit - /opt/saltstack/salt/lib/python3.10/site-packages/importlib_metadata/__pycache__/_itertools.cpython-310.pyc
Back
o ;j� � @ sH d dl mZmZ d dlmZ d dd�Zeeffdd�ZG dd � d �Z dS )� )�defaultdict�deque)�filterfalseNc c sf � t � }|j}|du rt|j| �D ] }||� |V qdS | D ]}||�}||vr0||� |V qdS )zHList unique elements, preserving order. Remember all elements ever seen.N)�set�addr �__contains__)�iterable�key�seenZseen_add�element�k� r �Q/opt/saltstack/salt/lib/python3.10/site-packages/importlib_metadata/_itertools.py�unique_everseen s ����r c C sT | du rt d�S |durt| |�rt | f�S zt | �W S ty) 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 Zd ZdZd dd�Zdd� Zdd� Zd d � Zdd� ZdS )�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 |�| _|| _tt�| _|pdd� | _d S )Nc S s dS )NTr )�xr r r �<lambda>v s z!bucket.__init__.<locals>.<lambda>)r �_it�_keyr r �_cache� _validator)�selfr r Z validatorr r r �__init__r s zbucket.__init__c C sH | � |�sdS zt| | �}W n ty Y dS w | j| �|� dS )NFT)r �next� StopIterationr � appendleft)r �value�itemr r r r x s �zbucket.__contains__c c s~ � | j | r| j | �� V n. zt| j�}W n ty" Y dS w | �|�}||kr0|V n| �|�r=| j | �|� qq)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"