1 #----------------------------------------------------------------------------
3 # Purpose: Object Utilities
5 # Author: Alan Mullendore
9 # Copyright: (c) 2004-2005 ActiveGrid, Inc.
10 # License: wxWindows License
11 #----------------------------------------------------------------------------
19 def classForName(className
):
20 pathList
= className
.split('.')
21 moduleName
= '.'.join(pathList
[:-1])
22 code
= __import__(moduleName
)
23 for name
in pathList
[1:]:
24 code
= code
.__dict
__[name
]
27 def hasPropertyValue(obj
, attr
):
30 prop
= obj
.__class
__.__dict
__[attr
]
31 if (isinstance(prop
, property)):
32 hasProp
= hasattr(obj
, attr
)
34 # It's a property and it has a value but sometimes we don't want it.
35 # If there is a _hasattr method execute it and the
36 # result will tell us whether to include this value
38 hasProp
= obj
._hasattr
(attr
)
45 def toDiffableString(value
):
56 i
= s
.find(" at 0x", start
)
59 def toString(value
, options
=0):
60 if ((options
& PRINT_OBJ_DIFFABLE
) > 0):
61 return toDiffableString(value
)
64 def toTypeString(obj
):
65 if (isinstance(obj
, BooleanType
)):
67 elif (isinstance(obj
, UnicodeType
)):
69 elif (isinstance(obj
, basestring
)):
71 elif (isinstance(obj
, IntType
)):
73 elif (isinstance(obj
, FloatType
)):
75 elif (type(obj
) == ListType
):
77 elif (isinstance(obj
, DictType
)):
79 elif (isinstance(obj
, TupleType
)):
81 elif (isinstance(obj
, InstanceType
)):
87 PRINT_OBJ_HIDE_INTERNAL
= 2
90 PRINT_OBJ_DIFFABLE
= 16
91 PRINT_OBJ_INTERNAL
= 512
93 def printObject(out
, object, name
="", indent
=0, flags
=0, exclude
=None, maxIndent
=30):
94 if ((maxIndent
!= None) and (indent
> maxIndent
)):
95 print >> out
, " "*indent
, "%s: %s" % (name
, toString(str(object), flags
)),
96 if ((flags
& PRINT_OBJ_INTERNAL
) == 0):
101 ## if (exclude == None):
103 if ((flags
& PRINT_OBJ_COMPACT
) > 0):
104 if (exclude
and object in exclude
):
107 if ((flags
& PRINT_OBJ_INTERNAL
) == 0):
109 flags |
= PRINT_OBJ_INTERNAL
111 if (flags
& PRINT_OBJ_NONONE
) == 0:
112 print >> out
, " "*indent
, name
, " = None",
116 elif (name
.startswith("_") and ((flags
& PRINT_OBJ_HIDE_INTERNAL
) > 0)):
119 elif (isinstance(object, (list, tuple))):
120 if ((exclude
!= None) and object in exclude
):
121 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " of length = ", len(object), " (already printed)",
122 elif ((exclude
!= None) and name
in exclude
):
123 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " of length = ", len(object), " (excluded)",
125 if ((exclude
!= None) and (len(object) > 0)): exclude
.append(object)
126 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " of length = %d" % len(object),
127 for i
, o
in enumerate(object):
129 printObject(out
, o
, name
="[%d]" % i
, indent
=indent
+2, flags
=flags
, exclude
=exclude
, maxIndent
=maxIndent
)
130 elif (isinstance(object, dict)):
131 if ((exclude
!= None) and object in exclude
):
132 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " (already printed)",
134 if ((exclude
!= None) and (len(object) > 0)): exclude
.append(object)
136 print >> out
, " "*indent
, name
,
137 if ((flags
& PRINT_OBJ_COMPACT
) == 0):
140 print >> out
, " "*indent
, "{",
141 if ((flags
& PRINT_OBJ_COMPACT
) == 0):
148 if (not (isinstance(n
, basestring
))):
150 if ((not n
.startswith("_") or ((flags
& PRINT_OBJ_HIDE_INTERNAL
) == 0))):
151 if printObject(out
, object[key
], name
=n
, indent
=indent
+2, flags
=(flags | PRINT_OBJ_INTERNAL
), exclude
=exclude
, maxIndent
=maxIndent
):
152 if ((flags
& PRINT_OBJ_COMPACT
) == 0):
156 print >> out
, " "*indent
, "}",
157 elif (hasattr(object, "__dict__")):
158 if ((exclude
!= None) and object in exclude
):
159 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " (already printed) = ", toDiffableString(object),
161 if (exclude
!= None): exclude
.append(object)
162 if (name
.startswith("_")): ## and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)):
163 print >> out
, " "*indent
, name
, " : ", toTypeString(object),
164 elif ((exclude
!= None) and object.__dict
__ in exclude
):
165 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " (already printed)",
167 print >> out
, " "*indent
, name
, " : ", toTypeString(object),
168 if ((flags
& PRINT_OBJ_GETATTR
) == 0):
169 if ((flags
& PRINT_OBJ_COMPACT
) == 0):
171 printObject(out
, object.__dict
__, indent
=indent
, flags
=flags
, exclude
=exclude
, maxIndent
=maxIndent
)
173 keys
= object.__dict
__.keys()
176 if ((flags
& PRINT_OBJ_COMPACT
) == 0):
178 printObject(out
, getattr(object, n
), name
=n
, indent
=indent
+2, flags
=flags
, exclude
=exclude
, maxIndent
=maxIndent
)
180 print >> out
, object,
181 elif isinstance(object, basestring
):
182 if ((exclude
!= None) and name
in exclude
):
183 print >> out
, " "*indent
, name
, " : ", toTypeString(object), " of length = ", len(object), " (excluded)",
184 elif (len(object) > 100):
185 print >> out
, " "*indent
, name
, ":", toTypeString(object), "[%d] = %s...%s" % (len(object), object[:50], object[-50:]),
187 print >> out
, " "*indent
, name
, ":", toTypeString(object), "=", str(object),
188 ## elif (isinstance(object, float)):
190 ## if (len(val) > 17):
192 ## print >> out, " "*indent, name, ":", type(object), "=", val,
194 print >> out
, " "*indent
, name
, ":", toTypeString(object), "=", str(object),