ATLAS Offline Software
Loading...
Searching...
No Matches
python.StackedDict.StackedDict Class Reference
Inheritance diagram for python.StackedDict.StackedDict:
Collaboration diagram for python.StackedDict.StackedDict:

Public Member Functions

 __init__ (self, *dicts)
 __getitem__ (self, k)
 __setitem__ (self, k, v)
 __len__ (self)
 __iter__ (self)
 has_key (self, k)
 copy (self)

Public Attributes

 dicts = dicts

Detailed Description

Allow lookups in multiple dictionaries while writing to one.

A StackedDict references a list of other dictionaries.
Reads try each dictionary in sequence, succeeding with the first dictionary
that contains the target key.  Modifications go to the last dictionary
in the list.

Notes: No attempt is made to deal with keys duplicated between dictionaries;
len() and __iter__ will process them all.  __delitem__ is not implemented.

Examples:
>>> from pprint import pprint
>>> d1 = {'a':'b'}
>>> d2 = {'c':'d'}
>>> d3 = {'e':'f'}
>>> d = StackedDict (d1, d2, d3)
>>> d['a']
'b'
>>> d['c']
'd'
>>> d['e']
'f'
>>> len(d)
3
>>> [x for x in d]
['a', 'c', 'e']
>>> 'c' in d
True
>>> 'c' in d
True
>>> 'd' in d
False
>>> d['c'] = 10
>>> d['c']
10
>>> d2
{'c': 10}
>>> d['x'] = 20
>>> d['x']
20
>>> pprint(d3)
{'e': 'f', 'x': 20}
>>> pprint(d.copy())
{'a': 'b', 'c': 10, 'e': 'f', 'x': 20}

Definition at line 14 of file StackedDict.py.

Constructor & Destructor Documentation

◆ __init__()

python.StackedDict.StackedDict.__init__ ( self,
* dicts )

Definition at line 61 of file StackedDict.py.

61 def __init__ (self, *dicts):
62 self.dicts = dicts
63 return
64
65

Member Function Documentation

◆ __getitem__()

python.StackedDict.StackedDict.__getitem__ ( self,
k )

Definition at line 66 of file StackedDict.py.

66 def __getitem__ (self, k):
67 for d in self.dicts:
68 if k in d:
69 return d[k]
70 raise KeyError (k)
71
72

◆ __iter__()

python.StackedDict.StackedDict.__iter__ ( self)

Definition at line 86 of file StackedDict.py.

86 def __iter__ (self):
87 for d in self.dicts:
88 for x in d: yield x
89 return
90
91

◆ __len__()

python.StackedDict.StackedDict.__len__ ( self)

Definition at line 82 of file StackedDict.py.

82 def __len__ (self):
83 return sum([len(d) for d in self.dicts])
84
85

◆ __setitem__()

python.StackedDict.StackedDict.__setitem__ ( self,
k,
v )

Definition at line 73 of file StackedDict.py.

73 def __setitem__ (self, k, v):
74 for d in self.dicts[:-1]:
75 if k in d:
76 d[k] = v
77 return
78 self.dicts[-1][k] = v
79 return
80
81

◆ copy()

python.StackedDict.StackedDict.copy ( self)

Definition at line 104 of file StackedDict.py.

104 def copy (self):
105 d = {}
106 for i in range(len(self.dicts)-1, -1, -1):
107 d.update(self.dicts[i])
108 return d
109

◆ has_key()

python.StackedDict.StackedDict.has_key ( self,
k )
Some code still uses this.
>>> d1 = {'a':'b'}
>>> d = StackedDict (d1)
>>> d.has_key ('a')
True
>>> d.has_key ('z')
False

Definition at line 92 of file StackedDict.py.

92 def has_key (self, k):
93 """Some code still uses this.
94 >>> d1 = {'a':'b'}
95 >>> d = StackedDict (d1)
96 >>> d.has_key ('a')
97 True
98 >>> d.has_key ('z')
99 False
100 """
101 return k in self
102
103

Member Data Documentation

◆ dicts

python.StackedDict.StackedDict.dicts = dicts

Definition at line 62 of file StackedDict.py.


The documentation for this class was generated from the following file: