commit-gnue
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[gnue] r9807 - in trunk/gnue-common/src: apps base


From: johannes
Subject: [gnue] r9807 - in trunk/gnue-common/src: apps base
Date: Thu, 15 Nov 2007 02:06:05 -0600 (CST)

Author: johannes
Date: 2007-11-15 02:06:04 -0600 (Thu, 15 Nov 2007)
New Revision: 9807

Added:
   trunk/gnue-common/src/base/checktype.py
Modified:
   trunk/gnue-common/src/apps/checktype.py
Log:
Moved checktype from apps into base


Modified: trunk/gnue-common/src/apps/checktype.py
===================================================================
--- trunk/gnue-common/src/apps/checktype.py     2007-11-14 15:18:39 UTC (rev 
9806)
+++ trunk/gnue-common/src/apps/checktype.py     2007-11-15 08:06:04 UTC (rev 
9807)
@@ -23,161 +23,8 @@
 
 """
 Support for checking the type of variables.
+
+This module is *DEPRECATED*. Please use gnue.common.base.checktype instead.
 """
 
-import sys
-import types
-
-from gnue.common.base import errors, i18n
-
-
-# =============================================================================
-# Exception definition
-# =============================================================================
-
-class TypeError(errors.SystemError):
-    """
-    Raised when L{checktype} detects a wrong type.
-
-    Do not raise this exception manually.
-    """
-    def __init__(self, variable, expected):
-
-        self.varname = '<?>'
-        for (k, v) in (sys._getframe(2)).f_locals.items():
-            if variable is v:
-                self.varname = k
-
-        self.expected = expected            # Expected type of variable
-
-        if isinstance(variable, types.InstanceType):
-            self.actual = variable.__class__
-        else:
-            self.actual = type(variable)     # Actual type of variable
-
-        self.value = variable               # Value of variable
-
-        message = u_('"%(varname)s" is expected to be of %(expected)s but is '
-                     'of %(actual)s and has value %(value)s') \
-                  % {'varname' : self.varname,
-                     'expected': self.__stringify(self.expected),
-                     'actual'  : self.__stringify(self.actual),
-                     'value'   : repr(self.value)}
-        errors.SystemError.__init__(self, message)
-
-
-    # -------------------------------------------------------------------------
-    # Get a nice string for a given type
-    # -------------------------------------------------------------------------
-
-    def __stringify(self, atype):
-
-        if isinstance(atype, list):
-            return ' / '.join([self.__stringify(t) for t in atype])
-
-        elif isinstance(atype, type):
-            return str(atype)
-
-        elif isinstance(atype, types.ClassType):
-            return '<class %s>' % str(atype)
-
-        else:
-            return '<type %s>' % str(atype)
-
-
-# -----------------------------------------------------------------------------
-# Check type of a variable
-# -----------------------------------------------------------------------------
-
-def checktype(variable, validtype):
-    """
-    Check a varaible (for example a parameter to a function) for a correct 
type.  
-    This function is available as builtin function.
-
-    @param variable: Variable to check.
-    @param validtype: Type, class, or a list of types and classes that are
-         valid.
-    @raise TypeError: The variable has a type not listed in the valid types.
-    """
-    if isinstance(validtype, list):
-        for t in validtype:
-            if t is None:
-                if variable is None:
-                    return
-            else:
-                if isinstance(variable, t):
-                    return
-    else:
-        if isinstance(variable, validtype):
-            return
-
-    raise TypeError, (variable, validtype)
-
-
-# -----------------------------------------------------------------------------
-# Module initialization
-# -----------------------------------------------------------------------------
-
-import __builtin__
-__builtin__.__dict__['checktype'] = checktype
-
-
-# -----------------------------------------------------------------------------
-# Self test code
-# -----------------------------------------------------------------------------
-
-if __name__ == '__main__':
-
-    import sys
-
-    def mustfail(testvar, validtype):
-        try:
-            checktype(testvar, validtype)
-
-        except TypeError, message:
-            print message
-            return
-        raise Error("checking %s as %s hasn't failed!" % (repr(variable),
-                                                          repr(validtype)))
-
-    n = None
-    s = 'this is a string'
-    u = u'this is a unicode string'
-    i = 100
-    f = 17.85
-    class p:
-        pass
-    class c(p):
-        pass
-    o = c()
-
-    print 'Single type, success ...'
-    checktype(n, types.NoneType)
-    checktype(s, str)
-    checktype(u, unicode)
-    checktype(i, int)
-    checktype(f, float)
-    checktype(c, types.ClassType)
-    checktype(o, types.InstanceType)
-    checktype(o, c)
-    checktype(o, p)
-
-    print 'Multiple types, success ...'
-    checktype(n, [str, types.NoneType])
-    checktype(s, [str, unicode])
-    checktype(u, [str, unicode])
-    checktype(o, [types.NoneType, c])
-
-    print 'Single type, failure ...'
-    mustfail(n, str)
-    mustfail(s, unicode)
-    mustfail(u, str)
-    mustfail(i, float)
-    mustfail(o, int)
-    mustfail(c, c)
-
-    print 'Multiple types, failure ...'
-    mustfail(n, [str, unicode])
-    mustfail(s, [int, float])
-
-    print 'All test passed.'
+from gnue.common.base.checktype import TypeError, checktype

