
    <hP.                     $    S r S/r " S S\5      rg)zA dict subclass that supports attribute style access.

Authors:

* Fernando Perez (original)
* Brian Granger (refactoring to a dict subclass)
Structc                   x    \ rS rSrSrSrS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rSS jrSS jrSrg)r      a9  A dict subclass with attribute style access.

This dict subclass has a a few extra features:

* Attribute style access.
* Protection of class members (like keys, items) when using attribute
  style access.
* The ability to restrict assignment to only existing keys.
* Intelligent merging.
* Overloaded operators.
Tc                 f    [         R                  U SS5        [        R                  " U /UQ70 UD6  g)a=  Initialize with a dictionary, another Struct, or data.

Parameters
----------
*args : dict, Struct
    Initialize with one dict or Struct
**kw : dict
    Initialize with key, value pairs.

Examples
--------
>>> s = Struct(a=10,b=30)
>>> s.a
10
>>> s.b
30
>>> s2 = Struct(s,c=30)
>>> sorted(s2.keys())
['a', 'b', 'c']
	_allownewTN)object__setattr__dict__init__)selfargskws      iC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\IPython/utils/ipstruct.pyr
   Struct.__init__)   s,    * 	4d3d(T(R(    c                 x    U R                   (       d  X;  a  [        SU-  5      e[        R                  XU5        g)a  Set an item with check for allownew.

Examples
--------
>>> s = Struct()
>>> s['a'] = 10
>>> s.allow_new_attr(False)
>>> s['a'] = 10
>>> s['a']
10
>>> try:
...     s['b'] = 20
... except KeyError:
...     print('this is not allowed')
...
this is not allowed
z8can't create new attribute %s when allow_new_attr(False)N)r   KeyErrorr	   __setitem__)r   keyvalues      r   r   Struct.__setitem__A   s6    $ ~~#/JSPR RE*r   c                     [        U[        5      (       a2  XR                  ;   d  [        [        U5      (       a  [        SU-  5      e U R                  X5        g! [         a  n[        U5      UeSnAff = f)aK  Set an attr with protection of class members.

This calls :meth:`self.__setitem__` but convert :exc:`KeyError` to
:exc:`AttributeError`.

Examples
--------
>>> s = Struct()
>>> s.a = 10
>>> s.a
10
>>> try:
...     s.get = 10
... except AttributeError:
...     print("you can't set a class member")
...
you can't set a class member
z.attr %s is a protected member of class Struct.N)
isinstancestr__dict__hasattrr   AttributeErrorr   r   )r   r   r   es       r   r   Struct.__setattr__X   sn    ( c3
 mm#wvs';';$DsJ 	+S( 	+ #*	+s   	A 
A6%A11A6c                 L     X   nU$ ! [          a  n[        U5      UeSnAff = f)aL  Get an attr by calling :meth:`dict.__getitem__`.

Like :meth:`__setattr__`, this method converts :exc:`KeyError` to
:exc:`AttributeError`.

Examples
--------
>>> s = Struct(a=10)
>>> s.a
10
>>> type(s.get)
<...method'>
>>> try:
...     s.b
... except AttributeError:
...     print("I don't have that key")
...
I don't have that key
N)r   r   )r   r   resultr   s       r   __getattr__Struct.__getattr__z   s4    (	YF M  	- %1,	-s    
##c                 (    U R                  U5        U $ )zs += s2 is a shorthand for s.merge(s2).

Examples
--------
>>> s = Struct(a=10,b=30)
>>> s2 = Struct(a=20,c=40)
>>> s += s2
>>> sorted(s.keys())
['a', 'b', 'c']
)merge)r   others     r   __iadd__Struct.__iadd__   s     	

5r   c                 H    U R                  5       nUR                  U5        U$ )zs + s2 -> New Struct made from s.merge(s2).

Examples
--------
>>> s1 = Struct(a=10,b=30)
>>> s2 = Struct(a=20,c=40)
>>> s = s1 + s2
>>> sorted(s.keys())
['a', 'b', 'c']
)copyr$   r   r%   souts      r   __add__Struct.__add__   s      yy{

5r   c                 .    U R                  5       nX!-  nU$ )zs1 - s2 -> remove keys in s2 from s1.

Examples
--------
>>> s1 = Struct(a=10,b=30)
>>> s2 = Struct(a=40)
>>> s = s1 - s2
>>> s
{'b': 30}
)r)   r*   s      r   __sub__Struct.__sub__   s     yy{r   c                 F    UR                  5        H  nX ;   d  M
  X	 M     U $ )zInplace remove keys from self that are in other.

Examples
--------
>>> s1 = Struct(a=10,b=30)
>>> s2 = Struct(a=40)
>>> s1 -= s2
>>> s1
{'b': 30}
)keys)r   r%   ks      r   __isub__Struct.__isub__   s%     AyG  r   c                     0 nUR                  5        H7  u  p4[        U[        5      (       a  UR                  5       nU H  nX2U'   M	     M9     U$ )zHelper function for merge.

Takes a dictionary whose values are lists and returns a dict with
the elements of each list as keys and the original keys as values.
)itemsr   r   split)r   dataoutdictr3   lstentrys         r   __dict_invertStruct.__dict_invert   sK     ZZ\EA#s##iik!"  "
 r   c                     U $ N r   s    r   r	   Struct.dict   s    r   c                 >    [        [        R                  U 5      5      $ )zvReturn a copy as a Struct.

Examples
--------
>>> s = Struct(a=10,b=30)
>>> s2 = s.copy()
>>> type(s2) is Struct
True
)r   r	   r)   rB   s    r   r)   Struct.copy   s     diio&&r   c                 
    X;   $ )zhasattr function available as a method.

