當前位置: 首頁>>代碼示例>>Python>>正文


Python tempfile.html方法代碼示例

本文整理匯總了Python中tempfile.html方法的典型用法代碼示例。如果您正苦於以下問題:Python tempfile.html方法的具體用法?Python tempfile.html怎麽用?Python tempfile.html使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在tempfile的用法示例。


在下文中一共展示了tempfile.html方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: safe_tempfile_path

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def safe_tempfile_path():
    # This gets a valid temporary file path in the safest possible way, although there is still no
    # guarantee that another process will not create a file at this path. The NamedTemporaryFile is
    # deleted when the context manager exits and the file object is closed.
    #
    # This is preferable to using NamedTemporaryFile as a context manager and passing the name
    # attribute of the file object around because NamedTemporaryFiles cannot be opened a second time
    # if already open on Windows NT or later:
    # https://docs.python.org/3.8/library/tempfile.html#tempfile.NamedTemporaryFile
    # https://github.com/dagster-io/dagster/issues/1582
    with tempfile.NamedTemporaryFile() as fd:
        path = fd.name

    try:
        yield Path(path).as_posix()

    finally:
        if os.path.exists(path):
            os.unlink(path) 
開發者ID:dagster-io,項目名稱:dagster,代碼行數:21,代碼來源:__init__.py

示例2: workspace_dir_option

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def workspace_dir_option(command: Callable[..., None]) -> Callable[..., None]:
    """
    An option decorator for the workspace directory.
    """
    help_text = (
        'Creating a cluster can use approximately 2 GB of temporary storage. '
        'Set this option to use a custom "workspace" for this temporary '
        'storage. '
        'See '
        'https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir '
        'for details on the temporary directory location if this option is '
        'not set.'
    )
    function = click.option(
        '--workspace-dir',
        type=click_pathlib.Path(
            exists=True,
            dir_okay=True,
            file_okay=False,
            resolve_path=True,
        ),
        callback=get_workspace_dir,
        help=help_text,
    )(command)  # type: Callable[..., None]
    return function 
開發者ID:dcos,項目名稱:dcos-e2e,代碼行數:27,代碼來源:workspaces.py

示例3: download_and_transform_erf

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def download_and_transform_erf(self, partner_id=None):
  """Load and Transform ERF files to Newline Delimeted JSON.

  Then upload this file to the project GCS.

  Args:
    self: The operator this is being used in.
    partner_id: A string of the DCM id of the partner.

  Returns:
    entity_read_file_ndj: The filename for the converted entity read file.
  """
  if partner_id:
    self.erf_bucket = 'gdbm-%s' % partner_id
  else:
    self.erf_bucket = 'gdbm-public'

  gcs_hook = GoogleCloudStorageHook(google_cloud_storage_conn_id=self.gcp_conn_id)
  entity_read_file = tempfile.NamedTemporaryFile(delete=False)
  gcs_hook.download(self.erf_bucket, self.erf_object, entity_read_file.name)
  temp_file = None
  # Creating temp file. Not using the delete-on-close functionality
  # as opening the file for reading while still open for writing
  # will not work on all platform
  # https://docs.python.org/2/library/tempfile.html#tempfile.NamedTemporaryFile
  try:
    temp_file = tempfile.NamedTemporaryFile(mode='w', delete=False)
    temp_file.writelines(json_to_jsonlines(entity_read_file.name))
    temp_file.close()
    # Random here used as a nonce for writing multiple files at once.
    filename = '%s_%s_%d.json' % (randint(1, 1000000), self.entity_type,
                                  time.time() * 1e+9)
    gcs_hook.upload(self.gcs_bucket, filename, temp_file.name)

  finally:
    if temp_file:
      temp_file.close()
    os.unlink(temp_file.name)

  return filename 
開發者ID:google,項目名稱:orchestra,代碼行數:42,代碼來源:erf_utils.py

