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