Salome HOME
Use isinstance() instead of type() and remove useless list(....keys())
[modules/kernel.git] / src / KERNEL_PY / kernel / enumerate.py
index dc445740ad9760b69d0d6b0dc8ba6051591c59a6..ad04448afe98395c7bca4dc6b855c9570af58bd5 100644 (file)
@@ -1,10 +1,10 @@
 # -*- coding: iso-8859-1 -*-
-# Copyright (C) 2010-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+# Copyright (C) 2010-2016  CEA/DEN, EDF R&D, OPEN CASCADE
 #
 # This library is free software; you can redistribute it and/or
 # modify it under the terms of the GNU Lesser General Public
 # License as published by the Free Software Foundation; either
-# version 2.1 of the License.
+# version 2.1 of the License, or (at your option) any later version.
 #
 # This library is distributed in the hope that it will be useful,
 # but WITHOUT ANY WARRANTY; without even the implied warranty of
 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 #
 
+## \defgroup enumerate enumerate
+#  \{ 
+#  \details Emulates a C-like enum for python
+#  \}
+
 __author__ = "gboulant"
 __date__ = "$1 avr. 2010 09:08:02$"
 
+## This class emulates a C-like enum for python. It is initialized with a list
+#  of strings to be used as the enum symbolic keys. The enum values are automatically
+#  generated as sequencing integer starting at the specified offset value.
+#  \ingroup enumerate
 class Enumerate(object):
     """
     This class emulates a C-like enum for python. It is initialized with a list
     of strings to be used as the enum symbolic keys. The enum values are automatically
     generated as sequencing integer starting at the specified offset value.
     """
+    
+    ## Canonical constructor.
+    #  \param keys a list of string to be used as the enum symbolic keys. The enum values
+    #  are automatically generated as a sequence of integers starting at the specified
+    #  offset value.
     def __init__(self, keys, offset=0):
         """
         Canonical constructor
@@ -39,38 +53,44 @@ class Enumerate(object):
             value = offset + number
             setattr(self, key, value)
             self._dict_keynumbers[key] = value
+        self._dict_numberkeys = {v: k for k, v in self._dict_keynumbers.items()}
 
+    ## Return true if this enumerate contains the specified key string
+    #  \param key a key string to test
     def contains(self, key):
         """
         Return true if this enumerate contains the specified key string
         @key a key string to test
         """
-        return (key in self._dict_keynumbers.keys())
+        return key in self._dict_keynumbers
 
+    ## Returns true if the specified integer value is defined as an identifier
+    #  in this enumarate.
+    #  \param value a value to test
     def isValid(self, value):
         """
         Returns true if the specified integer value is defined as an identifier
         in this enumarate.
         @value a value to test
         """
-        return (value in self._dict_keynumbers.values())
+        return value in self._dict_numberkeys
 
+    ## Returns the list of keys in this enumerate.
     def listkeys(self):
         """
         Returns the list of keys in this enumerate.
         """
-        list = self._dict_keynumbers.keys()
-        list.sort()
-        return list
+        return sorted(self._dict_keynumbers)
 
+    ## Returns the list of values specified to initiate this enumerate.
     def listvalues(self):
         """
         Returns the list of values specified to initiate this enumerate.
         """
-        list = self._dict_keynumbers.values()
-        list.sort()
-        return list
+        return sorted(self._dict_numberkeys)
 
+    ## Returns the symbolic key string associated to the specified identifier value.
+    #  \param value an integer value whose associated key string is requested.
     def keyOf(self, value):
         """
         Returns the symbolic key string associated to the specified identifier
@@ -81,7 +101,7 @@ class Enumerate(object):
             return None
         # _MEM_ We assume here that the keys and associated values are in the
         # same order in their list.
-        return self._dict_keynumbers.keys()[self._dict_keynumbers.values().index(value)]
+        return self._dict_numberkeys[value]
 
         # If not, weshould use a longer implementation such that:
         #for key in self._dict_keynumbers.keys():
@@ -99,7 +119,7 @@ def TEST_simple():
         'SEP',
         'OTHER'
     ])
-    print TYPES_LIST.listvalues()
+    print(TYPES_LIST.listvalues())
     return True
 
 def TEST_createFromList():
@@ -110,8 +130,8 @@ def TEST_createFromList():
         'MED',
         'SMESH'])
 
-    print codes.KERNEL
-    print codes.GEOM
+    print(codes.KERNEL)
+    print(codes.GEOM)
     if (codes.KERNEL == 0 and codes.GEOM == 2):
         return True
     else:
@@ -122,8 +142,8 @@ def TEST_createFromString():
 
     codes = Enumerate(aList.split())
 
-    print codes.KERNEL
-    print codes.GEOM
+    print(codes.KERNEL)
+    print(codes.GEOM)
     if (codes.KERNEL == 0 and codes.GEOM == 2):
         return True
     else:
@@ -137,7 +157,7 @@ def TEST_contains():
         'MED',
         'SMESH'])
 
-    print "VISU in enumerate?", codes.contains("VISU")
+    print("VISU in enumerate?", codes.contains("VISU"))
     if (not codes.contains("VISU")):
         return True
     else:
@@ -164,8 +184,8 @@ def TEST_offset():
         'MED',
         'SMESH'], offset=20)
 
-    print codes.KERNEL
-    print codes.GEOM
+    print(codes.KERNEL)
+    print(codes.GEOM)
     if (codes.KERNEL == 20 and codes.GEOM == 22):
         return True
     else:
@@ -179,7 +199,7 @@ def TEST_listvalues():
         'MED',
         'SMESH'], offset=20)
 
-    print codes.listvalues()
+    print(codes.listvalues())
     if codes.listvalues() != [20, 21, 22, 23, 24]:
         return False
     return True
@@ -201,7 +221,7 @@ def TEST_keyOf():
     return True
 
 if __name__ == "__main__":
-    import unittester
+    from . import unittester
     unittester.run("enumerate", "TEST_simple")
     unittester.run("enumerate", "TEST_createFromList")
     unittester.run("enumerate", "TEST_createFromString")