示例4: safe_isfile

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def safe_isfile(path):
    '''"Backport of Python 3.8 os.path.isfile behavior.

    This is intended to backport https://docs.python.org/dev/whatsnew/3.8.html#os-path. I'm not
    sure that there are other ways to provoke this behavior on Unix other than the null byte,
    but there are certainly other ways to do it on Windows. Afaict, we won't mask other
    ValueErrors, and the behavior in the status quo ante is rough because we risk throwing an
    unexpected, uncaught ValueError from very deep in our logic.
    '''
    try:
        return os.path.isfile(path)
    except ValueError:
        return False 
開發者ID:dagster-io,項目名稱:dagster,代碼行數:15,代碼來源:__init__.py

示例5: __readonly__

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def __readonly__(self, *args, **kwargs):
        raise RuntimeError("Cannot modify ReadOnlyDict")

    # https://docs.python.org/3/library/pickle.html#object.__reduce__
    #
    # For a dict, the default behavior for pickle is to iteratively call __setitem__ (see 5th item
    #  in __reduce__ tuple). Since we want to disable __setitem__ and still inherit dict, we
    # override this behavior by defining __reduce__. We return the 3rd item in the tuple, which is
    # passed to __setstate__, allowing us to restore the frozendict. 
開發者ID:dagster-io,項目名稱:dagster,代碼行數:11,代碼來源:__init__.py

示例6: get_multiprocessing_context

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def get_multiprocessing_context():
    # Set execution method to spawn, to avoid fork and to have same behavior between platforms.
    # Older versions are stuck with whatever is the default on their platform (fork on
    # Unix-like and spawn on windows)
    #
    # https://docs.python.org/3/library/multiprocessing.html#multiprocessing.get_context
    if hasattr(multiprocessing, 'get_context'):
        return multiprocessing.get_context('spawn')
    else:
        return multiprocessing 
開發者ID:dagster-io,項目名稱:dagster,代碼行數:12,代碼來源:__init__.py

示例7: TemporaryFileWithContents

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def TemporaryFileWithContents(contents, **kw):
  """A contextmanager that writes out a string to a file on disk.

  This is useful whenever you need to call a function or command that expects a
  file on disk with some contents that you have in memory. The context manager
  abstracts the writing, flushing, and deletion of the temporary file. This is a
  common idiom that boils down to a single with statement.

  Note:  if you need a temporary file-like object for calling an internal
  function, you should use a StringIO as a file-like object and not this.
  Temporary files should be avoided unless you need a file name or contents in a
  file on disk to be read by some other function or program.

  Args:
    contents: a string with the contents to write to the file.
    **kw: Optional arguments passed on to tempfile.NamedTemporaryFile.
  Yields:
    The temporary file object, opened in 'w' mode.

  """
  temporary_file = tempfile.NamedTemporaryFile(**kw)
  temporary_file.write(contents)
  temporary_file.flush()
  yield temporary_file
  temporary_file.close()


# TODO(user): remove after migration to Python 3.2.
# This context manager can be replaced with tempfile.TemporaryDirectory in
# Python 3.2 (http://bugs.python.org/issue5178,
# http://docs.python.org/dev/library/tempfile.html#tempfile.TemporaryDirectory). 
開發者ID:google,項目名稱:google-apputils,代碼行數:33,代碼來源:file_util.py

示例8: __call__

# 需要導入模塊: import tempfile [as 別名]
# 或者: from tempfile import html [as 別名]
def __call__(self, checkpoint: Mapping, filename: str, metadata: Optional[Mapping] = None) -> None:
        # wont work on XLA

        with tempfile.NamedTemporaryFile() as tmp:
            # we can not use tmp.name to open tmp.file twice on Win32
            # https://docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
            torch.save(checkpoint, tmp.file)
            self._logger.log_artifact(tmp.name, filename) 
開發者ID:pytorch,項目名稱:ignite,代碼行數:10,代碼來源:neptune_logger.py


注:本文中的tempfile.html方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。