MCPcopy Create free account
hub / github.com/ipython/ipython / Struct

Class Struct

IPython/utils/ipstruct.py:28–390  ·  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

26
27
28class Struct(dict):
29 """A dict subclass with attribute style access.
30
31 This dict subclass has a a few extra features:
32
33 * Attribute style access.
34 * Protection of class members (like keys, items) when using attribute
35 style access.
36 * The ability to restrict assignment to only existing keys.
37 * Intelligent merging.
38 * Overloaded operators.
39 """
40 _allownew = True
41 def __init__(self, *args, **kw):
42 """Initialize with a dictionary, another Struct, or data.
43
44 Parameters
45 ----------
46 args : dict, Struct
47 Initialize with one dict or Struct
48 kw : dict
49 Initialize with key, value pairs.
50
51 Examples
52 --------
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, value):
67 """Set an item with check for allownew.
68
69 Examples
70 --------
71
72 >>> s = Struct()
73 >>> s['a'] = 10
74 >>> s.allow_new_attr(False)
75 >>> s['a'] = 10
76 >>> s['a']
77 10
78 >>> try:
79 ... s['b'] = 20
80 ... except KeyError:
81 ... print('this is not allowed')
82 ...
83 this is not allowed
84 """
85 if not self._allownew and key not in self:

Callers 8

__init__Method · 0.90
init_instance_attrsMethod · 0.90
init_hooksMethod · 0.90
_object_findMethod · 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