1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010 EDF R&D
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License.
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # Lesser General Public License for more details.
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 __author__ = "gboulant"
24 __date__ = "$1 avr. 2010 09:08:02$"
26 class Enumerate(object):
28 This class emulates a C-like enum for python. It is initialized with a list
29 of strings to be used as the enum symbolic keys. The enum values are automatically
30 generated as sequencing integer starting at the specified offset value.
32 def __init__(self, keys, offset=0):
35 @keys a list of string to be used as the enum symbolic keys. The enum values
36 are automatically generated as a sequence of integers starting at the specified
39 self._dict_keynumbers = {}
40 for number, key in enumerate(keys):
41 value = offset + number
42 setattr(self, key, value)
43 self._dict_keynumbers[key] = value
45 def contains(self, key):
47 Return true if this enumerate contains the specified key string
48 @key a key string to test
50 return (key in self._dict_keynumbers.keys())
52 def isValid(self, value):
54 Returns true if the specified integer value is defined as an identifier
56 @value a value to test
58 return (value in self._dict_keynumbers.values())
62 Returns the list of keys in this enumerate.
64 list = self._dict_keynumbers.keys()
70 Returns the list of values specified to initiate this enumerate.
72 list = self._dict_keynumbers.values()
76 def keyOf(self, value):
78 Returns the symbolic key string associated to the specified identifier
80 @param value : an integer value whose associated key string is requested.
82 if not self.isValid(value):
84 # _MEM_ We assume here that the keys and associated values are in the
85 # same order in their list.
86 return self._dict_keynumbers.keys()[self._dict_keynumbers.values().index(value)]
88 # If not, weshould use a longer implementation such that:
89 #for key in self._dict_keynumbers.keys():
90 # if self._dict_keynumbers[key] == value:
94 # ==============================================================================
95 # Basic use cases and unit test functions
96 # ==============================================================================
100 TYPES_LIST = Enumerate([
104 print TYPES_LIST.listvalues()
107 def TEST_createFromList():
109 'KERNEL', # This should take the value 0
110 'GUI', # This should take the value 1
117 if (codes.KERNEL == 0 and codes.GEOM == 2):
122 def TEST_createFromString():
123 aList = "KERNEL GUI GEOM MED"
125 codes = Enumerate(aList.split())
129 if (codes.KERNEL == 0 and codes.GEOM == 2):
136 'KERNEL', # This should take the value 0
137 'GUI', # This should take the value 1
142 print "VISU in enumerate?", codes.contains("VISU")
143 if (not codes.contains("VISU")):
150 'KERNEL', # This should take the value 0
151 'GUI', # This should take the value 1
156 if (not codes.isValid(23)):
163 'KERNEL', # This should take the value 0
164 'GUI', # This should take the value 1
171 if (codes.KERNEL == 20 and codes.GEOM == 22):
176 def TEST_listvalues():
178 'KERNEL', # This should take the value 0
179 'GUI', # This should take the value 1
184 print codes.listvalues()
185 if codes.listvalues() != [20, 21, 22, 23, 24]:
191 'KERNEL', # This should take the value 0
192 'GUI', # This should take the value 1
197 if ( codes.keyOf(codes.KERNEL) != 'KERNEL' or
198 codes.keyOf(codes.GUI) != 'GUI' or
199 codes.keyOf(codes.GEOM) != 'GEOM' or
200 codes.keyOf(codes.MED) != 'MED' or
201 codes.keyOf(codes.SMESH) != 'SMESH'):
205 if __name__ == "__main__":
207 unittester.run("enumerate", "TEST_simple")
208 unittester.run("enumerate", "TEST_createFromList")
209 unittester.run("enumerate", "TEST_createFromString")
210 unittester.run("enumerate", "TEST_contains")
211 unittester.run("enumerate", "TEST_isValid")
212 unittester.run("enumerate", "TEST_offset")
213 unittester.run("enumerate", "TEST_listvalues")
214 unittester.run("enumerate", "TEST_keyOf")