Salome HOME
Updated copyright comment
[modules/kernel.git] / src / KERNEL_PY / kernel / enumerate.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2024  CEA, EDF, 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:
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         self._dict_numberkeys = {v: k for k, v in self._dict_keynumbers.items()}
57
58     ## Return true if this enumerate contains the specified key string
59     #  \param key a key string to test
60     def contains(self, key):
61         """
62         Return true if this enumerate contains the specified key string
63         @key a key string to test
64         """
65         return key in self._dict_keynumbers
66
67     ## Returns true if the specified integer value is defined as an identifier
68     #  in this enumerate.
69     #  \param value a value to test
70     def isValid(self, value):
71         """
72         Returns true if the specified integer value is defined as an identifier
73         in this enumerate.
74         @value a value to test
75         """
76         return value in self._dict_numberkeys
77
78     ## Returns the list of keys in this enumerate.
79     def listkeys(self):
80         """
81         Returns the list of keys in this enumerate.
82         """
83         return sorted(self._dict_keynumbers)
84
85     ## Returns the list of values specified to initiate this enumerate.
86     def listvalues(self):
87         """
88         Returns the list of values specified to initiate this enumerate.
89         """
90         return sorted(self._dict_numberkeys)
91
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):
95         """
96         Returns the symbolic key string associated to the specified identifier
97         value.
98         @param value : an integer value whose associated key string is requested.
99         """
100         if not self.isValid(value):
101             return None
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]
105
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:
109         #        return key
110
111 #
112 # ==============================================================================
113 # Basic use cases and unit test functions
114 # ==============================================================================
115 #
116
117 def TEST_simple():
118     TYPES_LIST = Enumerate([
119         'SEP',
120         'OTHER'
121     ])
122     print(TYPES_LIST.listvalues())
123     return True
124
125 def TEST_createFromList():
126     codes = Enumerate([
127         'KERNEL', # This should take the value 0
128         'GUI', # This should take the value 1
129         'GEOM', # ...
130         'MED',
131         'SMESH'])
132
133     print(codes.KERNEL)
134     print(codes.GEOM)
135     if (codes.KERNEL == 0 and codes.GEOM == 2):
136         return True
137     else:
138         return False
139
140 def TEST_createFromString():
141     aList = "KERNEL GUI GEOM MED"
142
143     codes = Enumerate(aList.split())
144
145     print(codes.KERNEL)
146     print(codes.GEOM)
147     if (codes.KERNEL == 0 and codes.GEOM == 2):
148         return True
149     else:
150         return False
151
152 def TEST_contains():
153     codes = Enumerate([
154         'KERNEL', # This should take the value 0
155         'GUI', # This should take the value 1
156         'GEOM', # ...
157         'MED',
158         'SMESH'])
159
160     print("VISU in enumerate?", codes.contains("VISU"))
161     if (not codes.contains("VISU")):
162         return True
163     else:
164         return False
165
166 def TEST_isValid():
167     codes = Enumerate([
168         'KERNEL', # This should take the value 0
169         'GUI', # This should take the value 1
170         'GEOM', # ...
171         'MED',
172         'SMESH'])
173
174     if (not codes.isValid(23)):
175         return True
176     else:
177         return False
178
179 def TEST_offset():
180     codes = Enumerate([
181         'KERNEL', # This should take the value 0
182         'GUI', # This should take the value 1
183         'GEOM', # ...
184         'MED',
185         'SMESH'], offset=20)
186
187     print(codes.KERNEL)
188     print(codes.GEOM)
189     if (codes.KERNEL == 20 and codes.GEOM == 22):
190         return True
191     else:
192         return False
193
194 def TEST_listvalues():
195     codes = Enumerate([
196         'KERNEL', # This should take the value 0
197         'GUI', # This should take the value 1
198         'GEOM', # ...
199         'MED',
200         'SMESH'], offset=20)
201
202     print(codes.listvalues())
203     if codes.listvalues() != [20, 21, 22, 23, 24]:
204         return False
205     return True
206
207 def TEST_keyOf():
208     codes = Enumerate([
209         'KERNEL', # This should take the value 0
210         'GUI', # This should take the value 1
211         'GEOM', # ...
212         'MED',
213         'SMESH'])
214
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'):
220             return False
221     return True
222
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")