U
    q±ËhP  ã                   @   s,   G d d„ de ƒZedkr(ddlZe ¡  dS )c                       sD   e Zd ZdZddd„Z‡ fdd„ZeZddd„Z‡ fd	d
„Z‡  Z	S )ÚEasyDictaC	  
    Get attributes

    >>> d = EasyDict({'foo':3})
    >>> d['foo']
    3
    >>> d.foo
    3
    >>> d.bar
    Traceback (most recent call last):
    ...
    AttributeError: 'EasyDict' object has no attribute 'bar'

    Works recursively

    >>> d = EasyDict({'foo':3, 'bar':{'x':1, 'y':2}})
    >>> isinstance(d.bar, dict)
    True
    >>> d.bar.x
    1

    Bullet-proof

    >>> EasyDict({})
    {}
    >>> EasyDict(d={})
    {}
    >>> EasyDict(None)
    {}
    >>> d = {'a': 1}
    >>> EasyDict(**d)
    {'a': 1}
    >>> EasyDict((('a', 1), ('b', 2)))
    {'a': 1, 'b': 2}
    
    Set attributes

    >>> d = EasyDict()
    >>> d.foo = 3
    >>> d.foo
    3
    >>> d.bar = {'prop': 'value'}
    >>> d.bar.prop
    'value'
    >>> d
    {'foo': 3, 'bar': {'prop': 'value'}}
    >>> d.bar.prop = 'newer'
    >>> d.bar.prop
    'newer'
    >>> d.lst = [1, 2, 3]
    >>> d.lst
    [1, 2, 3]
    >>> d.tpl = (1, 2, 3)
    >>> d.tpl
    (1, 2, 3)


    Values extraction

    >>> d = EasyDict({'foo':0, 'bar':[{'x':1, 'y':2}, {'x':3, 'y':4}]})
    >>> isinstance(d.bar, list)
    True
    >>> from operator import attrgetter
    >>> list(map(attrgetter('x'), d.bar))
    [1, 3]
    >>> list(map(attrgetter('y'), d.bar))
    [2, 4]
    >>> d = EasyDict()
    >>> list(d.keys())
    []
    >>> d = EasyDict(foo=3, bar=dict(x=1, y=2))
    >>> d.foo
    3
    >>> d.bar.x
    1

    Still like a dict though

    >>> o = EasyDict({'clean':True})
    >>> list(o.items())
    [('clean', True)]

    And like a class

    >>> class Flower(EasyDict):
    ...     power = 1
    ...     mean = {}
    ...     color = {"r": 100, "g": 0, "b": 0}
    ...
    >>> f = Flower()
    >>> f.power
    1
    >>> f.color.r
    100
    >>> f.mean.x = 10
    >>> f.mean.x
    10
    >>> f = Flower({'height': 12})
    >>> f.height
    12
    >>> f['power']
    1
    >>> sorted(f.keys())
    ['color', 'height', 'mean', 'power']

    update and pop items
    >>> d = EasyDict(a=1, b='2')
    >>> e = EasyDict(c=3.0, a=9.0)
    >>> d.update(e)
    >>> d.c
    3.0
    >>> d['c']
    3.0
    >>> d.get('c')
    3.0
    >>> d.update(a=4, b=4)
    >>> d.b
    4
    >>> d.pop('a')
    4
    >>> d.a
    Traceback (most recent call last):
    ...
    AttributeError: 'EasyDict' object has no attribute 'a'
    >>> d.pop('a', 8)
    8
    >>> d.pop('b', 100)
    4
    >>> d
    {'c': 3.0}
    Nc                 K   sˆ   |d kri }nt |ƒ}|r&|jf |Ž | ¡ D ]\}}t| ||ƒ q.| jj ¡ D ]2}| d¡rh| d¡sP|dkrPt| |t	| |ƒƒ qPd S )NÚ__)ÚupdateÚpop)
Údictr   ÚitemsÚsetattrÚ	__class__Ú__dict__ÚkeysÚ
startswithÚendswithÚgetattr)ÚselfÚdÚkwargsÚkÚv© r   ú5/tmp/pip-unpacked-wheel-f5vlxql4/easydict/__init__.pyÚ__init__…   s    zEasyDict.__init__c                    sn   t |ttfƒr*t|ƒ‡ fdd„|D ƒƒ}nt |tƒrFt |tƒsFt|ƒ}ttˆ ƒ ||¡ ttˆ ƒ ||¡ d S )Nc                 3   s&   | ]}t |tƒrˆ  |¡n|V  qd S ©N)Ú
isinstancer   r   )Ú.0Úx©r   r   r   Ú	<genexpr>•   s    ÿz'EasyDict.__setattr__.<locals>.<genexpr>)	r   ÚlistÚtupleÚtyper   r   ÚsuperÚ__setattr__Ú__setitem__)r   ÚnameÚvalue©r   r   r   r    “   s    ÿ
zEasyDict.__setattr__c                 K   s2   |pt ƒ }| |¡ |D ]}t| ||| ƒ qd S r   )r   r   r   )r   ÚeÚfr   r   r   r   r   r   ž   s    

zEasyDict.updatec                    s*   t | |ƒrt| |ƒ tt| ƒj|f|žŽ S r   )ÚhasattrÚdelattrr   r   r   )r   r   Úargsr$   r   r   r   ¤   s    

zEasyDict.pop)N)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r    r!   r   r   Ú__classcell__r   r   r$   r   r      s    
	
r   Ú__main__é    N)r   r   r*   ÚdoctestÚtestmodr   r   r   r   Ú<module>   s    *