-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisttree.py
More file actions
46 lines (40 loc) · 1.71 KB
/
listtree.py
File metadata and controls
46 lines (40 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
class ListTree:
'''
Mix-in that returns an __str__ trace of the entire
class tree and all its objects' attrs at and above
self; run by print(), str() returns constructed
string; uses __x attr names to avoid impacting clients
recurses to superclasses explicitly, uses str.format
'''
def __attrnames(self, obj, indent):
spaces = ' ' * (indent + 1)
result = ''
for attr in sorted(obj.__dict__):
if (attr.startswith('__') and
attr.endswith('__')):
result += spaces + '{}\n'.format(attr)
else:
result += (spaces +
'{0}={1}\n'.format(attr,
getattr(obj,attr)))
return result
def __listclass(self, aClass, indent):
dots = '.' * indent
if aClass in self.__visited:
return '\n{0}<Class {1}:, address {2}:(see above)>\n'.format(dots,aClass.__name__, id(aClass))
else:
self.__visited[aClass] = True
here = self.__attrnames(aClass, indent)
above = ''
for super in aClass.__bases__:
above += self.__listclass(super,
indent+4)
return '\n{0}<Class {1}, address {2}:\n{3}{4}{5}>\n'.format(dots, aClass.__name__, id(aClass),here,
above, dots)
def __str__(self):
self.__visited = {}
here = self.__attrnames(self, 0)
above = self.__listclass(self.__class__, 4)
return '<Instance of {0}, address {1}:\n{2}{3}>'.format(self.__class__.__name__, id(self), here, above)
if __name__ == '__main__':
pass