psddl/src/Enum.py

Go to the documentation of this file.
00001 #--------------------------------------------------------------------------
00002 # File and Version Information:
00003 #  $Id: Enum.py 6852 2013-09-25 03:33:35Z salnikov@SLAC.STANFORD.EDU $
00004 #
00005 # Description:
00006 #  Module Enum...
00007 #
00008 #------------------------------------------------------------------------
00009 
00010 """Class representing enum definition.
00011 
00012 This software was developed for the SIT project.  If you use all or 
00013 part of it, please give an appropriate acknowledgment.
00014 
00015 @see RelatedModule
00016 
00017 @version $Id: Enum.py 6852 2013-09-25 03:33:35Z salnikov@SLAC.STANFORD.EDU $
00018 
00019 @author Andrei Salnikov
00020 """
00021 
00022 
00023 #------------------------------
00024 #  Module's version from CVS --
00025 #------------------------------
00026 __version__ = "$Revision: 6852 $"
00027 # $Source$
00028 
00029 #--------------------------------
00030 #  Imports of standard modules --
00031 #--------------------------------
00032 import sys
00033 
00034 #---------------------------------
00035 #  Imports of base class module --
00036 #---------------------------------
00037 from psddl.Namespace import Namespace
00038 from psddl.Type import Type
00039 
00040 #-----------------------------
00041 # Imports for other modules --
00042 #-----------------------------
00043 
00044 #----------------------------------
00045 # Local non-exported definitions --
00046 #----------------------------------
00047 
00048 #------------------------
00049 # Exported definitions --
00050 #------------------------
00051 
00052 #---------------------
00053 #  Class definition --
00054 #---------------------
00055 class Enum ( Namespace ) :
00056 
00057     #----------------
00058     #  Constructor --
00059     #----------------
00060     def __init__ ( self, name, parent, **kw ) :
00061 
00062         Namespace.__init__(self, name, parent)
00063 
00064         self.included = kw.get('included')
00065         self.comment = kw.get('comment', '')
00066         self.base = self.lookup(kw.get('base', 'int32_t'), Type)
00067         
00068         self.basic = True
00069         self.value_type = True
00070 
00071     def __str__(self):
00072         res = "<Enum(" + self.name
00073         for c in self.constants() :
00074             res += ", "
00075             res += c.name
00076             if c.value:
00077                 res += " = "
00078                 res += str(c.value)
00079         res += ")>"
00080         return res
00081 
00082     def __repr__(self):
00083         res = "<Enum(" + self.name + ")>"
00084         return res
00085 
00086     def unique_constants(self):
00087         '''
00088         This method returns the list of enum constants which have unique values.
00089         '''
00090         
00091         values = set()
00092         res = []
00093         for const in self.constants():
00094             if not const.value: 
00095                 res.append(const)
00096             elif const.value not in values:
00097                 values.add(const.value)
00098                 res.append(const)
00099 
00100         return res
00101 
00102 #
00103 #  In case someone decides to run this module
00104 #
00105 if __name__ == "__main__" :
00106 
00107     # In principle we can try to run test suite for this module,
00108     # have to think about it later. Right now just abort.
00109     sys.exit ( "Module is not supposed to be run as main module" )

Generated on 19 Dec 2016 for PSDMSoftware by  doxygen 1.4.7