1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2013 CEA/DEN, EDF R&D, OPEN CASCADE
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
21 __author__ = "gboulant"
22 __date__ = "$1 avr. 2010 09:08:02$"
24 class Enumerate(object):
26 This class emulates a C-like enum for python. It is initialized with a list
27 of strings to be used as the enum symbolic keys. The enum values are automatically
28 generated as sequencing integer starting at the specified offset value.
30 def __init__(self, keys, offset=0):
33 @keys a list of string to be used as the enum symbolic keys. The enum values
34 are automatically generated as a sequence of integers starting at the specified
37 self._dict_keynumbers = {}
38 for number, key in enumerate(keys):
39 value = offset + number
40 setattr(self, key, value)
41 self._dict_keynumbers[key] = value
43 def contains(self, key):
45 Return true if this enumerate contains the specified key string
46 @key a key string to test
48 return (key in self._dict_keynumbers.keys())
50 def isValid(self, value):
52 Returns true if the specified integer value is defined as an identifier
54 @value a value to test
56 return (value in self._dict_keynumbers.values())
60 Returns the list of keys in this enumerate.
62 list = self._dict_keynumbers.keys()
68 Returns the list of values specified to initiate this enumerate.
70 list = self._dict_keynumbers.values()
74 def keyOf(self, value):
76 Returns the symbolic key string associated to the specified identifier
78 @param value : an integer value whose associated key string is requested.
80 if not self.isValid(value):
82 # _MEM_ We assume here that the keys and associated values are in the
83 # same order in their list.
84 return self._dict_keynumbers.keys()[self._dict_keynumbers.values().index(value)]
86 # If not, weshould use a longer implementation such that:
87 #for key in self._dict_keynumbers.keys():
88 # if self._dict_keynumbers[key] == value:
92 # ==============================================================================
93 # Basic use cases and unit test functions
94 # ==============================================================================
98 TYPES_LIST = Enumerate([
102 print TYPES_LIST.listvalues()
105 def TEST_createFromList():
107 'KERNEL', # This should take the value 0
108 'GUI', # This should take the value 1
115 if (codes.KERNEL == 0 and codes.GEOM == 2):
120 def TEST_createFromString():
121 aList = "KERNEL GUI GEOM MED"
123 codes = Enumerate(aList.split())
127 if (codes.KERNEL == 0 and codes.GEOM == 2):
134 'KERNEL', # This should take the value 0
135 'GUI', # This should take the value 1
140 print "VISU in enumerate?", codes.contains("VISU")
141 if (not codes.contains("VISU")):
148 'KERNEL', # This should take the value 0
149 'GUI', # This should take the value 1
154 if (not codes.isValid(23)):
161 'KERNEL', # This should take the value 0
162 'GUI', # This should take the value 1
169 if (codes.KERNEL == 20 and codes.GEOM == 22):
174 def TEST_listvalues():
176 'KERNEL', # This should take the value 0
177 'GUI', # This should take the value 1
182 print codes.listvalues()
183 if codes.listvalues() != [20, 21, 22, 23, 24]:
189 'KERNEL', # This should take the value 0
190 'GUI', # This should take the value 1
195 if ( codes.keyOf(codes.KERNEL) != 'KERNEL' or
196 codes.keyOf(codes.GUI) != 'GUI' or
197 codes.keyOf(codes.GEOM) != 'GEOM' or
198 codes.keyOf(codes.MED) != 'MED' or
199 codes.keyOf(codes.SMESH) != 'SMESH'):
203 if __name__ == "__main__":
205 unittester.run("enumerate", "TEST_simple")
206 unittester.run("enumerate", "TEST_createFromList")
207 unittester.run("enumerate", "TEST_createFromString")
208 unittester.run("enumerate", "TEST_contains")
209 unittester.run("enumerate", "TEST_isValid")
210 unittester.run("enumerate", "TEST_offset")
211 unittester.run("enumerate", "TEST_listvalues")
212 unittester.run("enumerate", "TEST_keyOf")