def keep(source, key=None, value=None):
    """Discard all records that don't match the given criteria

    Examples
    --------

    Discard any record that doesn't have an 'id' key with value "GPGGA":

    >>> pipe = # Defined elsewhere
    >>> pipe = pipecat.filter.keep(pipe, key="id", value="GPGGA")
    >>> for record in pipe:
    ...     print record

    Parameters
    ----------
    source: :ref:`Record generator <record-generators>`, required
    key: string or tuple-of-strings, optional
        Records must contain this key or they will be discarded.
    value: optional.
        Records with field 'key' must match this value or they will be discarded.
    """
    for record in source:
        if key is not None and key not in record:
            continue
        if key is not None and value is not None and record[key] != value:
            continue
        yield record