Added: trunk/gnue-common/src/base/checktype.py
===================================================================
--- trunk/gnue-common/src/base/checktype.py     2007-11-14 15:18:39 UTC (rev 
9806)
+++ trunk/gnue-common/src/base/checktype.py     2007-11-15 08:06:04 UTC (rev 
9807)
@@ -0,0 +1,186 @@
+# GNU Enterprise Common Library - checktype support
+#
+# This file is part of GNU Enterprise.
+#
+# GNU Enterprise is free software; you can redistribute it
+# and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation; either
+# version 2, or (at your option) any later version.
+#
+# GNU Enterprise is distributed in the hope that it will be
+# useful, but WITHOUT ANY WARRANTY; without even the implied
+# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+# PURPOSE. See the GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public
+# License along with program; see the file COPYING. If not,
+# write to the Free Software Foundation, Inc., 59 Temple Place
+# - Suite 330, Boston, MA 02111-1307, USA.
+#
+# Copyright 2001-2007 Free Software Foundation
+#
+# $Id$
+
+"""
+Support for checking the type of variables.
+"""
+
+import sys
+import types
+
+from gnue.common.base import errors
+
+__all__ = ['TypeError', 'checktype']
+
+
+# =============================================================================
+# Exception definition
+# =============================================================================
+
+class TypeError(errors.SystemError):
+    """
+    Raised when L{checktype} detects a wrong type.
+
+    Do not raise this exception manually.
+    """
+    def __init__(self, variable, expected):
+
+        self.varname = '<?>'
+        for (key, val) in (sys._getframe(2)).f_locals.items():
+            if variable is val:
+                self.varname = key
+
+        self.expected = expected            # Expected type of variable
+
+        if isinstance(variable, types.InstanceType):
+            self.actual = variable.__class__
+        else:
+            self.actual = type(variable)     # Actual type of variable
+
+        self.value = variable               # Value of variable
+
+        message = u_('"%(varname)s" is expected to be of %(expected)s but is '
+                     'of %(actual)s and has value %(value)s') \
+                  % {'varname' : self.varname,
+                     'expected': self.__stringify(self.expected),
+                     'actual'  : self.__stringify(self.actual),
+                     'value'   : repr(self.value)}
+        errors.SystemError.__init__(self, message)
+
+
+    # -------------------------------------------------------------------------
+    # Get a nice string for a given type
+    # -------------------------------------------------------------------------
+
+    def __stringify(self, atype):
+
+        if isinstance(atype, list):
+            return ' / '.join([self.__stringify(ctp) for ctp in atype])
+
+        elif isinstance(atype, type):
+            return str(atype)
+
+        elif isinstance(atype, types.ClassType):
+            return '<class %s>' % str(atype)
+
+        else:
+            return '<type %s>' % str(atype)
+
+
+# -----------------------------------------------------------------------------
+# Check type of a variable
+# -----------------------------------------------------------------------------
+
+def checktype(variable, validtype):
+    """
+    Check a varaible (for example a parameter to a function) for a correct 
type.  
+    This function is available as builtin function.
+
+    @param variable: Variable to check.
+    @param validtype: Type, class, or a list of types and classes that are
+         valid.
+    @raise TypeError: The variable has a type not listed in the valid types.
+    """
+    if isinstance(validtype, list):
+        for ctp in validtype:
+            if ctp is None:
+                if variable is None:
+                    return
+            else:
+                if isinstance(variable, ctp):
+                    return
+    else:
+        if isinstance(variable, validtype):
+            return
+
+    raise TypeError, (variable, validtype)
+
+
+# -----------------------------------------------------------------------------
+# Module initialization
+# -----------------------------------------------------------------------------
+
+import __builtin__
+__builtin__.__dict__['checktype'] = checktype
+
+
+# -----------------------------------------------------------------------------
+# Self test code
+# -----------------------------------------------------------------------------
+
+if __name__ == '__main__':
+
+    def mustfail(testvar, validtype):
+        """
+        Test a variable agains a given type, assuming that the test fails
+        """
+        try:
+            checktype(testvar, validtype)
+
+        except TypeError, message:
+            print message
+            return
+        raise errors.Error("checking %s as %s hasn't failed!" % 
(repr(variable),
+                                                          repr(validtype)))
+
+    n = None
+    s = 'this is a string'
+    u = u'this is a unicode string'
+    i = 100
+    f = 17.85
+    class p:
+        pass
+    class c(p):
+        pass
+    o = c()
+
+    print 'Single type, success ...'
+    checktype(n, types.NoneType)
+    checktype(s, str)
+    checktype(u, unicode)
+    checktype(i, int)
+    checktype(f, float)
+    checktype(c, types.ClassType)
+    checktype(o, types.InstanceType)
+    checktype(o, c)
+    checktype(o, p)
+
+    print 'Multiple types, success ...'
+    checktype(n, [str, types.NoneType])
+    checktype(s, [str, unicode])
+    checktype(u, [str, unicode])
+    checktype(o, [types.NoneType, c])
+
+    print 'Single type, failure ...'
+    mustfail(n, str)
+    mustfail(s, unicode)
+    mustfail(u, str)
+    mustfail(i, float)
+    mustfail(o, int)
+    mustfail(c, c)
+
+    print 'Multiple types, failure ...'
+    mustfail(n, [str, unicode])
+    mustfail(s, [int, float])
+
+    print 'All test passed.'


Property changes on: trunk/gnue-common/src/base/checktype.py
___________________________________________________________________
Name: svn:keywords
   + Id





reply via email to

[Prev in Thread] Current Thread [Next in Thread]