1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2013 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2.1 of the License.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 __date__ ="$21 mai 2010 18:00:23$"
28 # search a dictionary for key or value
29 # using named functions or a class
30 # tested with Python25 by Ene Uran 01/19/2008
32 def find_key(dic, val):
33 """return the key of dictionary dic given the value"""
34 return [k for k, v in dic.iteritems() if v == val][0]
36 def find_value(dic, key):
37 """return the value of dictionary dic given the key"""
42 a dictionary which can lookup value by key, or keys by value
44 def __init__(self, items=[]):
45 """items can be a list of pair_lists or a dictionary"""
46 dict.__init__(self, items)
48 def get_keys(self, value):
49 """find the key(s) as a list given a value"""
50 return [item[0] for item in self.items() if item[1] == value]
52 def get_key(self, value):
54 find the key associated to the given a value. If several keys exist,
55 only the first is given. To get the whole list, use get_keys instead.
57 list = self.get_keys(value)
62 def get_value(self, key):
63 """find the value given a key"""
67 # ==============================================================================
68 # Use cases and unit tests
69 # ==============================================================================
71 def TEST_getTestDictionnary():
72 # dictionary of chemical symbols
83 def TEST_find_value():
84 symbol_dic = TEST_getTestDictionnary()
85 print find_key(symbol_dic, 'boron') # B
86 print find_value(symbol_dic, 'B') # boron
87 print find_value(symbol_dic, 'H') # hydrogen
88 if find_key(symbol_dic, 'nitrogen') != 'N':
93 symbol_dic = TEST_getTestDictionnary()
97 # use a dictionary as initialization argument
98 look = Lookup(symbol_dic)
99 print look.get_key(name) # [Li']
100 if look.get_key(name) != symbol:
101 print "get "+str(look.get_key(name))+" while "+str(symbol)+" was expected"
103 print look.get_value(symbol) # lithium
105 # use a list of pairs instead of a dictionary as initialization argument
106 # (will be converted to a dictionary by the class internally)
107 age_list = [['Fred', 23], ['Larry', 28], ['Ene', 23]]
108 look2 = Lookup(age_list)
109 print look2.get_keys(23) # ['Ene', 'Fred']
110 if look2.get_keys(23)[0] != 'Ene' or look2.get_keys(23)[1] != 'Fred':
111 print "get "+str(look2.get_keys(23))+" while ['Ene', 'Fred'] was expected"
113 print look2.get_value('Fred') # 23
116 if __name__ == '__main__':
118 unittester.run("diclookup", "TEST_find_value")
119 unittester.run("diclookup", "TEST_lookup")