commit-gnue
[Top][All Lists]
Advanced

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

r5271 - trunk/gnue-common/src/apps


From: reinhard
Subject: r5271 - trunk/gnue-common/src/apps
Date: Mon, 8 Mar 2004 14:58:47 -0600 (CST)

Author: reinhard
Date: 2004-03-08 14:58:46 -0600 (Mon, 08 Mar 2004)
New Revision: 5271

Added:
   trunk/gnue-common/src/apps/checktype.py
Modified:
   trunk/gnue-common/src/apps/__init__.py
Log:
Implemented global checktype() function.


Modified: trunk/gnue-common/src/apps/__init__.py
===================================================================
--- trunk/gnue-common/src/apps/__init__.py      2004-03-08 20:52:04 UTC (rev 
5270)
+++ trunk/gnue-common/src/apps/__init__.py      2004-03-08 20:58:46 UTC (rev 
5271)
@@ -37,3 +37,4 @@
 
 import GDebug
 import i18n
+import checktype

Added: trunk/gnue-common/src/apps/checktype.py
===================================================================
--- trunk/gnue-common/src/apps/checktype.py     2004-03-08 20:52:04 UTC (rev 
5270)
+++ trunk/gnue-common/src/apps/checktype.py     2004-03-08 20:58:46 UTC (rev 
5271)
@@ -0,0 +1,148 @@
+# 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-2004 Free Software Foundation
+#
+# $Id$
+
+import sys
+import string
+from types import *
+
+from gnue.common.apps import i18n
+
+# -----------------------------------------------------------------------------
+# Exceptions
+# -----------------------------------------------------------------------------
+
+class TypeError (Exception):
+
+# -----------------------------------------------------------------------------
+
+  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)
+
+# -----------------------------------------------------------------------------
+
+  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
+    self.actual = type (variable)       # Actual type of variable
+    self.value = variable               # Value of variable
+
+    message = _(
+'''"%(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)}
+    Exception.__init__ (self, message)
+
+# -----------------------------------------------------------------------------
+# Check type of a variable
+# -----------------------------------------------------------------------------
+
+def __checktype (variable, validtype):
+  if isinstance (validtype, ListType):
+    for t in validtype:
+      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:
+      print sys.exc_info () [1]
+      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, 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)
+
+  print 'Multiple types, success ...'
+  checktype (n, [StringType, NoneType])
+  checktype (s, [StringType, UnicodeType])
+  checktype (u, [StringType, UnicodeType])
+  checktype (o, [NoneType, c])
+
+  print 'Single type, failure ...'
+  mustfail (n, StringType)
+  mustfail (s, UnicodeType)
+  mustfail (u, StringType)
+  mustfail (i, FloatType)
+  mustfail (f, IntType)
+  mustfail (c, c)
+
+  print 'Multiple types, failure ...'
+  mustfail (n, [StringType, UnicodeType])
+  mustfail (s, [IntType, FloatType])
+
+  print 'All test passed.'


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





reply via email to

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