Format: application/x-python-code
1 o
2
3 ��hP. � @ s d Z dgZG dd� de�ZdS )z�A dict subclass that supports attribute style access.
4
5 Authors:
6
7 * Fernando Perez (original)
8 * Brian Granger (refactoring to a dict subclass)
9 �Structc @ s� e Zd ZdZdZdd� Zdd� Zdd� Zd d
10 � Zdd� Z d
11 d� Z
12 dd� Zdd� Zdd� Z
13 dd� Zdd� Zdd� Zd dd�Zd!dd�ZdS )"r aY A dict subclass with attribute style access.
14
15 This dict subclass has a a few extra features:
16
17 * Attribute style access.
18 * Protection of class members (like keys, items) when using attribute
19 style access.
20 * The ability to restrict assignment to only existing keys.
21 * Intelligent merging.
22 * Overloaded operators.
23 Tc O s* t �| dd� tj| g|�R i |�� dS )a� Initialize with a dictionary, another Struct, or data.
24
25 Parameters
26 ----------
27 *args : dict, Struct
28 Initialize with one dict or Struct
29 **kw : dict
30 Initialize with key, value pairs.
31
32 Examples
33 --------
34 >>> s = Struct(a=10,b=30)
35 >>> s.a
36 10
37 >>> s.b
38 30
39 >>> s2 = Struct(s,c=30)
40 >>> sorted(s2.keys())
41 ['a', 'b', 'c']
42 � _allownewTN)�object�__setattr__�dict�__init__)�self�args�kw� r
43 �Lc:\Users\shubh\Videos\agent\nova\Lib\site-packages\IPython/utils/ipstruct.pyr ) s zStruct.__init__c C s, | j s
44 || vr
45 td| ��t�| ||� dS )az Set an item with check for allownew.
46
47 Examples
48 --------
49 >>> s = Struct()
50 >>> s['a'] = 10
51 >>> s.allow_new_attr(False)
52 >>> s['a'] = 10
53 >>> s['a']
54 10
55 >>> try:
56 ... s['b'] = 20
57 ... except KeyError:
58 ... print('this is not allowed')
59 ...
60 this is not allowed
61 z8can't create new attribute %s when allow_new_attr(False)N)r �KeyErrorr �__setitem__)r �key�valuer
62 r
63 r r
64 A s
65 �zStruct.__setitem__c
66 C s` t |t�r|| jv stt|�rtd| ��z | �||� W dS ty/ } zt|�|�d}~ww )a� Set an attr with protection of class members.
67
68 This calls :meth:`self.__setitem__` but convert :exc:`KeyError` to
69 :exc:`AttributeError`.
70
71 Examples
72 --------
73 >>> s = Struct()
74 >>> s.a = 10
75 >>> s.a
76 10
77 >>> try:
78 ... s.get = 10
79 ... except AttributeError:
80 ... print("you can't set a class member")
81 ...
82 you can't set a class member
83 z.attr %s is a protected member of class Struct.N)�
84 isinstance�str�__dict__�hasattrr �AttributeErrorr
85 r )r r r �er
86 r
87 r r X s
88 �
89 ��zStruct.__setattr__c
90 C s2 z| | }W |S t y } zt|�|�d}~ww )a� Get an attr by calling :meth:`dict.__getitem__`.
91
92 Like :meth:`__setattr__`, this method converts :exc:`KeyError` to
93 :exc:`AttributeError`.
94
95 Examples
96 --------
97 >>> s = Struct(a=10)
98 >>> s.a
99 10
100 >>> type(s.get)
101 <...method'>
102 >>> try:
103 ... s.b
104 ... except AttributeError:
105 ... print("I don't have that key")
106 ...
107 I don't have that key
108 N)r r )r r �resultr r
109 r
110 r �__getattr__z s
111 �
112 ��zStruct.__getattr__c C s | � |� | S )z�s += s2 is a shorthand for s.merge(s2).
113
114 Examples
115 --------
116 >>> s = Struct(a=10,b=30)
117 >>> s2 = Struct(a=20,c=40)
118 >>> s += s2
119 >>> sorted(s.keys())
120 ['a', 'b', 'c']
121 )�merge)r �otherr
122 r
123 r �__iadd__� s
124 zStruct.__iadd__c C s | � � }|�|� |S )z�s + s2 -> New Struct made from s.merge(s2).
125
126 Examples
127 --------
128 >>> s1 = Struct(a=10,b=30)
129 >>> s2 = Struct(a=20,c=40)
130 >>> s = s1 + s2
131 >>> sorted(s.keys())
132 ['a', 'b', 'c']
133 )�copyr �r r Zsoutr
134 r
135 r �__add__� s
136 zStruct.__add__c C s | � � }||8 }|S )z�s1 - s2 -> remove keys in s2 from s1.
137
138 Examples
139 --------
140 >>> s1 = Struct(a=10,b=30)
141 >>> s2 = Struct(a=40)
142 >>> s = s1 - s2
143 >>> s
144 {'b': 30}
145 )r r r
146 r
147 r �__sub__� s zStruct.__sub__c C s |� � D ] }|| v r
148 | |= q| S )z�Inplace remove keys from self that are in other.
149
150 Examples
151 --------
152 >>> s1 = Struct(a=10,b=30)
153 >>> s2 = Struct(a=40)
154 >>> s1 -= s2
155 >>> s1
156 {'b': 30}
157 )�keys)r r �kr
158 r
159 r �__isub__� s
160 �zStruct.__isub__c C s>