Author: johannes
Date: 2007-11-14 09:18:39 -0600 (Wed, 14 Nov 2007)
New Revision: 9806
Modified:
trunk/gnue-common/src/apps/checktype.py
Log:
PEP8-ification of checktype
Modified: trunk/gnue-common/src/apps/checktype.py
===================================================================
--- trunk/gnue-common/src/apps/checktype.py 2007-11-14 14:53:15 UTC (rev 9805)
+++ trunk/gnue-common/src/apps/checktype.py 2007-11-14 15:18:39 UTC (rev 9806)
@@ -25,97 +25,100 @@
Support for checking the type of variables.
"""
-from types import *
-
-import string
import sys
+import types
-from gnue.common.apps import errors, i18n
+from gnue.common.base import errors, i18n
-# -----------------------------------------------------------------------------
+# =============================================================================
# Exception definition
-# -----------------------------------------------------------------------------
+# =============================================================================
-class TypeError (errors.SystemError):
- """
- Raised when L{checktype} detects a wrong type.
+class TypeError(errors.SystemError):
+ """
+ Raised when L{checktype} detects a wrong type.
- Do not raise this exception manually.
- """
+ 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
- def __stringify (self, atype):
- if isinstance (atype, ListType):
- return string.join ([self.__stringify (t) for t in atype], ' / ')
- elif isinstance (atype, TypeType):
- return str (atype)
- elif isinstance (atype, ClassType):
- return '<class %s>' % str (atype)
- else:
- return '<type %s>' % str (atype)
+ 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
- def __init__ (self, variable, expected):
+ self.value = variable # Value of variable
- self.varname = '<?>'
- for (k, v) in (sys._getframe (2)).f_locals.items ():
- if variable is v:
- self.varname = k
+ 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)
- self.expected = expected # Expected type of variable
- if isinstance (variable, InstanceType):
- self.actual = variable.__class__
- else:
- self.actual = type (variable) # Actual type of variable
+ # -------------------------------------------------------------------------
+ # Get a nice string for a given type
+ # -------------------------------------------------------------------------
- self.value = variable # Value of variable
+ def __stringify(self, atype):
- 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)
+ 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.
+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.
- 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
- @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, ListType):
- 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)
+ raise TypeError, (variable, validtype)
# -----------------------------------------------------------------------------
# Module initialization
# -----------------------------------------------------------------------------
-import __builtin__
+import __builtin__
__builtin__.__dict__['checktype'] = checktype
@@ -125,55 +128,56 @@
if __name__ == '__main__':
- import sys
+ 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)))
+ def mustfail(testvar, validtype):
+ try:
+ checktype(testvar, 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 ()
+ except TypeError, message:
+ print message
+ return
+ raise Error("checking %s as %s hasn't failed!" % (repr(variable),
+ repr(validtype)))
- print 'Single type, success ...'
- checktype (n, NoneType)
- checktype (s, StringType)
- checktype (u, UnicodeType)
- checktype (i, IntType)
- checktype (f, FloatType)
- checktype (c, ClassType)
- checktype (o, InstanceType)
- checktype (o, c)
- checktype (o, p)
+ 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 'Multiple types, success ...'
- checktype (n, [StringType, NoneType])
- checktype (s, [StringType, UnicodeType])
- checktype (u, [StringType, UnicodeType])
- checktype (o, [NoneType, 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 'Single type, failure ...'
- mustfail (n, StringType)
- mustfail (s, UnicodeType)
- mustfail (u, StringType)
- mustfail (i, FloatType)
- mustfail (o, IntType)
- mustfail (c, c)
+ print 'Multiple types, success ...'
+ checktype(n, [str, types.NoneType])
+ checktype(s, [str, unicode])
+ checktype(u, [str, unicode])
+ checktype(o, [types.NoneType, c])
- print 'Multiple types, failure ...'
- mustfail (n, [StringType, UnicodeType])
- mustfail (s, [IntType, FloatType])
+ print 'Single type, failure ...'
+ mustfail(n, str)
+ mustfail(s, unicode)
+ mustfail(u, str)
+ mustfail(i, float)
+ mustfail(o, int)
+ mustfail(c, c)
- print 'All test passed.'
+ print 'Multiple types, failure ...'
+ mustfail(n, [str, unicode])
+ mustfail(s, [int, float])
+
+ print 'All test passed.'
lmpx.com only provides a reader for public news (NNTP) servers. It is not
affiliated with the servers or forums shown here and is not responsible for
the content of articles, which is written by their respective authors.