Back to index

salome-kernel  6.5.0
enumerate.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 # Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00003 #
00004 # This library is free software; you can redistribute it and/or
00005 # modify it under the terms of the GNU Lesser General Public
00006 # License as published by the Free Software Foundation; either
00007 # version 2.1 of the License.
00008 #
00009 # This library is distributed in the hope that it will be useful,
00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 # Lesser General Public License for more details.
00013 #
00014 # You should have received a copy of the GNU Lesser General Public
00015 # License along with this library; if not, write to the Free Software
00016 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00017 #
00018 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00019 #
00020 
00021 __author__ = "gboulant"
00022 __date__ = "$1 avr. 2010 09:08:02$"
00023 
00024 class Enumerate(object):
00025     """
00026     This class emulates a C-like enum for python. It is initialized with a list
00027     of strings to be used as the enum symbolic keys. The enum values are automatically
00028     generated as sequencing integer starting at the specified offset value.
00029     """
00030     def __init__(self, keys, offset=0):
00031         """
00032         Canonical constructor
00033         @keys a list of string to be used as the enum symbolic keys. The enum values
00034         are automatically generated as a sequence of integers starting at the specified
00035         offset value.
00036         """
00037         self._dict_keynumbers = {}
00038         for number, key in enumerate(keys):
00039             value = offset + number
00040             setattr(self, key, value)
00041             self._dict_keynumbers[key] = value
00042 
00043     def contains(self, key):
00044         """
00045         Return true if this enumerate contains the specified key string
00046         @key a key string to test
00047         """
00048         return (key in self._dict_keynumbers.keys())
00049 
00050     def isValid(self, value):
00051         """
00052         Returns true if the specified integer value is defined as an identifier
00053         in this enumarate.
00054         @value a value to test
00055         """
00056         return (value in self._dict_keynumbers.values())
00057 
00058     def listkeys(self):
00059         """
00060         Returns the list of keys in this enumerate.
00061         """
00062         list = self._dict_keynumbers.keys()
00063         list.sort()
00064         return list
00065 
00066     def listvalues(self):
00067         """
00068         Returns the list of values specified to initiate this enumerate.
00069         """
00070         list = self._dict_keynumbers.values()
00071         list.sort()
00072         return list
00073 
00074     def keyOf(self, value):
00075         """
00076         Returns the symbolic key string associated to the specified identifier
00077         value.
00078         @param value : an integer value whose associated key string is requested.
00079         """
00080         if not self.isValid(value):
00081             return None
00082         # _MEM_ We assume here that the keys and associated values are in the
00083         # same order in their list.
00084         return self._dict_keynumbers.keys()[self._dict_keynumbers.values().index(value)]
00085 
00086         # If not, weshould use a longer implementation such that:
00087         #for key in self._dict_keynumbers.keys():
00088         #    if self._dict_keynumbers[key] == value:
00089         #        return key
00090 
00091 #
00092 # ==============================================================================
00093 # Basic use cases and unit test functions
00094 # ==============================================================================
00095 #
00096 
00097 def TEST_simple():
00098     TYPES_LIST = Enumerate([
00099         'SEP',
00100         'OTHER'
00101     ])
00102     print TYPES_LIST.listvalues()
00103     return True
00104 
00105 def TEST_createFromList():
00106     codes = Enumerate([
00107         'KERNEL', # This should take the value 0
00108         'GUI', # This should take the value 1
00109         'GEOM', # ...
00110         'MED',
00111         'SMESH'])
00112 
00113     print codes.KERNEL
00114     print codes.GEOM
00115     if (codes.KERNEL == 0 and codes.GEOM == 2):
00116         return True
00117     else:
00118         return False
00119 
00120 def TEST_createFromString():
00121     aList = "KERNEL GUI GEOM MED"
00122 
00123     codes = Enumerate(aList.split())
00124 
00125     print codes.KERNEL
00126     print codes.GEOM
00127     if (codes.KERNEL == 0 and codes.GEOM == 2):
00128         return True
00129     else:
00130         return False
00131 
00132 def TEST_contains():
00133     codes = Enumerate([
00134         'KERNEL', # This should take the value 0
00135         'GUI', # This should take the value 1
00136         'GEOM', # ...
00137         'MED',
00138         'SMESH'])
00139 
00140     print "VISU in enumerate?", codes.contains("VISU")
00141     if (not codes.contains("VISU")):
00142         return True
00143     else:
00144         return False
00145 
00146 def TEST_isValid():
00147     codes = Enumerate([
00148         'KERNEL', # This should take the value 0
00149         'GUI', # This should take the value 1
00150         'GEOM', # ...
00151         'MED',
00152         'SMESH'])
00153 
00154     if (not codes.isValid(23)):
00155         return True
00156     else:
00157         return False
00158 
00159 def TEST_offset():
00160     codes = Enumerate([
00161         'KERNEL', # This should take the value 0
00162         'GUI', # This should take the value 1
00163         'GEOM', # ...
00164         'MED',
00165         'SMESH'], offset=20)
00166 
00167     print codes.KERNEL
00168     print codes.GEOM
00169     if (codes.KERNEL == 20 and codes.GEOM == 22):
00170         return True
00171     else:
00172         return False
00173 
00174 def TEST_listvalues():
00175     codes = Enumerate([
00176         'KERNEL', # This should take the value 0
00177         'GUI', # This should take the value 1
00178         'GEOM', # ...
00179         'MED',
00180         'SMESH'], offset=20)
00181 
00182     print codes.listvalues()
00183     if codes.listvalues() != [20, 21, 22, 23, 24]:
00184         return False
00185     return True
00186 
00187 def TEST_keyOf():
00188     codes = Enumerate([
00189         'KERNEL', # This should take the value 0
00190         'GUI', # This should take the value 1
00191         'GEOM', # ...
00192         'MED',
00193         'SMESH'])
00194 
00195     if ( codes.keyOf(codes.KERNEL) != 'KERNEL' or
00196          codes.keyOf(codes.GUI) != 'GUI' or
00197          codes.keyOf(codes.GEOM) != 'GEOM' or
00198          codes.keyOf(codes.MED) != 'MED' or
00199          codes.keyOf(codes.SMESH) != 'SMESH'):
00200             return False
00201     return True
00202 
00203 if __name__ == "__main__":
00204     import unittester
00205     unittester.run("enumerate", "TEST_simple")
00206     unittester.run("enumerate", "TEST_createFromList")
00207     unittester.run("enumerate", "TEST_createFromString")
00208     unittester.run("enumerate", "TEST_contains")
00209     unittester.run("enumerate", "TEST_isValid")
00210     unittester.run("enumerate", "TEST_offset")
00211     unittester.run("enumerate", "TEST_listvalues")
00212     unittester.run("enumerate", "TEST_keyOf")