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 list = self._dict_keynumbers.keys()
59 list = self._dict_keynumbers.values()
64 # ==============================================================================
65 # Basic use cases and unit test functions
66 # ==============================================================================
70 TYPES_LIST=Enumerate([
74 print TYPES_LIST.listvalues()
77 def TEST_createFromList():
79 'KERNEL', # This should take the value 0
80 'GUI', # This should take the value 1
87 if (codes.KERNEL == 0 and codes.GEOM == 2):
92 def TEST_createFromString():
93 aList = "KERNEL GUI GEOM MED"
95 codes = Enumerate(aList.split())
99 if (codes.KERNEL == 0 and codes.GEOM == 2):
106 'KERNEL', # This should take the value 0
107 'GUI', # This should take the value 1
112 print "VISU in enumerate?", codes.contains("VISU")
113 if ( not codes.contains("VISU") ):
120 'KERNEL', # This should take the value 0
121 'GUI', # This should take the value 1
126 if ( not codes.isValid(23) ):
133 'KERNEL', # This should take the value 0
134 'GUI', # This should take the value 1
141 if (codes.KERNEL == 20 and codes.GEOM == 22):
146 def TEST_listvalues():
148 'KERNEL', # This should take the value 0
149 'GUI', # This should take the value 1
154 print codes.listvalues()
155 if codes.listvalues() != [20,21,22,23,24]:
160 if __name__ == "__main__":
162 unittester.run("enumerate","TEST_simple")
163 unittester.run("enumerate","TEST_createFromList")
164 unittester.run("enumerate","TEST_createFromString")
165 unittester.run("enumerate","TEST_contains")
166 unittester.run("enumerate","TEST_isValid")
167 unittester.run("enumerate","TEST_offset")
168 unittester.run("enumerate","TEST_listvalues")