o
    Ee                     @   sJ   d dl Z d dlZd dlZd dlZG dd dee jjZG dd deZ	dS )    Nc                   @   s6   e Zd ZdZdd Zdd ZejZdd Z	dd	 Z
d
S )	DictStacka  
    A stack of dictionaries that behaves as a view on those dictionaries,
    giving preference to the last.

    >>> stack = DictStack([dict(a=1, c=2), dict(b=2, a=2)])
    >>> stack['a']
    2
    >>> stack['b']
    2
    >>> stack['c']
    2
    >>> len(stack)
    3
    >>> stack.push(dict(a=3))
    >>> stack['a']
    3
    >>> set(stack.keys()) == set(['a', 'b', 'c'])
    True
    >>> set(stack.items()) == set([('a', 3), ('b', 2), ('c', 2)])
    True
    >>> dict(**stack) == dict(stack) == dict(a=3, c=2, b=2)
    True
    >>> d = stack.pop()
    >>> stack['a']
    2
    >>> d = stack.pop()
    >>> stack['a']
    1
    >>> stack.get('b', None)
    >>> 'c' in stack
    True
    c                 C   s(   t | }tttjdd |D S )Nc                 s   s    | ]}|  V  qd S N)keys).0c r   c/var/www/bmteknikk.ddns.net/venv/lib/python3.10/site-packages/setuptools/_distutils/_collections.py	<genexpr>,   s    z%DictStack.__iter__.<locals>.<genexpr>)list__iter__iterset	itertoolschainfrom_iterable)selfdictsr   r   r   r   *   s   
zDictStack.__iter__c                 C   s4   t tt| D ]}||v r||   S q	t|r   )reversedtupler
   r   KeyError)r   keyscoper   r   r   __getitem__.   s
   zDictStack.__getitem__c                 C   s   t jj| |S r   )collectionsabcMapping__contains__)r   otherr   r   r   r   6      zDictStack.__contains__c                 C   s   t tt| S r   )lenr
   r   )r   r   r   r   __len__9   r   zDictStack.__len__N)__name__
__module____qualname____doc__r   r   r
   appendpushr   r    r   r   r   r   r      s    !r   c                   @   s|   e Zd ZdZi ejfddZedd Zdd Z	dd	d
Z
dd Zdd Zeddi  ZG dd deZedZedZdS )RangeMapaa  
    A dictionary-like object that uses the keys as bounds for a range.
    Inclusion of the value for that range is determined by the
    key_match_comparator, which defaults to less-than-or-equal.
    A value is returned for a key if it is the first key that matches in
    the sorted list of keys.

    One may supply keyword parameters to be passed to the sort function used
    to sort keys (i.e. key, reverse) as sort_params.

    Let's create a map that maps 1-3 -> 'a', 4-6 -> 'b'

    >>> r = RangeMap({3: 'a', 6: 'b'})  # boy, that was easy
    >>> r[1], r[2], r[3], r[4], r[5], r[6]
    ('a', 'a', 'a', 'b', 'b', 'b')

    Even float values should work so long as the comparison operator
    supports it.

    >>> r[4.5]
    'b'

    But you'll notice that the way rangemap is defined, it must be open-ended
    on one side.

    >>> r[0]
    'a'
    >>> r[-1]
    'a'

    One can close the open-end of the RangeMap by using undefined_value

    >>> r = RangeMap({0: RangeMap.undefined_value, 3: 'a', 6: 'b'})
    >>> r[0]
    Traceback (most recent call last):
    ...
    KeyError: 0

    One can get the first or last elements in the range by using RangeMap.Item

    >>> last_item = RangeMap.Item(-1)
    >>> r[last_item]
    'b'

    .last_item is a shortcut for Item(-1)

    >>> r[RangeMap.last_item]
    'b'

    Sometimes it's useful to find the bounds for a RangeMap

    >>> r.bounds()
    (0, 6)

    RangeMap supports .get(key, default)

    >>> r.get(0, 'not found')
    'not found'

    >>> r.get(7, 'not found')
    'not found'

    One often wishes to define the ranges by their left-most values,
    which requires use of sort params and a key_match_comparator.

    >>> r = RangeMap({1: 'a', 4: 'b'},
    ...     sort_params=dict(reverse=True),
    ...     key_match_comparator=operator.ge)
    >>> r[1], r[2], r[3], r[4], r[5], r[6]
    ('a', 'a', 'a', 'b', 'b', 'b')

    That wasn't nearly as easy as before, so an alternate constructor
    is provided:

    >>> r = RangeMap.left({1: 'a', 4: 'b', 7: RangeMap.undefined_value})
    >>> r[1], r[2], r[3], r[4], r[5], r[6]
    ('a', 'a', 'a', 'b', 'b', 'b')

    c                 C   s   t | | || _|| _d S r   )dict__init__sort_paramsmatch)r   sourcer*   key_match_comparatorr   r   r   r)      s   
zRangeMap.__init__c                 C   s   | |t ddtjdS )NT)reverse)r*   r-   )r(   operatorge)clsr,   r   r   r   left   s   zRangeMap.leftc                 C   sb   t |  fi | j}t|tjr| || }|S | ||}t| |}|tj	u r/t
||S r   )sortedr   r*   
isinstancer'   Itemr   _find_first_match_r(   undefined_valuer   )r   itemsorted_keysresultr   r   r   r   r      s   
zRangeMap.__getitem__Nc                 C   s"   z| | W S  t y   | Y S w )z
        Return the value for key if key is in the dictionary, else default.
        If default is not given, it defaults to None, so that this method
        never raises a KeyError.
        )r   )r   r   defaultr   r   r   get   s
   
zRangeMap.getc                 C   s0   t | j|}tt||}|r|d S t|)Nr   )	functoolspartialr+   r
   filterr   )r   r   r8   is_matchmatchesr   r   r   r6      s
   zRangeMap._find_first_match_c                 C   s*   t |  fi | j}|tj |tj fS r   )r3   r   r*   r'   
first_item	last_item)r   r9   r   r   r   bounds   s   zRangeMap.boundsRangeValueUndefinedr   c                   @   s   e Zd ZdZdS )zRangeMap.ItemzRangeMap ItemN)r!   r"   r#   r$   r   r   r   r   r5      s    r5   r   r   )r!   r"   r#   r$   r/   ler)   classmethodr2   r   r<   r6   rD   typer7   intr5   rB   rC   r   r   r   r   r'   >   s    P

r'   )
r   r=   r   r/   r
   r   r   r   r(   r'   r   r   r   r   <module>   s    6