1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2016 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, or (at your option) any later version.
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 ## \defgroup enumerate enumerate
23 # \details Emulates a C-like enum for python
26 __author__ = "gboulant"
27 __date__ = "$1 avr. 2010 09:08:02$"
29 ## This class emulates a C-like enum for python. It is initialized with a list
30 # of strings to be used as the enum symbolic keys. The enum values are automatically
31 # generated as sequencing integer starting at the specified offset value.
33 class Enumerate(object):
35 This class emulates a C-like enum for python. It is initialized with a list
36 of strings to be used as the enum symbolic keys. The enum values are automatically
37 generated as sequencing integer starting at the specified offset value.
40 ## Canonical constructor.
41 # \param keys a list of string to be used as the enum symbolic keys. The enum values
42 # are automatically generated as a sequence of integers starting at the specified
44 def __init__(self, keys, offset=0):
47 @keys a list of string to be used as the enum symbolic keys. The enum values
48 are automatically generated as a sequence of integers starting at the specified
51 self._dict_keynumbers = {}
52 for number, key in enumerate(keys):
53 value = offset + number
54 setattr(self, key, value)
55 self._dict_keynumbers[key] = value
56 self._dict_numberkeys = {v: k for k, v in self._dict_keynumbers.items()}
58 ## Return true if this enumerate contains the specified key string
59 # \param key a key string to test
60 def contains(self, key):
62 Return true if this enumerate contains the specified key string
63 @key a key string to test
65 return key in self._dict_keynumbers
67 ## Returns true if the specified integer value is defined as an identifier
69 # \param value a value to test
70 def isValid(self, value):
72 Returns true if the specified integer value is defined as an identifier
74 @value a value to test
76 return value in self._dict_numberkeys
78 ## Returns the list of keys in this enumerate.
81 Returns the list of keys in this enumerate.
83 return sorted(self._dict_keynumbers)
85 ## Returns the list of values specified to initiate this enumerate.
88 Returns the list of values specified to initiate this enumerate.
90 return sorted(self._dict_numberkeys)
92 ## Returns the symbolic key string associated to the specified identifier value.
93 # \param value an integer value whose associated key string is requested.
94 def keyOf(self, value):
96 Returns the symbolic key string associated to the specified identifier
98 @param value : an integer value whose associated key string is requested.
100 if not self.isValid(value):
102 # _MEM_ We assume here that the keys and associated values are in the
103 # same order in their list.
104 return self._dict_numberkeys[value]
106 # If not, weshould use a longer implementation such that:
107 #for key in self._dict_keynumbers.keys():
108 # if self._dict_keynumbers[key] == value:
112 # ==============================================================================
113 # Basic use cases and unit test functions
114 # ==============================================================================
118 TYPES_LIST = Enumerate([
122 print(TYPES_LIST.listvalues())
125 def TEST_createFromList():
127 'KERNEL', # This should take the value 0
128 'GUI', # This should take the value 1
135 if (codes.KERNEL == 0 and codes.GEOM == 2):
140 def TEST_createFromString():
141 aList = "KERNEL GUI GEOM MED"
143 codes = Enumerate(aList.split())
147 if (codes.KERNEL == 0 and codes.GEOM == 2):
154 'KERNEL', # This should take the value 0
155 'GUI', # This should take the value 1
160 print("VISU in enumerate?", codes.contains("VISU"))
161 if (not codes.contains("VISU")):
168 'KERNEL', # This should take the value 0
169 'GUI', # This should take the value 1
174 if (not codes.isValid(23)):
181 'KERNEL', # This should take the value 0
182 'GUI', # This should take the value 1
189 if (codes.KERNEL == 20 and codes.GEOM == 22):
194 def TEST_listvalues():
196 'KERNEL', # This should take the value 0
197 'GUI', # This should take the value 1
202 print(codes.listvalues())
203 if codes.listvalues() != [20, 21, 22, 23, 24]:
209 'KERNEL', # This should take the value 0
210 'GUI', # This should take the value 1
215 if ( codes.keyOf(codes.KERNEL) != 'KERNEL' or
216 codes.keyOf(codes.GUI) != 'GUI' or
217 codes.keyOf(codes.GEOM) != 'GEOM' or
218 codes.keyOf(codes.MED) != 'MED' or
219 codes.keyOf(codes.SMESH) != 'SMESH'):
223 if __name__ == "__main__":
224 from . import unittester
225 unittester.run("enumerate", "TEST_simple")
226 unittester.run("enumerate", "TEST_createFromList")
227 unittester.run("enumerate", "TEST_createFromString")
228 unittester.run("enumerate", "TEST_contains")
229 unittester.run("enumerate", "TEST_isValid")
230 unittester.run("enumerate", "TEST_offset")
231 unittester.run("enumerate", "TEST_listvalues")
232 unittester.run("enumerate", "TEST_keyOf")