def winnow(statuses, arg, status_key, func=None):
    """
    Call with a list of statuses, and the ctx.<key>
    'arg' that you may want to filter by.
    If arg is not None, filter statuses by either:

    1) func=None: filter by status[status_key] == arg
    remove any status that fails

    2) func=<filter function that takes status>: remove any
    status for which func returns False

    Return the possibly-smaller set of statuses.
    """

    if arg is not None:
        if func:
            statuses = [_status for _status in statuses
                        if func(_status)]
        else:
            statuses = [_status for _status in statuses
                       if _status[status_key] == arg]

    return statuses