File: //opt/saltstack/salt/lib/python3.10/site-packages/more_itertools/__pycache__/more.cpython-310.pyc
o
;jn� � @ s� d dl Z d dlZd dlmZmZmZ d dlmZ d dlm Z d dl
mZmZm
Z
d dlmZmZ d dlmZmZmZmZmZmZmZmZmZmZmZmZmZmZm Z d dl m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z)m*Z* d d l m+Z+m,Z, d d
l-m.Z.m/Z/ d dl0m0Z0m1Z1m2Z2m3Z3 d dl4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z:m;Z;m<Z< d d
l=m>Z> d dl?m@Z@ d dlAmBZB ddlCmDZDmEZEmFZFmGZGmHZHmIZImJZJmKZKmLZLmMZMmNZNmOZO g d�ZPzd dl mQZR W n eSy� deTfdd�ZUdd� ZVdd� ZRY nw �d(dd�ZWeDfdd�ZXeDfd d!�ZYeDfd"d#�ZZG d$d%� d%�Z[d&d'� Z\d(d)� Z]d*d+� Z^d,d-� Z_G d.d/� d/�Z`�d)d0d1�Zad2d3� Zb�d)d4d5�Zc�d*d6d7�Zd�d*d8d9�Ze�d+d:d;�Zfd<d=� Zg�d,d>d?�Zhd@dA� Zi�d(dBdC�ZjG dDdE� dE�Zk�d+dFdG�ZldHdI� ZmdJdK� Zn�d*dLdM�ZodNdO� Zp�d)dPdQ�Zq�d-dRdS�Zr�d(dTdU�Zs�d.dWdX�Zt�d/dYdZ�Zu�d/d[d\�Zv�d/d]d^�Zwd_d`� Zx�d0dadb�Zy�d1ddde�Zz�d*dfdg�Z{dhdi� Z|�d2dkdl�Z}dddm�dndo�Z~ �d3dqdr�Zdsdt� Z�dudv� Z�e�e�ffdwdx�Z��d+dydz�Z��d-d{d|�Z�G d}d~� d~e�Z��d*dd��Z�d�d�� Z�e�dfd�d��Z�d�d�� Z�d�d�� Z�d�d�� Z�d�d�� Z�G d�d�� d��Z�d�d�� Z�d�d�� Z��d*d�d��Z�e;fdd��d�d��Z�G d�d�� d�e�Z�G d�d�� d��Z�G d�d�� d��Z�e�fd�d��Z��d+d�d��Z�dpd�d��Z��d)d�d��Z�e�dfd�d��Z��d,d�d��Z�d�d�� Z��d-d�d��Z�G d�d�� d��Z��d)d�d��Z�d�d�� Z�d�d�� Z�d�d�� Z�d�d�� Z�d�d�� Z��d*d�d��Z�d�d�� Z�d�d�� Z�d�dÄ Z��d*dddĜd�dƄZ��d4d�dȄZ�G d�dʄ d�e��Z�G d�d̄ d̃Z�d�d΄ Z��d*d�dЄZ�ddќd�dӄZ�d�dՄ Z�d�dׄ Z�d�dل Z�ddќd�dۄZ�d�d݄ Z�d�d߄ Z�d�d� Z�G d�d� d�Z�d�d� Z�e�e�fdd�d�d�Z��d*d�d�Z��d*d�d�Z��d*d�d�Z��d*d�d�Z�deDd�d�d�Z�de�d�fd�d��Z�ddќd�d��Z�ddќd�d��Z�d�d�� Z�d�d�� Z�d��d � ZȐd�d� Z�eʐd��d�d�Zːd�d� Z̐d�d � Z͐d
�d� Zΐd�d
� Zϐd�d� ZАd�d� Z�d�d��d�d�Z�d�d��d�d�Z�d�d��d�d�ZԐd�d� ZՐd�d� Z�dd��d�d�Z�G �d �d!� �d!�Zؐd"�d#� Zِd1�d$�d%�Z�G �d&�d'� �d'�Z�dS (5 � N)�Counter�defaultdict�deque)�Sequence)�suppress)�cached_property�partial�wraps)�heapify�heapreplace)�chain�combinations�compress�count�cycle� dropwhile�groupby�islice�permutations�repeat�starmap� takewhile�tee�zip_longest�product)
�comb�e�exp� factorial�floor�fsum�log�log1p�perm�tau)�ceil�prod)�Empty�Queue)�random� randrange�shuffle�uniform)�
attrgetter�getitem�is_not�
itemgetter�lt�neg�sub�gt)�maxsize)� monotonic)�Lock� )�_marker�consume�
first_true�flatten�is_prime�nth�powerset�sieve�take�unique_everseen� all_equal�batched)v�AbortThread�SequenceView�adjacent�
all_unique�always_iterable�always_reversible�argmax�argmin�bucket�
callback_iter�chunked�chunked_even�circular_shifts�collapse�combination_index�"combination_with_replacement_index�concurrent_tee�consecutive_groups�constrained_batches�consumer�count_cycle� countable�derangements�dft�
difference�distinct_combinations�distinct_permutations�
distribute�divide�
doublestarmap�duplicates_everseen�duplicates_justseen�classify_unique� exactly_n�extract�
filter_except�
filter_map�first�gray_product�groupby_transform�ichunked�iequals�idft�ilen�
interleave�interleave_evenly�interleave_longest�interleave_randomly�intersperse� is_sorted�islice_extended�iterate�
iter_suppress�
join_mappings�last�locate�longest_common_prefix�lstrip�make_decorator�
map_except�map_if�
map_reduce� mark_ends�minmax�nth_or_last�nth_permutation� nth_prime�nth_product� nth_combination_with_replacement�
numeric_range�one�only�
outer_product�padded�partial_product�
partitions�peekable�permutation_index�powerset_of_sets�
product_index�raise_�repeat_each�repeat_last�replace�rlocate�rstrip�
run_length�sample�seekable� serialize�set_partitions�side_effect�sized_iterator�sliced�
sort_together�split_after�split_at�split_before�
split_into�
split_when�spy�stagger�strip�
strictly_n�
substrings�substrings_indexes�synchronized�takewhile_inclusive�time_limited�unique_in_window�unique_to_each�unzip�value_chain�windowed�windowed_complete� with_iter�
zip_broadcast�
zip_offset)�sumprod�xc C s$ | d }||| }| | }||fS )z1Split a float into two half-precision components.g �A� )r� �t�hi�lor� r� �G/opt/saltstack/salt/lib/python3.10/site-packages/more_itertools/more.py�dl_split� s r� c
C sT t | �\}}t |�\}}|| }|| || }|| }|| | || } || fS )zLossless multiplication.)r� )
r� �yZxx_hiZxx_loZyy_hiZyy_lo�p�q�z�zzr� r� r� �dl_mul� s r� c C s t t�tt| |���S �N)r r �
from_iterable�mapr� )r� r� r� r� r� � _fsumprod� � r� Fc sB t tt�t | ��g �� |r�du rtd��� �fdd�}|� S � S )aJ Break *iterable* into lists of length *n*:
>>> list(chunked([1, 2, 3, 4, 5, 6], 3))
[[1, 2, 3], [4, 5, 6]]
By the default, the last yielded list will have fewer than *n* elements
if the length of *iterable* is not divisible by *n*:
>>> list(chunked([1, 2, 3, 4, 5, 6, 7, 8], 3))
[[1, 2, 3], [4, 5, 6], [7, 8]]
To use a fill-in value instead, see the :func:`grouper` recipe.
If the length of *iterable* is not divisible by *n* and *strict* is
``True``, then ``ValueError`` will be raised before the last
list is yielded.
Nz*n must not be None when using strict mode.c 3 �* � � D ]} t | ��krtd��| V qd S )Nziterable is not divisible by n.��len�
ValueError)�chunk��iterator�nr� r� �ret� � ��zchunked.<locals>.ret)�iterr rA r� )�iterabler� �strictr� r� r� r� rO � s rO c C s$ | D ]}| S |t u rtd��|S )a� Return the first item of *iterable*, or *default* if *iterable* is
empty.
>>> first([0, 1, 2, 3])
0
>>> first([], 'some default')
'some default'
If *default* is not provided and there are no items in the iterable,
raise ``ValueError``.
:func:`first` is useful when you have a generator of expensive-to-retrieve
values and want any arbitrary one. It is marginally shorter than
``next(iter(iterable), default)``.
zKfirst() was called on an empty iterable, and no default value was provided.)r9 r� )r� �default�itemr� r� r� rj � s �rj c
C sn z t | t�r| d W S t| dd�rtt| ��W S t| dd�d W S tttfy6 |t u r2t
d��| Y S w )a Return the last item of *iterable*, or *default* if *iterable* is
empty.
>>> last([0, 1, 2, 3])
3
>>> last([], 'some default')
'some default'
If *default* is not provided and there are no items in the iterable,
raise ``ValueError``.
����__reversed__Nr8 ��maxlenzJlast() was called on an empty iterable, and no default value was provided.)�
isinstancer �getattr�next�reversedr �
IndexError� TypeError�
StopIterationr9 r� )r� r� r� r� r� r{ s
��r{ c C s t t| |d �|d�S )ag Return the nth or the last item of *iterable*,
or *default* if *iterable* is empty.
>>> nth_or_last([0, 1, 2, 3], 2)
2
>>> nth_or_last([0, 1], 2)
1
>>> nth_or_last([], 0, 'some default')
'some default'
If *default* is not provided and there are no items in the iterable,
raise ``ValueError``.
r8 �r� )r{ r )r� r� r� r� r� r� r� - s r� c @ s^ e Zd ZdZdd� Zdd� Zdd� Zefdd �Zd
d� Z e
ej�Z
dd
� Zdd� Zdd� ZdS )r� a Wrap an iterator to allow lookahead and prepending elements.
Call :meth:`peek` on the result to get the value that will be returned
by :func:`next`. This won't advance the iterator:
>>> p = peekable(['a', 'b'])
>>> p.peek()
'a'
>>> next(p)
'a'
Pass :meth:`peek` a default value to return that instead of raising
``StopIteration`` when the iterator is exhausted.
>>> p = peekable([])
>>> p.peek('hi')
'hi'
peekables also offer a :meth:`prepend` method, which "inserts" items
at the head of the iterable:
>>> p = peekable([1, 2, 3])
>>> p.prepend(10, 11, 12)
>>> next(p)
10
>>> p.peek()
11
>>> list(p)
[11, 12, 1, 2, 3]
peekables can be indexed. Index 0 is the item that will be returned by
:func:`next`, index 1 is the item after that, and so on:
The values up to the given index will be cached.
>>> p = peekable(['a', 'b', 'c', 'd'])
>>> p[0]
'a'
>>> p[1]
'b'
>>> next(p)
'a'
Negative indexes are supported, but be aware that they will cache the
remaining items in the source iterator, which may require significant
storage.
To check whether a peekable is exhausted, check its truth value:
>>> p = peekable(['a', 'b'])
>>> if p: # peekable has items
... list(p)
['a', 'b']
>>> if not p: # peekable is exhausted
... list(p)
[]
c C � t |�| _t� | _d S r� )r� �_itr �_cache��selfr� r� r� r� �__init__y �
zpeekable.__init__c C � | S r� r� �r� r� r� r� �__iter__} � zpeekable.__iter__c C �$ z| � � W dS ty Y dS w �NFT��peekr� r� r� r� r� �__bool__� �
��zpeekable.__bool__c C sH | j sz| j �t| j�� W n ty |tu r� | Y S w | j d S )z�Return the item that will be next returned from ``next()``.
Return ``default`` if there are no items left. If ``default`` is not
provided, raise ``StopIteration``.
r )r� �appendr� r� r� r9 )r� r� r� r� r� r� � s �
z
peekable.peekc G s | j �t|�� dS )a Stack up items to be the next ones returned from ``next()`` or
``self.peek()``. The items will be returned in
first in, first out order::
>>> p = peekable([1, 2, 3])
>>> p.prepend(10, 11, 12)
>>> next(p)
10
>>> list(p)
[11, 12, 1, 2, 3]
It is possible, by prepending items, to "resurrect" a peekable that
previously raised ``StopIteration``.
>>> p = peekable([])
>>> next(p)
Traceback (most recent call last):
...
StopIteration
>>> p.prepend(1)
>>> next(p)
1
>>> next(p)
Traceback (most recent call last):
...
StopIteration
N)r� �
extendleftr� )r� �itemsr� r� r� �prepend� s zpeekable.prependc C s | j r| j �� S t| j�S r� )r� �popleftr� r� r� r� r� r� �__next__� s
zpeekable.__next__c C s� |j d u rdn|j }|dkr#|jd u rdn|j}|jd u rtn|j}n |dk r?|jd u r.dn|j}|jd u r;t d n|j}ntd��|dk sK|dk rS| j�| j� ntt ||�d t�}t
| j�}||krr| j�t| j|| �� t| j�| S )Nr8 r r� zslice step cannot be zero)
�step�start�stopr5 r� r� �extendr� �min�maxr� r �list)r� �indexr r r r� � cache_lenr� r� r� �
_get_slice� s
zpeekable._get_slicec C sd t |t�r
| �|�S t| j�}|dk r| j�| j� n||kr-| j�t| j|d | �� | j| S �Nr r8 )r� �slicer r� r� r r� r )r� r r r� r� r� �__getitem__� s
zpeekable.__getitem__N)�__name__�
__module__�__qualname__�__doc__r� r� r� r9 r� r� �classmethod�types�GenericAlias�__class_getitem__r� r r r� r� r� r� r� > s :
r� c � t � �� fdd��}|S )ab Decorator that automatically advances a PEP-342-style "reverse iterator"
to its first yield point so you don't have to call ``next()`` on it
manually.
>>> @consumer
... def tally():
... i = 0
... while True:
... print('Thing number %s is %s.' % (i, (yield)))
... i += 1
...
>>> t = tally()
>>> t.send('red')
Thing number 0 is red.
>>> t.send('fish')
Thing number 1 is fish.
Without the decorator, you would have to call ``next(t)`` before
``t.send()`` could be used.
c s � | i |��}t |� |S r� �r� )�args�kwargs�gen��funcr� r� �wrapper� s zconsumer.<locals>.wrapper�r )r r r� r r� rX � s rX c C s t ttd�t| ���S )a} Return the number of items in *iterable*.
For example, there are 168 prime numbers below 1,000:
>>> ilen(sieve(1000))
168
Equivalent to, but faster than::
def ilen(iterable):
count = 0
for _ in iterable:
count += 1
return count
This fully consumes the iterable, so handle with care.
r8 )�sumr r �zip�r� r� r� r� rp s rp c c s2 � t t��
|V | |�}q1 sw Y dS )a� Return ``start``, ``func(start)``, ``func(func(start))``, ...
Produces an infinite iterator. To add a stopping condition,
use :func:`take`, ``takewhile``, or :func:`takewhile_inclusive`:.
>>> take(10, iterate(lambda x: 2*x, 1))
[1, 2, 4, 8, 16, 32, 64, 128, 256, 512]
>>> collatz = lambda x: 3*x + 1 if x%2==1 else x // 2
>>> list(takewhile_inclusive(lambda x: x!=1, iterate(collatz, 10)))
[10, 5, 16, 8, 4, 2, 1]
TN)r r� )r r r� r� r� rx s �
��rx c c s6 � | �}|E dH W d � dS 1 sw Y dS )a� Wrap an iterable in a ``with`` statement, so it closes once exhausted.
For example, this will close the file when the iterator is exhausted::
upper_lines = (line.upper() for line in with_iter(open('foo')))
Note that you have to actually exhaust the iterator for opened files to be closed.
Any context manager which returns an iterable is a candidate for
``with_iter``.
Nr� )Zcontext_managerr� r� r� r� r� 2 s �
"�r� c @ �0 e Zd ZdZdd� Zdd� Zdd� Zdd � Zd
S )r� a� Wrapper for *iterable* that implements ``__len__``.
>>> it = map(str, range(5))
>>> sized_it = sized_iterator(it, 5)
>>> len(sized_it)
5
>>> list(sized_it)
['0', '1', '2', '3', '4']
This is useful for tools that use :func:`len`, like
`tqdm <https://pypi.org/project/tqdm/>`__ .
The wrapper doesn't validate the provided *length*, so be sure to choose
a value that reflects reality.
c C s t |�| _|| _d S r� )r� � _iterator�_length)r� r� �lengthr� r� r� r� T �
zsized_iterator.__init__c C �
t | j�S r� �r� r" r� r� r� r� r� X �
zsized_iterator.__next__c C r� r� r� r� r� r� r� r� [ r� zsized_iterator.__iter__c C � | j S r� )r# r� r� r� r� �__len__^ � zsized_iterator.__len__N)r
r r r r� r� r� r* r� r� r� r� r� C s r� c C sJ t | �}|D ]}|D ]}d|�d|�d�}|pt|��| S |p$td��)a� Return the first item from *iterable*, which is expected to contain only
that item. Raise an exception if *iterable* is empty or has more than one
item.
:func:`one` is useful for ensuring that an iterable contains only one item.
For example, it can be used to retrieve the result of a database query
that is expected to return a single row.
If *iterable* is empty, ``ValueError`` will be raised. You may specify a
different exception with the *too_short* keyword:
>>> it = []
>>> one(it) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: too few items in iterable (expected 1)'
>>> too_short = IndexError('too few items')
>>> one(it, too_short=too_short) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
IndexError: too few items
Similarly, if *iterable* contains more than one item, ``ValueError`` will
be raised. You may specify a different exception with the *too_long*
keyword:
>>> it = ['too', 'many']
>>> one(it) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: Expected exactly one item in iterable, but got 'too',
'many', and perhaps more.
>>> too_long = RuntimeError
>>> one(it, too_long=too_long) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
RuntimeError
Note that :func:`one` attempts to advance *iterable* twice to ensure there
is only one item. See :func:`spy` or :func:`peekable` to check iterable
contents less destructively.
�/Expected exactly one item in iterable, but got �, �, and perhaps more.z&too few items in iterable (expected 1)�r� r� )r� � too_short�too_longr� rj �second�msgr� r� r� r� b s ,��r� c G s | |� �r� r� )� exceptionr r� r� r� r� � s r� c c s~ � |du r dd� }|du rdd� }t | �}d}t||�D ] }|V |d7 }q||k r0||� dS |D ]
}||d � dS dS )a� Validate that *iterable* has exactly *n* items and return them if
it does. If it has fewer than *n* items, call function *too_short*
with the actual number of items. If it has more than *n* items, call function
*too_long* with the number ``n + 1``.
>>> iterable = ['a', 'b', 'c', 'd']
>>> n = 4
>>> list(strictly_n(iterable, n))
['a', 'b', 'c', 'd']
Note that the returned iterable must be consumed in order for the check to
be made.
By default, *too_short* and *too_long* are functions that raise
``ValueError``.
>>> list(strictly_n('ab', 3)) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: too few items in iterable (got 2)
>>> list(strictly_n('abc', 2)) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
ValueError: too many items in iterable (got at least 3)
You can instead supply functions that do something else.
*too_short* will be called with the number of items in *iterable*.
*too_long* will be called with `n + 1`.
>>> def too_short(item_count):
... raise RuntimeError
>>> it = strictly_n('abcd', 6, too_short=too_short)
>>> list(it) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
...
RuntimeError
>>> def too_long(item_count):
... print('The boss is going to hear about this')
>>> it = strictly_n('abcdef', 4, too_long=too_long)
>>> list(it)
The boss is going to hear about this
['a', 'b', 'c', 'd']
Nc S � t td| � d��S )NzToo few items in iterable (got �)�r� r� ��
item_countr� r� r� �<lambda>� �
�zstrictly_n.<locals>.<lambda>c S r5 )Nz)Too many items in iterable (got at least r6 r7 r8 r� r� r� r: � r; r r8 )r� r )r� r� r0 r1 �it�sentr� r� r� r� r� � s"