import gc #================================================================================== class SumList: def __init__(self, name, children, size): self.name = name self.children = children if self.children is None: self.children = [] self.size = size def append_child(self, sumlist): self.children.append(sumlist) self.size += sumlist.size def printout(self, tab=0 ): print " "*tab,"%s (%i)"%(sumlist.name,sumlist.size) if not sumlist.children: return if tab > 1: return for e in sumlist.children: e.printout(tab+1) def sort(self): self.children.sort(self.__cmp_fn) def sort_by_size(self): self.children.sort(self.__cmp_by_size) def __cmp_by_size(self, b, a ): return cmp(a[2], b[2]) # compare sizes def __cmp_fn(self, b, a ): """ a and b are tuples describing a directory tree. Compare first by directory status, then by size, and finally by name. """ a_dir = a[1] != None b_dir = b[1] != None if a_dir and not b_dir: return 1 elif b_dir and not a_dir: return -1 elif a_dir and b_dir: return cmp(a[2], b[2]) # compare sizes else: return cmp(a[0], b[0]) # compare names def __getitem__(self, key): if key == 0: return self.name if key == 1: return self.children if key == 2: return self.size def __len__(self): return 3