Salome HOME
97500ab30de6ad43c84923661465be1da33beda9
[modules/kernel.git] / src / KERNEL_PY / kernel / enumerate.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2014  CEA/DEN, EDF R&D, OPEN CASCADE
3 #
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.
8 #
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.
13 #
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
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 ## \defgroup enumerate enumerate
22 #  \{ 
23 #  \details Emulates a C-like enum for python
24 #  \}
25
26 __author__ = "gboulant"
27 __date__ = "$1 avr. 2010 09:08:02$"
28
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.
32 #  \ingroup enumerate
33 class Enumerate(object):
34     """
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.
38     """
39     
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
43     #  offset value.
44     def __init__(self, keys, offset=0):
45         """
46         Canonical constructor
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
49         offset value.
50         """
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
57     ## Return true if this enumerate contains the specified key string
58     #  \param key a key string to test
59     def contains(self, key):
60         """
61         Return true if this enumerate contains the specified key string
62         @key a key string to test
63         """
64         return (key in self._dict_keynumbers.keys())
65
66     ## Returns true if the specified integer value is defined as an identifier
67     #  in this enumarate.
68     #  \param value a value to test
69     def isValid(self, value):
70         """
71         Returns true if the specified integer value is defined as an identifier
72         in this enumarate.
73         @value a value to test
74         """
75         return (value in self._dict_keynumbers.values())
76
77     ## Returns the list of keys in this enumerate.
78     def listkeys(self):
79         """
80         Returns the list of keys in this enumerate.
81         """
82         list = self._dict_keynumbers.keys()
83         list.sort()
84         return list
85
86     ## Returns the list of values specified to initiate this enumerate.
87     def listvalues(self):
88         """
89         Returns the list of values specified to initiate this enumerate.
90         """
91         list = self._dict_keynumbers.values()
92         list.sort()
93         return list
94
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):
98         """
99         Returns the symbolic key string associated to the specified identifier
100         value.
101         @param value : an integer value whose associated key string is requested.
102         """
103         if not self.isValid(value):
104             return None
105         # _MEM_ We assume here that the keys and associated values are in the
106         # same order in their list.
107         return self._dict_keynumbers.keys()[self._dict_keynumbers.values().index(value)]
108
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:
112         #        return key
113
114 #
115 # ==============================================================================
116 # Basic use cases and unit test functions
117 # ==============================================================================
118 #
119
120 def TEST_simple():
121     TYPES_LIST = Enumerate([
122         'SEP',
123         'OTHER'
124     ])
125     print TYPES_LIST.listvalues()
126     return True
127
128 def TEST_createFromList():
129     codes = Enumerate([
130         'KERNEL', # This should take the value 0
131         'GUI', # This should take the value 1
132         'GEOM', # ...
133         'MED',
134         'SMESH'])
135
136     print codes.KERNEL
137     print codes.GEOM
138     if (codes.KERNEL == 0 and codes.GEOM == 2):
139         return True
140     else:
141         return False
142
143 def TEST_createFromString():
144     aList = "KERNEL GUI GEOM MED"
145
146     codes = Enumerate(aList.split())
147
148     print codes.KERNEL
149     print codes.GEOM
150     if (codes.KERNEL == 0 and codes.GEOM == 2):
151         return True
152     else:
153         return False
154
155 def TEST_contains():
156     codes = Enumerate([
157         'KERNEL', # This should take the value 0
158         'GUI', # This should take the value 1
159         'GEOM', # ...
160         'MED',
161         'SMESH'])
162
163     print "VISU in enumerate?", codes.contains("VISU")
164     if (not codes.contains("VISU")):
165         return True
166     else:
167         return False
168
169 def TEST_isValid():
170     codes = Enumerate([
171         'KERNEL', # This should take the value 0
172         'GUI', # This should take the value 1
173         'GEOM', # ...
174         'MED',
175         'SMESH'])
176
177     if (not codes.isValid(23)):
178         return True
179     else:
180         return False
181
182 def TEST_offset():
183     codes = Enumerate([
184         'KERNEL', # This should take the value 0
185         'GUI', # This should take the value 1
186         'GEOM', # ...
187         'MED',
188         'SMESH'], offset=20)
189
190     print codes.KERNEL
191     print codes.GEOM
192     if (codes.KERNEL == 20 and codes.GEOM == 22):
193         return True
194     else:
195         return False
196
197 def TEST_listvalues():
198     codes = Enumerate([
199         'KERNEL', # This should take the value 0
200         'GUI', # This should take the value 1
201         'GEOM', # ...
202         'MED',
203         'SMESH'], offset=20)
204
205     print codes.listvalues()
206     if codes.listvalues() != [20, 21, 22, 23, 24]:
207         return False
208     return True
209
210 def TEST_keyOf():
211     codes = Enumerate([
212         'KERNEL', # This should take the value 0
213         'GUI', # This should take the value 1
214         'GEOM', # ...
215         'MED',
216         'SMESH'])
217
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'):
223             return False
224     return True
225
226 if __name__ == "__main__":
227     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")