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
57 ## Return true if this enumerate contains the specified key string
58 # \param key a key string to test
59 def contains(self, key):
61 Return true if this enumerate contains the specified key string
62 @key a key string to test
64 return (key in list(self._dict_keynumbers.keys()))
66 ## Returns true if the specified integer value is defined as an identifier
68 # \param value a value to test
69 def isValid(self, value):
71 Returns true if the specified integer value is defined as an identifier
73 @value a value to test
75 return (value in list(self._dict_keynumbers.values()))
77 ## Returns the list of keys in this enumerate.
80 Returns the list of keys in this enumerate.
82 list = list(self._dict_keynumbers.keys())
86 ## Returns the list of values specified to initiate this enumerate.
89 Returns the list of values specified to initiate this enumerate.
91 list = list(self._dict_keynumbers.values())
95 ## Returns the symbolic key string associated to the specified identifier value.
96 # \param value an integer value whose associated key string is requested.
97 def keyOf(self, value):
99 Returns the symbolic key string associated to the specified identifier
101 @param value : an integer value whose associated key string is requested.
103 if not self.isValid(value):
105 # _MEM_ We assume here that the keys and associated values are in the
106 # same order in their list.
107 return list(self._dict_keynumbers.keys())[list(self._dict_keynumbers.values()).index(value)]
109 # If not, weshould use a longer implementation such that:
110 #for key in self._dict_keynumbers.keys():
111 # if self._dict_keynumbers[key] == value:
115 # ==============================================================================
116 # Basic use cases and unit test functions
117 # ==============================================================================
121 TYPES_LIST = Enumerate([
125 print(TYPES_LIST.listvalues())
128 def TEST_createFromList():
130 'KERNEL', # This should take the value 0
131 'GUI', # This should take the value 1
138 if (codes.KERNEL == 0 and codes.GEOM == 2):
143 def TEST_createFromString():
144 aList = "KERNEL GUI GEOM MED"
146 codes = Enumerate(aList.split())
150 if (codes.KERNEL == 0 and codes.GEOM == 2):
157 'KERNEL', # This should take the value 0
158 'GUI', # This should take the value 1
163 print("VISU in enumerate?", codes.contains("VISU"))
164 if (not codes.contains("VISU")):
171 'KERNEL', # This should take the value 0
172 'GUI', # This should take the value 1
177 if (not codes.isValid(23)):
184 'KERNEL', # This should take the value 0
185 'GUI', # This should take the value 1
192 if (codes.KERNEL == 20 and codes.GEOM == 22):
197 def TEST_listvalues():
199 'KERNEL', # This should take the value 0
200 'GUI', # This should take the value 1
205 print(codes.listvalues())
206 if codes.listvalues() != [20, 21, 22, 23, 24]:
212 'KERNEL', # This should take the value 0
213 'GUI', # This should take the value 1
218 if ( codes.keyOf(codes.KERNEL) != 'KERNEL' or
219 codes.keyOf(codes.GUI) != 'GUI' or
220 codes.keyOf(codes.GEOM) != 'GEOM' or
221 codes.keyOf(codes.MED) != 'MED' or
222 codes.keyOf(codes.SMESH) != 'SMESH'):
226 if __name__ == "__main__":
227 from . import unittester
228 unittester.run("enumerate", "TEST_simple")
229 unittester.run("enumerate", "TEST_createFromList")
230 unittester.run("enumerate", "TEST_createFromString")
231 unittester.run("enumerate", "TEST_contains")
232 unittester.run("enumerate", "TEST_isValid")
233 unittester.run("enumerate", "TEST_offset")
234 unittester.run("enumerate", "TEST_listvalues")
235 unittester.run("enumerate", "TEST_keyOf")