def top(sort_key='cpu'):
    # TODO: add sort key middleware
    _key = itemgetter(sort_key)
    entries = [format_dict(pd) for pd in get_process_dicts()]

    # handle an apparent bug in psutil where the first call of the
    # process does not return any cpu percentages. sorting by memory
    # percentages instead.
    try:
        sort_total = sum([_key(x) for x in entries if _key(x) is not None])
        if not sort_total:
            sort_key = 'mem'
            _key = itemgetter(sort_key)
    except __HOLE__:
        pass
    entries.sort(key=_key, reverse=True)
    return {'entries': entries}
dataset/ETHPy150Open mahmoud/clastic/clastic/contrib/webtop/top.py/top