MCPcopy Index your code
hub / github.com/ipython/ipython / Struct

Class Struct

IPython/utils/ipstruct.py:29–379  ·  view source on GitHub ↗

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. * Intellig

Source from the content-addressed store, hash-verified

27
28
29class Struct(dict):
30 """A dict subclass with attribute style access.
31
32 This dict subclass has a a few extra features:
33
34 * Attribute style access.
35 * Protection of class members (like keys, items) when using attribute
36 style access.
37 * The ability to restrict assignment to only existing keys.
38 * Intelligent merging.
39 * Overloaded operators.
40 """
41 _allownew = True
42 def __init__(self, *args, **kw):
43 """Initialize with a dictionary, another Struct, or data.
44
45 Parameters
46 ----------
47 *args : dict, Struct
48 Initialize with one dict or Struct
49 **kw : dict
50 Initialize with key, value pairs.
51
52 Examples
53 --------
54 >>> s = Struct(a=10,b=30)
55 >>> s.a
56 10
57 >>> s.b
58 30
59 >>> s2 = Struct(s,c=30)
60 >>> sorted(s2.keys())
61 ['a', 'b', 'c']
62 """
63 object.__setattr__(self, '_allownew', True)
64 dict.__init__(self, *args, **kw)
65
66 def __setitem__(self, key: str, value: Any):
67 """Set an item with check for allownew.
68
69 Examples
70 --------
71 >>> s = Struct()
72 >>> s['a'] = 10
73 >>> s.allow_new_attr(False)
74 >>> s['a'] = 10
75 >>> s['a']
76 10
77 >>> try:
78 ... s['b'] = 20
79 ... except KeyError:
80 ... print('this is not allowed')
81 ...
82 this is not allowed
83 """
84 if not self._allownew and key not in self:
85 raise KeyError(
86 "can't create new attribute %s when allow_new_attr(False)" % key)

Callers 6

init_instance_attrsMethod · 0.90
init_hooksMethod · 0.90
_run_with_profilerMethod · 0.90
doctest_modeMethod · 0.90
copyMethod · 0.85
parse_optionsMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…