def sequences_match(seq1,seq2,max_mismatches=0):
    """Determine whether two sequences match with specified tolerance

    Returns True if sequences 'seq1' and 'seq2' are 
    consider to match, and False if not.

    By default sequences only match if they are identical.
    This condition can be loosen by specify a maximum number
    mismatched bases that are allowed.

    An 'N' in either (or both) sequences is automatically
    counted as a mismatched position, except for exact
    matches.

    """
    if max_mismatches == 0:
        return (seq1 == seq2)
    mismatches = 0
    for b1,b2 in zip(seq1,seq2):
        if b1 != b2 or b1 == 'N' or b2 == 'N':
            mismatches += 1
            if mismatches > max_mismatches:
                return False
    return True