wL iP.$dZdgZGddeZy)zA dict subclass that supports attribute style access. Authors: * Fernando Perez (original) * Brian Granger (refactoring to a dict subclass) StructcleZdZdZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZddZddZy)raYA 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. Tcdtj|ddtj|g|i|y)aInitialize 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__)selfargskws \/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/IPython/utils/ipstruct.pyr zStruct.__init__)s,* 4d3 d(T(R(cn|js||vrtd|ztj|||y)azSet 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)rKeyErrorr __setitem__)r keyvalues r rzStruct.__setitem__As:$~~#T/JSPR R sE*rct|tr,||jvstt|rt d|z |j ||y#t$r}t ||d}~wwxYw)aSet 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) isinstancestr__dict__hasattrrAttributeErrorrr)r rres r rzStruct.__setattr__Xsl( c3  dmm#wvs';$DsJ +   S% ( + # * +sA A+ A&&A+cL ||}|S#t$r}t||d}~wwxYw)aGet 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)rr)r rresultrs r __getattr__zStruct.__getattr__zs6( #YFM - %1 , -s # #c(|j||S)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__zStruct.__iadd__s 5 rcH|j}|j||S)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'] )copyrr r souts r __add__zStruct.__add__s yy{ 5 rc0|j}||z}|S)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__zStruct.__sub__syy{   rc@|jD] }||vs||= |S)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__zStruct.__isub__s. ADyG  rci}|jD]1\}}t|tr|j}|D]}|||< 3|S)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. )itemsrrsplit)r dataoutdictr+lstentrys r __dict_invertzStruct.__dict_invertsV ZZ\ #EAc#s#iik #!" # # rc|SNr s r rz Struct.dicts rc>ttj|S)zReturn a copy as a Struct. Examples -------- >>> s = Struct(a=10,b=30) >>> s2 = s.copy() >>> type(s2) is Struct True )rrr#r8s r r#z Struct.copysdiio&&rc ||vS)ahasattr 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 r7)r rs r rzStruct.hasattrsd{rc2tj|d|y)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. rN)rr)r allows r allow_new_attrzStruct.allow_new_attrs 4e4rNc t|fi|}d}d}d}d}d} tj||} |rg|j} d|fd|fd|fd |fd | ffD]#\} } | | jvs| | | | <| | =%| j |j | |D]$}||vr ||||<| |||||||<&y ) 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|Sr6r7oldnews r zStruct.merge..`3rc|Sr6r7r@s r rCzStruct.merge..arDrc ||zSr6r7r@s r rCzStruct.merge..b 39rc ||zSr6r7r@s r rCzStruct.merge..crGrc|dz|zS)N r7r@s r rCzStruct.merge..ds39s?rpreserveupdateaddadd_flipadd_sN)rfromkeysr#r*rL_Struct__dict_invert)r __loc_data___Struct__conflict_solver data_dictrKrLrMrNrOconflict_solveinv_conflict_solve_usernamefuncrs r rz Struct.mergesj++ '&,,2tX6 &6&;&;&= # *84x6G %c{Z,A '0 6 d277994KD4Q+D1/5  6  ! !$"4"45L"M N JC$%cNS /N3/S )C.IS  Jr)T)NN)__name__ __module__ __qualname____doc__rr rrrr!r&r(r,rQrr#rr=rr7rr rrsW I)0+. +D6     '"5sJrN)r\__all__rrr7rr r^s#& *^JT^Jr