Salome HOME
Updating copyright date information
[modules/adao.git] / src / daSalome / daGUI / daUtils / enumerate.py
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2008-2024 EDF R&D
4 #
5 # Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
6 #
7 # Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
8 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
9 #
10 # This library is free software; you can redistribute it and/or
11 # modify it under the terms of the GNU Lesser General Public
12 # License as published by the Free Software Foundation; either
13 # version 2.1 of the License.
14 #
15 # This library is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 # Lesser General Public License for more details.
19 #
20 # You should have received a copy of the GNU Lesser General Public
21 # License along with this library; if not, write to the Free Software
22 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23 #
24 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
25 #
26
27 __author__ = "gboulant"
28 __date__ = "$1 avr. 2010 09:08:02$"
29
30 class Enumerate(object):
31     """
32     This class emulates a C-like enum for python.
33     """
34     def __init__(self, keys, offset=0):
35         """
36         Canonical constructor
37         @keys a list of keys string to be used as the enum symbols
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
46     def contains(self, key):
47         """
48         Return true if this enumerate contains the specified key
49         @key a key to test
50         """
51         return (key in self._dict_keynumbers.keys())
52
53     def isValid(self, value):
54         return (value in self._dict_keynumbers.values())
55
56     def listkeys(self):
57         return sorted(self._dict_keynumbers.keys())
58
59     def listvalues(self):
60         return sorted(self._dict_keynumbers.values())
61
62 #
63 # ==============================================================================
64 # Basic use cases and unit test functions
65 # ==============================================================================
66 #
67
68 def TEST_simple():
69     TYPES_LIST=Enumerate([
70         'SEP',
71         'OTHER'
72     ])
73     print(TYPES_LIST.listvalues())
74     return True
75
76 def TEST_createFromList():
77     codes = Enumerate([
78         'KERNEL', # This should take the value 0
79         'GUI', # This should take the value 1
80         'GEOM', # ...
81         'MED',
82         'SMESH'])
83
84     print(codes.KERNEL)
85     print(codes.GEOM)
86     if (codes.KERNEL == 0 and codes.GEOM == 2):
87         return True
88     else:
89         return False
90
91 def TEST_createFromString():
92     aList = "KERNEL GUI GEOM MED"
93
94     codes = Enumerate(aList.split())
95
96     print(codes.KERNEL)
97     print(codes.GEOM)
98     if (codes.KERNEL == 0 and codes.GEOM == 2):
99         return True
100     else:
101         return False
102
103 def TEST_contains():
104     codes = Enumerate([
105         'KERNEL', # This should take the value 0
106         'GUI', # This should take the value 1
107         'GEOM', # ...
108         'MED',
109         'SMESH'])
110
111     print("VISU in enumerate?", codes.contains("VISU"))
112     if ( not codes.contains("VISU") ):
113         return True
114     else:
115         return False
116
117 def TEST_isValid():
118     codes = Enumerate([
119         'KERNEL', # This should take the value 0
120         'GUI', # This should take the value 1
121         'GEOM', # ...
122         'MED',
123         'SMESH'])
124
125     if ( not codes.isValid(23) ):
126         return True
127     else:
128         return False
129
130 def TEST_offset():
131     codes = Enumerate([
132         'KERNEL', # This should take the value 0
133         'GUI', # This should take the value 1
134         'GEOM', # ...
135         'MED',
136         'SMESH'], offset=20)
137
138     print(codes.KERNEL)
139     print(codes.GEOM)
140     if (codes.KERNEL == 20 and codes.GEOM == 22):
141         return True
142     else:
143         return False
144
145 def TEST_listvalues():
146     codes = Enumerate([
147         'KERNEL', # This should take the value 0
148         'GUI', # This should take the value 1
149         'GEOM', # ...
150         'MED',
151         'SMESH'], offset=20)
152
153     print(codes.listvalues())
154     if codes.listvalues() != [20,21,22,23,24]:
155         return False
156     return True
157
158
159 if __name__ == "__main__":
160     import unittester
161     unittester.run("enumerate","TEST_simple")
162     unittester.run("enumerate","TEST_createFromList")
163     unittester.run("enumerate","TEST_createFromString")
164     unittester.run("enumerate","TEST_contains")
165     unittester.run("enumerate","TEST_isValid")
166     unittester.run("enumerate","TEST_offset")
167     unittester.run("enumerate","TEST_listvalues")