Implemented like has_key.

Examples
--------
>>> s = Struct(a=10)
>>> s.hasattr('a')
True
>>> s.hasattr('b')
False
>>> s.hasattr('get')
False
rA   )r   r   s     r   r   Struct.hasattr   s     {r   c                 2    [         R                  U SU5        g)zSet whether new attributes can be created in this Struct.

This can be used to catch typos by verifying that the attribute user
tries to change already exists in this Struct.
r   N)r   r   )r   allows     r   allow_new_attrStruct.allow_new_attr   s     	4e4r   Nc                    [        U40 UD6nS nS nS nS nS n	[         R                  X5      n
U(       ag  UR                  5       nSU4SU4SU4S	U4S
U	44 H"  u  pXR                  5       ;   d  M  X   X'   X	 M$     U
R	                  U R                  U5      5        U H!  nX;  a  XN   X'   M  X   " X   XN   5      X'   M#     g)a
  Merge two Structs with customizable conflict resolution.

This is similar to :meth:`update`, but much more flexible. First, a
dict is made from data+key=value pairs. When merging this dict with
the Struct S, the optional dictionary 'conflict' is used to decide
what to do.

If conflict is not given, the default behavior is to preserve any keys
with their current value (the opposite of the :meth:`update` method's
behavior).

Parameters
----------
__loc_data__ : dict, Struct
    The data to merge into self
__conflict_solve : dict
    The conflict policy dict.  The keys are binary functions used to
    resolve the conflict and the values are lists of strings naming
    the keys the conflict resolution function applies to.  Instead of
    a list of strings a space separated string can be used, like
    'a b c'.
**kw : dict
    Additional key, value pairs to merge in

Notes
-----
The `__conflict_solve` dict is a dictionary of binary functions which will be used to
solve key conflicts.  Here is an example::

    __conflict_solve = dict(
        func1=['a','b','c'],
        func2=['d','e']
    )

In this case, the function :func:`func1` will be used to resolve
keys 'a', 'b' and 'c' and the function :func:`func2` will be used for
keys 'd' and 'e'.  This could also be written as::

    __conflict_solve = dict(func1='a b c',func2='d e')

These functions will be called for each key they apply to with the
form::

    func1(self['a'], other['a'])

The return value is used as the final merged value.

As a convenience, merge() provides five (the most commonly needed)
pre-defined policies: preserve, update, add, add_flip and add_s. The
easiest explanation is their implementation::

    preserve = lambda old,new: old
    update   = lambda old,new: new
    add      = lambda old,new: old + new
    add_flip = lambda old,new: new + old  # note change of order!
    add_s    = lambda old,new: old + ' ' + new  # only for str!

You can use those four words (as strings) as keys instead
of defining them as functions, and the merge method will substitute
the appropriate functions for you.

For more complicated conflict resolution policies, you still need to
construct your own functions.

Examples
--------
This show the default policy:

>>> s = Struct(a=10,b=30)
>>> s2 = Struct(a=20,c=40)
>>> s.merge(s2)
>>> sorted(s.items())
[('a', 10), ('b', 30), ('c', 40)]

Now, show how to specify a conflict dict:

>>> s = Struct(a=10,b=30)
>>> s2 = Struct(a=20,b=40)
>>> conflict = {'update':'a','add':'b'}
>>> s.merge(s2,conflict)
>>> sorted(s.items())
[('a', 20), ('b', 70)]
c                     U $ r@   rA   oldnews     r   <lambda>Struct.merge.<locals>.<lambda>`      3r   c                     U$ r@   rA   rN   s     r   rQ   rR   a  rS   r   c                 
    X-   $ r@   rA   rN   s     r   rQ   rR   b      39r   c                 
    X-   $ r@   rA   rN   s     r   rQ   rR   c  rV   r   c                     U S-   U-   $ )N rA   rN   s     r   rQ   rR   d  s    39s?r   preserveupdateaddadd_flipadd_sN)r	   fromkeysr)   r2   r[   _Struct__dict_invert)r   __loc_data___Struct__conflict_solver   	data_dictrZ   r[   r\   r]   r^   conflict_solveinv_conflict_solve_usernamefuncr   s                  r   r$   Struct.merge  s    j ++	 '&,,2 t6 &6&;&;&=# *84x6G %c{Z,A '0
 77994K4Q+1/50 !!$"4"45L"MNC%N	*/	).I		 r   rA   )T)NN)__name__
__module____qualname____firstlineno____doc__r   r
   r   r   r!   r&   r,   r/   r4   r`   r	   r)   r   rJ   r$   __static_attributes__rA   r   r   r   r      sW    
 I)0+. +D6 
'"5sJr   N)rm   __all__r	   r   rA   r   r   <module>rp      s#   & *^JT ^Jr   