def get_filename(metadata, directory=None, ext="txt"):
    """Construct data file name from a metadata dictionary.

    Returns the file name, as a string.
    """
    metadata = metadata.copy()
    name = metadata.pop("name", "data")
    directory = metadata.pop("directory", directory)
    try:
        timestamp = metadata.pop("timestamp")
    except __HOLE__:
        tstring = ""
    else:
        tstring = timestamp.strftime("+%Y%m%d%H%M%S")

    mmd = []
    for key in sorted(metadata.keys()):
        value = metadata[key]
        if type(value) is str:
            value = value.upper()
        mmd.append("%s%s" % (key.lower(), value))

    fname = "%s%s+%s.%s" % ( name, tstring, "+".join(mmd), ext)
    if directory:
        return os.path.join(directory, fname)
    else:
        return fname
dataset/ETHPy150Open kdart/pycopia/QA/pycopia/datafile.py/get_filename