@contextmanager
def make_file(contents, suffix, decompress=False):
    if decompress:
        contents = zlib.decompress(contents)

    try:
        _, fname = tempfile.mkstemp(suffix=suffix)
        with open(fname, 'wb') as outfile:
            outfile.write(contents)
        yield fname
    finally:
        try:
            os.unlink(fname)
        except WindowsError:  # on Windows the unlink can fail
            pass
Context manager to write data to a temporary file,
dataset/ETHPy150Open glue-viz/glue/glue/tests/helpers.py/make_file