Package pyanaconda :: Module indexed_dict
[hide private]
[frames] | no frames]

Source Code for Module pyanaconda.indexed_dict

 1  # indexed_dict.py 
 2  # Implements IndexedDictionary class. 
 3  # 
 4  # Copyright (C) 2009  Red Hat, Inc. 
 5  # 
 6  # This copyrighted material is made available to anyone wishing to use, 
 7  # modify, copy, or redistribute it subject to the terms and conditions of 
 8  # the GNU General Public License v.2, or (at your option) any later version. 
 9  # This program is distributed in the hope that it will be useful, but WITHOUT 
10  # ANY WARRANTY expressed or implied, including the implied warranties of 
11  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General 
12  # Public License for more details.  You should have received a copy of the 
13  # GNU General Public License along with this program; if not, write to the 
14  # Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 
15  # 02110-1301, USA.  Any Red Hat trademarks that are incorporated in the 
16  # source code or documentation are not subject to the GNU General Public 
17  # License and may only be used or replicated with the express permission of 
18  # Red Hat, Inc. 
19  # 
20   
21 -class IndexedDict(dict):
22 """ Indexed dictionary that remembers order of the inserted elements. 23 24 Values can be inserted with string keys only, but referenced by both 25 string keys or index. 26 27 There's a unit test for the class, please maintain it along. 28 """
29 - def __init__(self):
30 super(IndexedDict, self).__init__() 31 self._indexes = []
32
33 - def __getitem__(self, key):
34 if type(key) is int: 35 key = self._indexes[key] 36 return super(IndexedDict, self).__getitem__(key)
37
38 - def __setitem__(self, key, value):
39 if type(key) is int: 40 raise TypeError("IndexedDict only accepts strings as new keys") 41 assert(len(self) == len(self._indexes)) 42 self._indexes.append(key) 43 return super(IndexedDict, self).__setitem__(key, value)
44
45 - def index(self, string_key):
46 return self._indexes.index(string_key)
47