constant.py :  » Game-2D-3D » PyOpenGL » PyOpenGL-3.0.1 » OpenGL » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Game 2D 3D » PyOpenGL 
PyOpenGL » PyOpenGL 3.0.1 » OpenGL » constant.py
"""Implementation of OpenGL constant objects"""
import sys

class Constant( object ):
    """OpenGL constant that displays itself as a name rather than a value
    
    The purpose of this class is to make debugging OpenGL code easier,
    as you recieve messages that say what value you passed in in a 
    human-readable form, rather than as a bald number that requires
    lookup and disambiguation in the header file.
    """
    def __new__( cls, name, value ):
        """Initialise the constant with the given name and value"""
        if isinstance( value, float ) and cls is not FloatConstant:
            return FloatConstant( name, value )
        elif isinstance( value, (int,long) ) and cls is not IntConstant:
            return IntConstant( name, value )
        elif isinstance( value, (str,unicode) ) and cls is not StringConstant:
            return StringConstant( name, str(value) )
        if isinstance( value, long ):
            if value > sys.maxint:
                value = - (value & sys.maxint)
        base = super(Constant,cls).__new__( cls, value )
        base.name = name 
        return base
    def __repr__( self ):
        """Return the name, rather than the bald value"""
        return self.name 

class NumericConstant( Constant ):
    """Base class for numeric-value constants"""
    def __str__( self ):
        """Return the value as a human-friendly string"""
        return '%s (%s)'%(self.name,super(Constant,self).__str__())
    def __getstate__(self):
        """Retrieve state for pickle and the like"""
        return self.name
    def __setstate__( self, state ):
        self.name = state
        
class IntConstant( NumericConstant, int ):
    """Integer constant"""
    __slots__ = ('name',)
class FloatConstant( NumericConstant, float ):
    """Float constant"""
    __slots__ = ('name',)

class StringConstant( Constant, str ):
    """String constants"""
    def __repr__( self ):
        """Return the value as a human-friendly string"""
        return '%s (%s)'%(self.name,super(Constant,self).__str__())
    def __getnewargs__( self ):
        """Produce the new arguments for recreating the instance"""
        return (self.name,) + super( Constant, self ).__getnewargs__()

if __name__ == "__main__":
    x = IntConstant( 'testint', 3 )
    y = FloatConstant( 'testfloat', 3.0 )
    z = StringConstant( 'teststr', 'some testing string' )
    
    import pickle
    for val in x,y,z:
        restored = pickle.loads( pickle.dumps( val ))
        assert restored == val, (str(restored),str(val))
        assert restored.name == val.name, (restored.name,val.name)
        
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.