code: def open_atomic(
      filename: Filename,
      binary: bool = True,
  ) -> typing.Iterator[typing.IO]:
      """Open a file for atomic writing. Instead of locking this method allows
      you to write the entire file and move it to the actual location. Note that
      this makes the assumption that a rename is atomic on your platform which
      is generally the case but not a guarantee.
  
      http://docs.python.org/library/os.html#os.rename
  
      >>> filename = 'test_file.txt'
      >>> if os.path.exists(filename):
      ...     os.remove(filename)
  
      >>> with open_atomic(filename) as fh:
      ...     written = fh.write(b'test')
      >>> assert os.path.exists(filename)
      >>> os.remove(filename)
  
      >>> import pathlib
      >>> path_filename = pathlib.Path('test_file.txt')
  
      >>> with open_atomic(path_filename) as fh:
      ...     written = fh.write(b'test')
      >>> assert path_filename.exists()
      >>> path_filename.unlink()
      """
      # `pathlib.Path` cast in case `path` is a `str`
      path: pathlib.Path = pathlib.Path(filename)
  
      assert not path.exists(), "%r exists" % path
  
      # Create the parent directory if it doesn't exist
      path.parent.mkdir(parents=True, exist_ok=True)
  
      temp_fh = tempfile.NamedTemporaryFile(
          mode=binary and "wb" or "w",
          dir=str(path.parent),
          delete=False,
      )
      yield temp_fh
      temp_fh.flush()
      os.fsync(temp_fh.fileno())
      temp_fh.close()
      try:
          os.rename(temp_fh.name, path)
      finally:
          with contextlib.suppress(Exception):
              os.remove(temp_fh.name)
  
imports: import abc
  import atexit
  import contextlib
  import logging
  import os
  import pathlib
  import random
  import tempfile
  import time
  import typing
  import warnings
  from .None import constants, exceptions, portalocker
label_desc: Use image node_id 2 for calling a global function with example usage: open_atomic(filename, binary) without return types