PhysicsTools
HeppyCore
python
utils
diclist.py
Go to the documentation of this file.
1
# Copyright (C) 2014 Colin Bernet
2
# https://github.com/cbernet/heppy/blob/master/LICENSE
3
4
class
diclist
( list ):
5
'''list with an internal dictionary for indexing,
6
allowing to keep dictionary elements ordered.
7
keys can be everything except an integer.
8
'''
9
10
def
__init__
(self):
11
super( diclist, self).
__init__
()
12
# internal dictionary, will contain key -> index in list
13
self.
dico
= {}
14
15
def
add
( self, key, value ):
16
if
isinstance(key, (int, long)):
17
raise
ValueError(
"key cannot be an integer"
)
18
if
key
in
self.
dico
:
19
raise
ValueError(
"key '{key}' already exists"
.
format
(key=key) )
20
self.
dico
[key] = len(self)
21
self.append(value)
22
23
def
__getitem__
(self, index):
24
'''index can be a dictionary key, or an integer specifying
25
the rank of the value to be accessed
26
'''
27
try
:
28
# if index is an integer (the rank), use the list.
29
return
super(diclist, self).
__getitem__
(index)
30
except
(TypeError, ValueError):
31
# else it's the dictionary key.
32
# use the internal dictionary to get the index,
33
# and return the corresponding value from the list
34
return
super(diclist, self).
__getitem__
( self.
dico
[index] )
35
36
def
__setitem__
(self, index, value):
37
'''These functions are quite risky...'''
38
try
:
39
return
super(diclist, self).
__setitem__
(index, value)
40
except
TypeError
as
ValueError:
41
return
super(diclist, self).
__setitem__
( self.
dico
[index], value )
42
43
44
diclist.diclist.dico
dico
Definition:
diclist.py:13
diclist.diclist
Definition:
diclist.py:4
diclist.diclist.add
def add(self, key, value)
Definition:
diclist.py:15
diclist.diclist.__init__
def __init__(self)
Definition:
diclist.py:10
diclist.diclist.__setitem__
def __setitem__(self, index, value)
Definition:
diclist.py:36
format
diclist.diclist.__getitem__
def __getitem__(self, index)
Definition:
diclist.py:23
Generated for CMSSW Reference Manual by
1.8.16