U
    f                     @   s   d Z ddlZddlmZ ddlZddlmZ zddlZejZW n$ e	k
rb   G dd de
ZY nX dd Zd	d
 ZdddZdd ZdS )zAContains _sequence_like and helpers for sequence data structures.    N)abc)_treec                   @   s   e Zd ZdZdS )ObjectProxyz$Stub-class for `wrapt.ObjectProxy``.N)__name__
__module____qualname____doc__ r	   r	   4/tmp/pip-target-lpfmz8o1/lib/python/tree/sequence.pyr      s   r   c                 C   s,   z
t | W S  tk
r&   tdY nX dS )zHReturns a sorted list of the dict keys, with error if keys not sortable.z,tree only supports dicts with sortable keys.N)sorted	TypeError)
dictionaryr	   r	   r
   _sorted   s    
r   c                 C   s
   t | S N)r   Zis_attrs)instancer	   r	   r
   	_is_attrs&   s    r   Fc                 C   s   t | |S )a  Returns True iff `instance` is a `namedtuple`.

  Args:
    instance: An instance of a Python object.
    strict: If True, `instance` is considered to be a `namedtuple` only if
        it is a "plain" namedtuple. For instance, a class inheriting
        from a `namedtuple` will be considered to be a `namedtuple`
        iff `strict=False`.

  Returns:
    True if `instance` is a `namedtuple`.
  )r   is_namedtuple)r   strictr	   r	   r
   _is_namedtuple*   s    r   c              
      s`  t | ttjfrzttt| |  fdd| D }t | tjrPt| | j	|S t | t
jrlt| t|S t| |S nt | tjrt|S t| st| r0t | trt| j}nt| }z4t| r|f dd t|j|D W S || W S W n: tk
r, } ztd| d| |W 5 d}~X Y nX n,t | trPt| t| j|S t| |S dS )a!  Converts the sequence `args` to the same type as `instance`.

  Args:
    instance: an instance of `tuple`, `list`, `namedtuple`, `dict`, or
        `collections.OrderedDict`.
    args: elements to be converted to the `instance` type.

  Returns:
    `args` with the type of `instance`.
  c                 3   s   | ]}| | fV  qd S r   r	   ).0keyresultr	   r
   	<genexpr>L   s     z!_sequence_like.<locals>.<genexpr>c                 S   s   i | ]\}}|j |qS r	   )name)r   attrargr	   r	   r
   
<dictcomp>`   s    z"_sequence_like.<locals>.<dictcomp>zCouldn't traverse z with arguments N)
isinstancedictcollections_abcMappingzipr   collectionsdefaultdicttypedefault_factorytypesMappingProxyTypeMappingViewlistr   r   r   __wrapped__Z__attrs_attrs__	Exceptionr   _sequence_like)r   argsZkeys_and_valuesZinstance_typeer	   r   r
   r-   :   s>    

r-   )F)r   r#   r   r    r'   treer   Zwraptr   ImportErrorobjectr   r   r   r-   r	   r	   r	   r
   <module>   s   

