ATLAS Offline Software
Public Member Functions | Public Attributes | List of all members
python.StackedDict.StackedDict Class Reference
Inheritance diagram for python.StackedDict.StackedDict:
Collaboration diagram for python.StackedDict.StackedDict:

Public Member Functions

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

Public Attributes

 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__()

def 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__()

def 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__()

def 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__()

def 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__()

def 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()

def 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()

def 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

Definition at line 62 of file StackedDict.py.


The documentation for this class was generated from the following file:
python.Bindings.__iter__
__iter__
Definition: Control/AthenaPython/python/Bindings.py:791
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
plotBeamSpotVxVal.range
range
Definition: plotBeamSpotVxVal.py:195
python.processes.powheg.ZZ.ZZ.__init__
def __init__(self, base_directory, **kwargs)
Constructor: all process options are set here.
Definition: ZZ.py:18
python.Bindings.__setitem__
__setitem__
Definition: Control/AthenaPython/python/Bindings.py:771
calibdata.copy
bool copy
Definition: calibdata.py:27
python.Bindings.__getitem__
__getitem__
Definition: Control/AthenaPython/python/Bindings.py:779