Re: Testing for an object

Marvin R Long (mlong@express.ctron.com)
Wed, 16 Mar 94 08:47:59 EST

To: "Adrian Phillips
Subject: Re: Testing for an object
In-Reply-To: Mail from '"Adrian Phillips (Tandem User)" <tandem@freeze.oslo.dnmi.no>'
dated: Wed, 16 Mar 1994 12:20:09 +0000
Cc: dev

Adrian,

Marvin Long here I have a module which you may be interested in. I call
it debug.py and it has proved very useful to me in checking things out.
I hope theres something in it which proves usefull. There is a companion
module called constants.py also.

-Marvin

------------------------ debug.py -------------------------------------------
import sys
from constants import *

def dumpModule( thing ) :
outstr = '\n--> ' + thing.__name__ + '.py'
print outstr
keys = thing.__dict__.keys()
for key in keys :
if type(thing.__dict__[key]) == CLASS_TYPE :
#print thing.__dict__[key]
dumpClass( thing.__dict__[key], 0 )
#base_methods = base.__dict__.keys()
#for base_method in base_methods :
#print ' ',base.__dict__[base_method]
#
#class_methods = thing.__dict__[key].__dict__.keys()
#for class_method in class_methods :
#print ' ',thing.__dict__[key].__dict__[class_method]

return

def dumpClass( thing, indent ):

i = 0
if not indent :
print '\n'
tab_str = ''
while i < indent :
tab_str = tab_str + ' '
i = i + 1
print tab_str, thing
tab_str = tab_str + ' '
indent = indent + 1
bases = thing.__bases__
for base in bases :
#dumpClass( base, indent )
print tab_str, '<derived from> ', `base`

method_keys = thing.__dict__.keys()
for method_key in method_keys :
method = thing.__dict__[method_key]
#print 'debug method :', getattr(thing,method_key)
print tab_str, method.func_code
#print tab_str, method.func_code.co_names
#print tab_str, method.func_code.co_consts

return

def dumpAttributes( thing ) :

print '\n-->',thing,'\n\n\tATTRIBUTE LIST.\n'
keys = thing.__dict__.keys()
for key in keys :
print '\t\t',key,':',getattr(thing,key)#type(thing.__dict__[key])
return

def dumpClassInstance( thing ) :
#
# Class Instance Attribute Dictionary
#
dumpAttributes( thing )
#
# Class Name Space
#
print '\n\tNAME SPACE.\n'
dumpClass( thing.__class__,2 )
#module_names = thing.__class__.__dict__.keys()
#for module_name in module_names :
#print ' ',module_name,':',type(thing.__class__.__dict__[module_name])
#module = thing.__class__.__dict__[module_name]
#func_global_keys = module.func_globals.keys()
#for func_global_key in func_global_keys :
#print ' ',func_global_key
#print ' ',module_name,':',module.func_code
#arg_dict_loc = len(module.func_code.co_consts)-1
#arg_dict = module.func_code.co_consts[arg_dict_loc]
#for item in module.func_code.co_consts :
#print ' ',item
#if type(item) == type(1) :
#keys = arg_dict.keys()
#for key in keys :
#if arg_dict[ key ] == item :
#print ' argument :', key
#print ' ',item,' type:',type(item)
#print ' arg_dict:',module.func_code.co_consts[arg_dict_loc]

#
# Base Classes
#
#print '\n-->',thing,'derived classes.\n'
#for base in thing.__class__.__bases__ :
#print ' ',base
#base_class_method_keys = base.__dict__.keys()
#for base_class_method_key in base_class_method_keys :
#print ' ',base_class_method_key,': <type base_class_method_key>'

print '\n\n'
return

------------------------------ constants.py ----------------------------------
#
def afunction(): pass
class DUMMYCLASS: pass

NEWLINE = '\n'

TRUE = 1
FALSE = 0
INTEGER_TYPE = type(0)
STRING_TYPE = type('a')
NONE_TYPE = type(None)
TUPLE_TYPE = type(1,2)
FUNCTION_TYPE = type(afunction)
CLASS_TYPE = type(DUMMYCLASS)
REAL_TYPE = type(3.14159)
OBJECT_TYPE = type(DUMMYCLASS())
LIST_TYPE = type([])
DICTIONARY_TYPE = type({})
LONG_TYPE = type(0L)

del(afunction) #don't keep this around, no longer needed
//////////////////////////////////////////////////////////////////////////////
// Marvin Long // Cabletron Systems, Inc. // 603-337-7384 //
// mlong@ctron.com // Box 5005, Rochester, NH 03867 // fax 603-332-1019 //
//////////////////////////////////////////////////////////////////////////////