import re


def prepare_defines(defines, fmt, excluded = None):
    """Prepare defines for a C++ or Assembly program.
    """
    lines = []
    for name, value in defines.items():
        if excluded and re.match(excluded + '$', name):
            continue
        try:
            lines.append(fmt % (name, value))
        except TypeError:
            pass
    return '\n'.join(lines)