1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008-2023 EDF R&D
5 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
7 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
8 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
24 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
27 __author__ = "gboulant"
28 __date__ = "$1 avr. 2010 09:08:02$"
30 class Enumerate(object):
32 This class emulates a C-like enum for python.
34 def __init__(self, keys, offset=0):
37 @keys a list of keys string to be used as the enum symbols
39 self._dict_keynumbers = {}
40 for number, key in enumerate(keys):
42 setattr(self, key, value)
43 self._dict_keynumbers[key] = value
46 def contains(self, key):
48 Return true if this enumerate contains the specified key
51 return (key in self._dict_keynumbers.keys())
53 def isValid(self, value):
54 return (value in self._dict_keynumbers.values())
57 return sorted(self._dict_keynumbers.keys())
60 return sorted(self._dict_keynumbers.values())
63 # ==============================================================================
64 # Basic use cases and unit test functions
65 # ==============================================================================
69 TYPES_LIST=Enumerate([
73 print(TYPES_LIST.listvalues())
76 def TEST_createFromList():
78 'KERNEL', # This should take the value 0
79 'GUI', # This should take the value 1
86 if (codes.KERNEL == 0 and codes.GEOM == 2):
91 def TEST_createFromString():
92 aList = "KERNEL GUI GEOM MED"
94 codes = Enumerate(aList.split())
98 if (codes.KERNEL == 0 and codes.GEOM == 2):
105 'KERNEL', # This should take the value 0
106 'GUI', # This should take the value 1
111 print("VISU in enumerate?", codes.contains("VISU"))
112 if ( not codes.contains("VISU") ):
119 'KERNEL', # This should take the value 0
120 'GUI', # This should take the value 1
125 if ( not codes.isValid(23) ):
132 'KERNEL', # This should take the value 0
133 'GUI', # This should take the value 1
140 if (codes.KERNEL == 20 and codes.GEOM == 22):
145 def TEST_listvalues():
147 'KERNEL', # This should take the value 0
148 'GUI', # This should take the value 1
153 print(codes.listvalues())
154 if codes.listvalues() != [20,21,22,23,24]:
159 if __name__ == "__main__":
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")