Salome HOME
Add functions to know that SALOME module version is development one
[modules/kernel.git] / src / KERNEL_PY / kernel / enumerate.py
1 # -*- coding: iso-8859-1 -*-
2 #  Copyright (C) 2010  EDF R&D
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 # -* Makefile *- 
21 #
22
23 __author__ = "gboulant"
24 __date__ = "$1 avr. 2010 09:08:02$"
25
26 class Enumerate(object):
27     """
28     This class emulates a C-like enum for python. It is initialized with a list
29     of strings to be used as the enum symbolic keys. The enum values are automatically
30     generated as sequencing integer starting at the specified offset value.
31     """
32     def __init__(self, keys, offset=0):
33         """
34         Canonical constructor
35         @keys a list of string to be used as the enum symbolic keys. The enum values
36         are automatically generated as a sequence of integers starting at the specified
37         offset value.
38         """
39         self._dict_keynumbers = {}
40         for number, key in enumerate(keys):
41             value = offset + number
42             setattr(self, key, value)
43             self._dict_keynumbers[key] = value
44
45     def contains(self, key):
46         """
47         Return true if this enumerate contains the specified key string
48         @key a key string to test
49         """
50         return (key in self._dict_keynumbers.keys())
51
52     def isValid(self, value):
53         """
54         Returns true if the specified integer value is defined as an identifier
55         in this enumarate.
56         @value a value to test
57         """
58         return (value in self._dict_keynumbers.values())
59
60     def listkeys(self):
61         """
62         Returns the list of keys in this enumerate.
63         """
64         list = self._dict_keynumbers.keys()
65         list.sort()
66         return list
67
68     def listvalues(self):
69         """
70         Returns the list of values specified to initiate this enumerate.
71         """
72         list = self._dict_keynumbers.values()
73         list.sort()
74         return list
75
76     def keyOf(self, value):
77         """
78         Returns the symbolic key string associated to the specified identifier
79         value.
80         @param value : an integer value whose associated key string is requested.
81         """
82         if not self.isValid(value):
83             return None
84         # _MEM_ We assume here that the keys and associated values are in the
85         # same order in their list.
86         return self._dict_keynumbers.keys()[self._dict_keynumbers.values().index(value)]
87
88         # If not, weshould use a longer implementation such that:
89         #for key in self._dict_keynumbers.keys():
90         #    if self._dict_keynumbers[key] == value:
91         #        return key
92
93 #
94 # ==============================================================================
95 # Basic use cases and unit test functions
96 # ==============================================================================
97 #
98
99 def TEST_simple():
100     TYPES_LIST = Enumerate([
101         'SEP',
102         'OTHER'
103     ])
104     print TYPES_LIST.listvalues()
105     return True
106
107 def TEST_createFromList():
108     codes = Enumerate([
109         'KERNEL', # This should take the value 0
110         'GUI', # This should take the value 1
111         'GEOM', # ...
112         'MED',
113         'SMESH'])
114
115     print codes.KERNEL
116     print codes.GEOM
117     if (codes.KERNEL == 0 and codes.GEOM == 2):
118         return True
119     else:
120         return False
121
122 def TEST_createFromString():
123     aList = "KERNEL GUI GEOM MED"
124
125     codes = Enumerate(aList.split())
126
127     print codes.KERNEL
128     print codes.GEOM
129     if (codes.KERNEL == 0 and codes.GEOM == 2):
130         return True
131     else:
132         return False
133
134 def TEST_contains():
135     codes = Enumerate([
136         'KERNEL', # This should take the value 0
137         'GUI', # This should take the value 1
138         'GEOM', # ...
139         'MED',
140         'SMESH'])
141
142     print "VISU in enumerate?", codes.contains("VISU")
143     if (not codes.contains("VISU")):
144         return True
145     else:
146         return False
147
148 def TEST_isValid():
149     codes = Enumerate([
150         'KERNEL', # This should take the value 0
151         'GUI', # This should take the value 1
152         'GEOM', # ...
153         'MED',
154         'SMESH'])
155
156     if (not codes.isValid(23)):
157         return True
158     else:
159         return False
160
161 def TEST_offset():
162     codes = Enumerate([
163         'KERNEL', # This should take the value 0
164         'GUI', # This should take the value 1
165         'GEOM', # ...
166         'MED',
167         'SMESH'], offset=20)
168
169     print codes.KERNEL
170     print codes.GEOM
171     if (codes.KERNEL == 20 and codes.GEOM == 22):
172         return True
173     else:
174         return False
175
176 def TEST_listvalues():
177     codes = Enumerate([
178         'KERNEL', # This should take the value 0
179         'GUI', # This should take the value 1
180         'GEOM', # ...
181         'MED',
182         'SMESH'], offset=20)
183
184     print codes.listvalues()
185     if codes.listvalues() != [20, 21, 22, 23, 24]:
186         return False
187     return True
188
189 def TEST_keyOf():
190     codes = Enumerate([
191         'KERNEL', # This should take the value 0
192         'GUI', # This should take the value 1
193         'GEOM', # ...
194         'MED',
195         'SMESH'])
196
197     if ( codes.keyOf(codes.KERNEL) != 'KERNEL' or
198          codes.keyOf(codes.GUI) != 'GUI' or
199          codes.keyOf(codes.GEOM) != 'GEOM' or
200          codes.keyOf(codes.MED) != 'MED' or
201          codes.keyOf(codes.SMESH) != 'SMESH'):
202             return False
203     return True
204
205 if __name__ == "__main__":
206     import unittester
207     unittester.run("enumerate", "TEST_simple")
208     unittester.run("enumerate", "TEST_createFromList")
209     unittester.run("enumerate", "TEST_createFromString")
210     unittester.run("enumerate", "TEST_contains")
211     unittester.run("enumerate", "TEST_isValid")
212     unittester.run("enumerate", "TEST_offset")
213     unittester.run("enumerate", "TEST_listvalues")
214     unittester.run("enumerate", "TEST_keyOf")