@contextlib.contextmanager
def atomic_open(filename, mode, *args, **kwargs):
    if 'wxa' not in mode[0] or len(mode) > 1 and mode[1] == '+':
        raise ValueError("invalid mode: '{}'".format(mode))
    f = tempfile.NamedTemporaryFile(mode=mode,
                                    prefix=os.path.basename(filename),
                                    dir=os.path.dirname(filename),
                                    suffix='.tmp',
                                    delete=False)
    _discard = [False]
    try:
        if mode[0] == 'a':
            try:
                with open(filename, 'r'+mode[1:], *args, **kwargs) as fin:
                    shutil.copyfileobj(fin, f)
            except (OSError, IOError) as e:
                if e.errno == errno.ENOENT:
                    pass

        def discard(self, _discard=_discard):
            _discard[0] = True
        f.discard = types.MethodType(discard, f)
        yield f
    # ischneider addition to fatomic - catch/raise any exception thrown in with
    # block and force discarding
    except:
        _discard = [True]
        raise
    finally:
        f.close()
        if _discard[0]:
            os.unlink(f.name)
        else:
            _replace_file(f.name, filename)
dataset/ETHPy150Open planetlabs/planet-client-python/planet/api/_fatomic.py/atomic_open/swapped operands of `not in`