Salome HOME
CMake: set the flag Boost_NO_BOOST_CMAKE to avoid that the *standard* FindBoost.cmake
[modules/kernel.git] / src / KERNEL_PY / kernel / enumerate.py
1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2010-2013  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.
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 __author__ = "gboulant"
22 __date__ = "$1 avr. 2010 09:08:02$"
23
24 class Enumerate(object):
25     """
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.
29     """
30     def __init__(self, keys, offset=0):
31         """
32         Canonical constructor
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
35         offset value.
36         """
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
42
43     def contains(self, key):
44         """
45         Return true if this enumerate contains the specified key string
46         @key a key string to test
47         """
48         return (key in self._dict_keynumbers.keys())
49
50     def isValid(self, value):
51         """
52         Returns true if the specified integer value is defined as an identifier
53         in this enumarate.
54         @value a value to test
55         """
56         return (value in self._dict_keynumbers.values())
57
58     def listkeys(self):
59         """
60         Returns the list of keys in this enumerate.
61         """
62         list = self._dict_keynumbers.keys()
63         list.sort()
64         return list
65
66     def listvalues(self):
67         """
68         Returns the list of values specified to initiate this enumerate.
69         """
70         list = self._dict_keynumbers.values()
71         list.sort()
72         return list
73
74     def keyOf(self, value):
75         """
76         Returns the symbolic key string associated to the specified identifier
77         value.
78         @param value : an integer value whose associated key string is requested.
79         """
80         if not self.isValid(value):
81             return None
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)]
85
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:
89         #        return key
90
91 #
92 # ==============================================================================
93 # Basic use cases and unit test functions
94 # ==============================================================================
95 #
96
97 def TEST_simple():
98     TYPES_LIST = Enumerate([
99         'SEP',
100         'OTHER'
101     ])
102     print TYPES_LIST.listvalues()
103     return True
104
105 def TEST_createFromList():
106     codes = Enumerate([
107         'KERNEL', # This should take the value 0
108         'GUI', # This should take the value 1
109         'GEOM', # ...
110         'MED',
111         'SMESH'])
112
113     print codes.KERNEL
114     print codes.GEOM
115     if (codes.KERNEL == 0 and codes.GEOM == 2):
116         return True
117     else:
118         return False
119
120 def TEST_createFromString():
121     aList = "KERNEL GUI GEOM MED"
122
123     codes = Enumerate(aList.split())
124
125     print codes.KERNEL
126     print codes.GEOM
127     if (codes.KERNEL == 0 and codes.GEOM == 2):
128         return True
129     else:
130         return False
131
132 def TEST_contains():
133     codes = Enumerate([
134         'KERNEL', # This should take the value 0
135         'GUI', # This should take the value 1
136         'GEOM', # ...
137         'MED',
138         'SMESH'])
139
140     print "VISU in enumerate?", codes.contains("VISU")
141     if (not codes.contains("VISU")):
142         return True
143     else:
144         return False
145
146 def TEST_isValid():
147     codes = Enumerate([
148         'KERNEL', # This should take the value 0
149         'GUI', # This should take the value 1
150         'GEOM', # ...
151         'MED',
152         'SMESH'])
153
154     if (not codes.isValid(23)):
155         return True
156     else:
157         return False
158
159 def TEST_offset():
160     codes = Enumerate([
161         'KERNEL', # This should take the value 0
162         'GUI', # This should take the value 1
163         'GEOM', # ...
164         'MED',
165         'SMESH'], offset=20)
166
167     print codes.KERNEL
168     print codes.GEOM
169     if (codes.KERNEL == 20 and codes.GEOM == 22):
170         return True
171     else:
172         return False
173
174 def TEST_listvalues():
175     codes = Enumerate([
176         'KERNEL', # This should take the value 0
177         'GUI', # This should take the value 1
178         'GEOM', # ...
179         'MED',
180         'SMESH'], offset=20)
181
182     print codes.listvalues()
183     if codes.listvalues() != [20, 21, 22, 23, 24]:
184         return False
185     return True
186
187 def TEST_keyOf():
188     codes = Enumerate([
189         'KERNEL', # This should take the value 0
190         'GUI', # This should take the value 1
191         'GEOM', # ...
192         'MED',
193         'SMESH'])
194
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'):
200             return False
201     return True
202
203 if __name__ == "__main__":
204     import unittester
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")