Free Python Software, but you get what you pay for

Jeff P. Lankford (jpl@nrtc.northrop.com)
Mon, 29 Aug 1994 18:09:43 GMT

====================== CUT HERE =================
###
### typep.py -- by Jeff Lankford, 1994.
### This module implements type predicates analogous to Common Lisp.
### The implementation is in Python, 'cause i don wanna be C programer.
### These really should be built-in functions, along with Boolean type values
### TRUE and FALSE.
### Use of this software is unrestricted, provided you acknowledge the author.
### No warranty or liability -- use at your own risk!
###
### istype(object, type) returns TRUE if object is of type; otherwise FALSE.
### typep(object) returns TRUE if object is a type object; otherwise FALSE.
### nonep(object) returns TRUE if object is of type None (ie, the object None);
### otherwise FALSE.
### numberp(object) returns TRUE if object is of type integerp or floatp;
### otherwise FALSE.
### integerp(object) returns TRUE if object is of type plainintp or longintp;
### plainintp(object) returns TRUE if object is of type 32-bit (or better)
### precision integer; otherwise FALSE.
### longintp(object) returns TRUE if object is of type unlimited
### precision integer; otherwise FALSE.
### floatp(object) returns TRUE if object is of type machine-level double
### precision floating point number; otherwise FALSE.
### sequencep(object) returns TRUE if object is of type immutableseqp or
### mutableseqp; otherwise FALSE.
### immutableseqp(object) returns TRUE if object is of type stringp or tuplep;
### otherwise FALSE.
### stringp(object) returns TRUE if object is of type character sequence;
### otherwise FALSE.
### tuplep(object) returns TRUE if object is of type arbitrary Python object
### sequence; otherwise FALSE.
### mutableseqp(object) returns TRUE if object is of type listp;
### otherwise FALSE.
### listp(object) returns TRUE if object is of type arbitrary Python object
### list; otherwise FALSE.
### mappingp(object) returns TRUE if object is of type dictionaryp;
### otherwise FALSE.
### dictonaryp(object) returns TRUE if object is of type finite set of objects
### indexed by immutable values; otherwise FALSE.
### callablep(object) returns TRUE if object is of type userfuncp, usermethp,
### binfuncp, binmethp, or classp; otherwise FALSE.
### userfuncp(object) returns TRUE if object is of type user-defined function;
### otherwise FALSE.
### usermethp(object) returns TRUE if object is of type user-defined method;
### otherwise FALSE.
### binfuncp(object) returns TRUE if object is of type built-in function;
### otherwise FALSE.
### binmethp(object) returns TRUE if object is of type built-in method;
### otherwise FALSE.
### classp(object) returns TRUE if object is of type class definition;
### otherwise FALSE
### instancep(object) returns TRUE if object is of type class instantiation;
### otherwise FALSE.
### modulep(object) returns TRUE if object is of type imported object;
### otherwise FALSE. Raises 'TypepException' if module 'sys'
### can't be imported.
### filep(object) returns TRUE if object is of type open file; otherwise FALSE.
### Raises 'TypepException' if file 'typep.py' can't be opened.
### internalp(object) returns TRUE if object is of type codep, framep,
### or tracebackp; otherwise FALSE. Raises 'TypepException' if neither
### FALSE nor TRUE.
### codep(object) returns TRUE if object is of type executable code;
### otherwise FALSE. Raises 'TypepException' if 'UserDefinedException'
### can't be raised.
### framep(object) returns TRUE if object is of type execution frame;
### otherwise FALSE. Raises 'TypepException' if 'UserDefinedException'
### can't be raised.
### tracebackp(object) returns TRUE if object is of type exception stack trace;
### otherwise FALSE. Raises 'TypepException' if 'UserDefinedException'
### can't be raised.
###

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