'Return \'weight\' of a process and all its children for sorting.'

def get_weight(self):

    if ((self.sort_key == 'name') or (self.sort_key == 'username')):
        return self.stats[self.sort_key]
    total = 0
    nodes_to_sum = collections.deque([self])
    while nodes_to_sum:
        current_node = nodes_to_sum.pop()
        if isinstance(self.sort_key, collections.Callable):
            total += self.sort_key(current_node.stats)
        elif (self.sort_key == 'io_counters'):
            stats = current_node.stats[self.sort_key]
            total += (((stats[0] - stats[2]) + stats[1]) - stats[3])
        elif (self.sort_key == 'cpu_times'):
            total += sum(current_node.stats[self.sort_key])
        else:
            total += current_node.stats[self.sort_key]
        nodes_to_sum.extend(current_node.children)
    return total
