/ Published in: Python
                    
                                        
not an answer for everything, but perhaps for basic types
                
                            
                                Expand |
                                Embed | Plain Text
                            
                        
                        Copy this code and paste it in your HTML
from types import *
def bfnCheckArgTypes(*args):
sErr = ""
# loops through: ('sString', 'tupTuple', 'iInteger', 'lstList')
for iInd, sArgName in enumerate(args[0].func_code.co_varnames):
if sArgName.startswith("tup") and not type(args[iInd+1]) is TupleType:
sErr += "\n arg '%s' is not of type 'tuple'" % sArgName
elif sArgName.startswith("lst") and not type(args[iInd+1]) is ListType:
sErr += "\n arg '%s' is not of type 'list'" % sArgName
elif sArgName.startswith("i") and not type(args[iInd+1]) is IntType:
sErr += "\n arg '%s' is not of type 'int'" % sArgName
elif sArgName.startswith("s") and not type(args[iInd+1]) is StringType:
sErr += "\n arg '%s' is not of type 'str'" % sArgName
if not sErr == "":
print "Function '%s' has argument errors: %s" % (args[0].__name__, sErr)
return False
return True
def fnTestMe(sString, tupTuple, iInteger, lstList):
assert bfnCheckArgTypes(fnTestMe, sString, tupTuple, iInteger, lstList)
# Test the code with these:
fnTestMe("string-OK", ("tuple","OK"), 55, ["list","OK"]) # all OK
#fnTestMe("string-OK", "notTuple", 55, "notList") # 2nd and 4th fail
fnTestMe(55, ("tuple","OK"), "string-NOK", ["list","OK"]) # 1st and 3rd fail
Comments
 Subscribe to comments
                    Subscribe to comments
                
                