1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
4 # Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
5 # CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
21 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
24 __author__ = "gboulant"
25 __date__ = "$1 avr. 2010 09:08:02$"
27 class Enumerate(object):
29 This class emulates a C-like enum for python.
31 def __init__(self, keys, offset=0):
34 @keys a list of keys string to be used as the enum symbols
36 self._dict_keynumbers = {}
37 for number, key in enumerate(keys):
39 setattr(self, key, value)
40 self._dict_keynumbers[key] = value
43 def contains(self, key):
45 Return true if this enumerate contains the specified key
48 return (key in self._dict_keynumbers.keys())
50 def isValid(self, value):
51 return (value in self._dict_keynumbers.values())
54 return sorted(self._dict_keynumbers.keys())
57 return sorted(self._dict_keynumbers.values())
60 # ==============================================================================
61 # Basic use cases and unit test functions
62 # ==============================================================================
66 TYPES_LIST=Enumerate([
70 print(TYPES_LIST.listvalues())
73 def TEST_createFromList():
75 'KERNEL', # This should take the value 0
76 'GUI', # This should take the value 1
83 if (codes.KERNEL == 0 and codes.GEOM == 2):
88 def TEST_createFromString():
89 aList = "KERNEL GUI GEOM MED"
91 codes = Enumerate(aList.split())
95 if (codes.KERNEL == 0 and codes.GEOM == 2):
102 'KERNEL', # This should take the value 0
103 'GUI', # This should take the value 1
108 print("VISU in enumerate?", codes.contains("VISU"))
109 if ( not codes.contains("VISU") ):
116 'KERNEL', # This should take the value 0
117 'GUI', # This should take the value 1
122 if ( not codes.isValid(23) ):
129 'KERNEL', # This should take the value 0
130 'GUI', # This should take the value 1
137 if (codes.KERNEL == 20 and codes.GEOM == 22):
142 def TEST_listvalues():
144 'KERNEL', # This should take the value 0
145 'GUI', # This should take the value 1
150 print(codes.listvalues())
151 if codes.listvalues() != [20,21,22,23,24]:
156 if __name__ == "__main__":
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")