home *** CD-ROM | disk | FTP | other *** search
/ Hackers Magazine 57 / CdHackersMagazineNr57.iso / Software / Multimedia / k3d-setup-0.7.11.0.exe / lib / site-packages / OpenGL / constant.py < prev    next >
Encoding:
Python Source  |  2008-12-07  |  2.3 KB  |  68 lines

  1. """Implementation of OpenGL constant objects"""
  2. import sys
  3.  
  4. class Constant( object ):
  5.     """OpenGL constant that displays itself as a name rather than a value
  6.     
  7.     The purpose of this class is to make debugging OpenGL code easier,
  8.     as you recieve messages that say what value you passed in in a 
  9.     human-readable form, rather than as a bald number that requires
  10.     lookup and disambiguation in the header file.
  11.     """
  12.     def __new__( cls, name, value ):
  13.         """Initialise the constant with the given name and value"""
  14.         if isinstance( value, float ) and cls is not FloatConstant:
  15.             return FloatConstant( name, value )
  16.         elif isinstance( value, (int,long) ) and cls is not IntConstant:
  17.             return IntConstant( name, value )
  18.         elif isinstance( value, (str,unicode) ) and cls is not StringConstant:
  19.             return StringConstant( name, str(value) )
  20.         if isinstance( value, long ):
  21.             if value > sys.maxint:
  22.                 value = - (value & sys.maxint)
  23.         base = super(Constant,cls).__new__( cls, value )
  24.         base.name = name 
  25.         return base
  26.     def __repr__( self ):
  27.         """Return the name, rather than the bald value"""
  28.         return self.name 
  29.  
  30. class NumericConstant( Constant ):
  31.     """Base class for numeric-value constants"""
  32.     def __str__( self ):
  33.         """Return the value as a human-friendly string"""
  34.         return '%s (%s)'%(self.name,super(Constant,self).__str__())
  35.     def __getstate__(self):
  36.         """Retrieve state for pickle and the like"""
  37.         return self.name
  38.     def __setstate__( self, state ):
  39.         self.name = state
  40.         
  41. class IntConstant( NumericConstant, int ):
  42.     """Integer constant"""
  43.     __slots__ = ('name',)
  44. class FloatConstant( NumericConstant, float ):
  45.     """Float constant"""
  46.     __slots__ = ('name',)
  47.  
  48. class StringConstant( Constant, str ):
  49.     """String constants"""
  50.     def __repr__( self ):
  51.         """Return the value as a human-friendly string"""
  52.         return '%s (%s)'%(self.name,super(Constant,self).__str__())
  53.     def __getnewargs__( self ):
  54.         """Produce the new arguments for recreating the instance"""
  55.         return (self.name,) + super( Constant, self ).__getnewargs__()
  56.  
  57. if __name__ == "__main__":
  58.     x = IntConstant( 'testint', 3 )
  59.     y = FloatConstant( 'testfloat', 3.0 )
  60.     z = StringConstant( 'teststr', 'some testing string' )
  61.     
  62.     import pickle
  63.     for val in x,y,z:
  64.         restored = pickle.loads( pickle.dumps( val ))
  65.         assert restored == val, (str(restored),str(val))
  66.         assert restored.name == val.name, (restored.name,val.name)
  67.         
  68.