def getmembers(obj, predicate=None):
    """ ** Extracted from inspect module for optimisation purposes **
    Return all members of an object as (name, value) pairs sorted by name.
    Optionally, only return members that satisfy a given predicate."""
    results = []
    for key in dir(obj):
        try:
            value = getattr(obj, key)
        except AttributeError:
            continue
        if not predicate or predicate(value):
            results.append((key, value))

    results.sort()
    return results