
    hP                     R     " S  S\ 5      r\S:X  a  SSKr\R                  " 5         gg)c                   P   ^  \ rS rSrSrSS jrU 4S jr\rSS jrU 4S jr	Sr
U =r$ )	EasyDict   as  
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}
c           	         Uc  0 nO[        U5      nU(       a  UR                  " S0 UD6  UR                  5        H  u  p4[        XU5        M     U R                  R
                  R                  5        HN  nUR                  S5      (       a  UR                  S5      (       a  M1  US;  d  M9  [        X[        X5      5        MP     g )N__)updatepop )
dictr   itemssetattr	__class____dict__keys
startswithendswithgetattr)selfdkwargskvs        K/var/www/fran/franai/venv/lib/python3.13/site-packages/easydict/__init__.py__init__EasyDict.__init__   s    9AQAHHvGGIDADQ  ((--/ALL&&1::d+;+;J[A[!12 0    c                 ,  >^  [        U[        [        45      (       a  [        U5      " U 4S jU 5       5      nO5[        U[        5      (       a   [        U[
        5      (       d  [        U5      n[        [
        T ]  X5        [        [
        T ]#  X5        g )Nc              3   t   >#    U  H-  n[        U[        5      (       a  TR                  U5      OUv   M/     g 7fN)
isinstancer
   r   ).0xr   s     r   	<genexpr>'EasyDict.__setattr__.<locals>.<genexpr>   s6       C<Aq
1d++ !%q 112!3<As   58)	r   listtupletyper
   r   super__setattr____setitem__)r   namevaluer   s   `  r   r(   EasyDict.__setattr__   sr    edE]++K  C<A C CEt$$Zx-H-HUOEh)$6h)$6r   c                 z    U=(       d
    [        5       nUR                  U5        U H  n[        XX4   5        M     g r   )r
   r   r   )r   efr   r   s        r   r   EasyDict.update   s.    K	ADQT" r   c                 d   > [        X5      (       a  [        X5        [        [        U ]  " U/UQ76 $ r   )hasattrdelattrr'   r   r   )r   r   argsr   s      r   r   EasyDict.pop   s-    4DXt(2T22r   r	   r   )__name__
__module____qualname____firstlineno____doc__r   r(   r)   r   r   __static_attributes____classcell__)r   s   @r   r   r      s*    BF37 K#3 3r   r   __main__    N)r
   r   r6   doctesttestmodr	   r   r   <module>rA      s0   f3t f3R zOO r   