+
+def toDiffableString(value):
+ s = repr(value)
+ ds = ""
+ i = s.find(" at 0x")
+ start = 0
+ while (i >= 0):
+ j = s.find(">", i)
+ if (j < i):
+ break
+ ds += s[start:i]
+ start = j
+ i = s.find(" at 0x", start)
+ return ds + s[start:]
+
+PRINT_OBJ_GETATTR = 1
+PRINT_OBJ_HIDE_INTERNAL = 2
+PRINT_OBJ_COMPACT = 4
+PRINT_OBJ_NONONE = 8
+PRINT_OBJ_INTERNAL = 512
+
+def printObject(out, object, name="", indent=0, flags=0, exclude=None, maxIndent=30):
+ if ((maxIndent != None) and (indent > maxIndent)):
+ print >> out, " "*indent, name, str(object)
+ return True
+ finalNewLine = False
+ printed = True
+ if ((flags & PRINT_OBJ_COMPACT) > 0):
+ if (exclude and object in exclude):
+ return
+ indent = 0
+ if ((flags & PRINT_OBJ_INTERNAL) == 0):
+ finalNewLine = True
+ flags |= PRINT_OBJ_INTERNAL
+ if (object == None):
+ if (flags & PRINT_OBJ_NONONE) == 0:
+ print >> out, " "*indent, name, " = None",
+ else:
+ finalNewLine = False
+ printed = False
+ elif (name.startswith("_") and ((flags & PRINT_OBJ_HIDE_INTERNAL) > 0)):
+ finalNewLine = False
+ printed = False
+ elif (isinstance(object, (list, tuple))):
+ if (exclude and object in exclude):
+ print >> out, " "*indent, name, " : ", type(object), " of length = ", len(object), " (already printed)",
+ elif (exclude and name in exclude):
+ print >> out, " "*indent, name, " : ", type(object), " of length = ", len(object), " (excluded)",
+ else:
+ if (exclude != None): exclude.append(object)
+ print >> out, " "*indent, name, " : ", type(object), " of length = %i" % len(object),
+ for i, o in enumerate(object):
+ print >> out
+ printObject(out, o, name="[%i]" % i, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent)
+ elif (isinstance(object, dict)):
+ if (exclude and object in exclude):
+ print >> out, " "*indent, name, " : ", type(object), " (already printed)",
+ else:
+ if (exclude != None): exclude.append(object)
+ if (len(name) > 0):
+ print >> out, " "*indent, name,
+ if ((flags & PRINT_OBJ_COMPACT) == 0):
+ print >> out
+ indent += 2
+ print >> out, " "*indent, "{",
+ if ((flags & PRINT_OBJ_COMPACT) == 0):
+ print >> out
+ keys = object.keys()
+ keys.sort()
+ for n in keys:
+ if ((n != None) and (not n.startswith("_") or ((flags & PRINT_OBJ_HIDE_INTERNAL) == 0))):
+ if printObject(out, object[n], name=n, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent):
+ if ((flags & PRINT_OBJ_COMPACT) == 0):
+ print >> out
+ else:
+ print >> out, ",",
+ print >> out, " "*indent, "}",
+ elif (hasattr(object, "__dict__")):
+ if (exclude and object in exclude):
+ print >> out, " "*indent, name, " : ", type(object), " (already printed) = ", toDiffableString(object),
+ else:
+ if (exclude != None): exclude.append(object)
+ if (name.startswith("_")):
+ print >> out, " "*indent, name, " : ", type(object),
+ elif (exclude and object.__dict__ in exclude):
+ print >> out, " "*indent, name, " : ", type(object), " (already printed)",
+ else:
+ print >> out, " "*indent, name, " : ", type(object),
+ if ((flags & PRINT_OBJ_GETATTR) == 0):
+ if ((flags & PRINT_OBJ_COMPACT) == 0):
+ print >> out
+ printObject(out, object.__dict__, indent=indent, flags=flags, exclude=exclude, maxIndent=maxIndent)
+ else:
+ keys = object.__dict__.keys()
+ keys.sort()
+ for n in keys:
+ if ((flags & PRINT_OBJ_COMPACT) == 0):
+ print >> out
+ printObject(out, getattr(object, n), name=n, indent=indent+2, flags=flags, exclude=exclude, maxIndent=maxIndent)
+ elif (indent < 0):
+ print >> out, object,
+ elif isinstance(object, basestring):
+ if (exclude and name in exclude):
+ print >> out, " "*indent, name, " : ", type(object), " of length = ", len(object), " (excluded)",
+ elif (len(object) > 100):
+ print >> out, " "*indent, name, ":", type(object), "[%i] = %s...%s" % (len(object), object[:50], object[-50:]),
+ else:
+ print >> out, " "*indent, name, ":", type(object), "=", str(object),
+ else:
+ print >> out, " "*indent, name, ":", type(object), "=", str(object),
+ if (finalNewLine):
+ print >> out
+ return printed