global FALSE, TRUE
FALSE = 0 # binary 000
TRUE = 1 # binary 001
MAYBE = 2 # binary 010
global typep_exc, MODULE_EXC, FILE_EXC, CODE_EXC, FRAME_EXC, TRACEBACK_EXC
typep_exc = 'TypepException'
MODULE_EXC = 'module'
FILE_EXC = 'file'
INTERNAL_EXC = 'internal'
CODE_EXC = 'code'
FRAME_EXC = 'frame'
TRACEBACK_EXC = 'traceback'
def istype(o, t):
if type(o) == t:
return TRUE
else:
return FALSE
def typep(o):
if type(o) == type(type(type)):
return TRUE
else:
return FALSE
def nonep(o):
if type(o) == type(None):
return TRUE
else:
return FALSE
def numberp(o):
if integerp(o) | floatp(o):
return TRUE
else:
return FALSE
def integerp(o):
if plainintp(o) | longintp(o):
return TRUE
else:
return FALSE
def plainintp(o):
if type(o) == type(0):
return TRUE
else:
return FALSE
def longintp(o):
if type(o) == type(0L):
return TRUE
else:
return FALSE
def floatp(o):
if type(o) == type(0.0):
return TRUE
else:
return FALSE
def sequencep(o):
if immutableseqp(o) | mutableseqp(o):
return TRUE
else:
return FALSE
def immutableseqp(o):
if stringp(o) | tuplep(o):
return TRUE
else:
return FALSE
def stringp(o):
if type(o) == type(''):
return TRUE
else:
return FALSE
def tuplep(o):
if type(o) == type(()):
return TRUE
else:
return FALSE
def mutableseqp(o):
if listpp(o):
return TRUE
else:
return FALSE
def listp(o):
if type(o) == type([]):
return TRUE
else:
return FALSE
def mappingp(o):
if dictp(o):
return TRUE
else:
return FALSE
def dictionaryp(o):
if type(o) == type({}):
return TRUE
else:
return FALSE
def callablep(o):
if ufunctionp(o) | umethodp(o) | bfunctionp(o) | bmethodp(o) | classp(o):
return TRUE
else:
return FALSE
def ufunctionp(o):
def function(): pass
if type(o) == type(function):
return TRUE
else:
return FALSE
def umethodp(o):
class C:
def method(): pass
if type(o) == type(C.method):
return TRUE
else:
return FALSE
def bfunctionp(o):
if type(o) == type(len):
return TRUE
else:
return FALSE
def bmethodp(o):
if type(o) == type([].append):
return TRUE
else:
return FALSE
def classp(o):
class C: pass
if type(o) == type(C):
return TRUE
else:
return FALSE
def instancep(o):
class C: pass
if type(o) == type(C()):
return TRUE
else:
return FALSE
def modulep(o):
try:
import sys
except ImportError, SyntaxError:
raise TypepException, MODULE_EXC
m = sys.modules['sys']
if type(o) == type(m):
return TRUE
else:
return FALSE
def filep(o):
try:
f = open('typep.py', 'r')
except IOError:
raise TypepException, FILE_EXC
if type(o) == type(f):
r = TRUE
else:
r = FALSE
close(f)
return r
def internalp(o):
b = FALSE
for f in [codep, framep, tracebackp]:
try:
b = b | f(o)
except typep_exc, value:
b = b | MAYBE
if b & TRUE:
return TRUE
elif b & MAYBE:
raise TypepException, INTERNAL_EXC
else:
return FALSE
def codep(o):
import sys
user_exc = 'UserDefinedException'
try:
raise user_exc
except user_exc:
t = sys.exc_traceback
f = t.tb_frame
c = f.f_code
if type(o) == type(c):
return TRUE
else:
return FALSE
raise TypepException, CODE_EXC
def framep(o):
import sys
user_exc = 'UserDefinedException'
try:
raise user_exc
except user_exc:
t = sys.exc_traceback
f = t.tb_frame
if type(o) == type(f):
return TRUE
else:
return FALSE
raise TypepException, FRAME_EXC
def tracebackp(o):
import sys
user_exc = 'UserDefinedException'
try:
raise user_exc
except user_exc:
t = sys.exc_traceback
if type(o) == type(t):
return TRUE
else:
return FALSE
raise TypepException, TRACEBACK_EXC
====================== CUT HERE =================
share and enjoy,
jpl