跳转到主要内容

将tempfile包装起来,为您提供pathlib.Path。

项目描述

temppathlib

Check status Test coverage PyPI - version PyPI - Python Version

Temppathlib提供围绕tempfile的包装,以便您可以直接与pathlib模块一起使用。我们发现每次需要临时文件时,手动将tempfile对象转换为pathlib.Path都很繁琐。

此外,我们还提供

  • 一个上下文管理器removing_tree,它检查路径是否存在,并通过包装shutil.rmtree递归地删除它。

  • 一个上下文管理器 TmpDirIfNecessary,当未指定目录时创建一个临时目录,否则使用提供的目录。这在程序完成后想要保留一些临时文件以供检查时非常有用。我们通常指定一个可选的 --operation_dir 命令行参数并传递其值给 TmpDirIfNecessary

如果您需要一个更复杂的库来从字符串路径转换到 pathlib.Path,请查看 ruamel.std.pathlib

用法

import pathlib

import temppathlib

# create a temporary directory
with temppathlib.TemporaryDirectory() as tmp_dir:
    tmp_pth = tmp_dir.path / "some-filename.txt"
    # do something else with tmp_dir ...

# create a temporary file
with temppathlib.NamedTemporaryFile() as tmp:
    # write to it
    tmp.file.write('hello'.encode())
    tmp.file.flush()

    # you can use its path.
    target_pth = pathlib.Path('/some/permanent/directory') / tmp.path.name

# create a temporary directory only if necessary
operation_dir = pathlib.Path("/some/operation/directory)
with temppathlib.TmpDirIfNecessary(path=operation_dir) as op_dir:
    # do something with the operation directory
    pth = op_dir.path / "some-file.txt"

    # operation_dir is not deleted since 'path' was specified.


with temppathlib.TmpDirIfNecessary() as op_dir:
    # do something with the operation directory
    pth = op_dir.path / "some-file.txt"

    # op_dir is deleted since 'path' argument was not specified.

# context manager to remove the path recursively
pth = pathlib.Path('/some/directory')
with temppathlib.removing_tree(pth):
    # do something in the directory ...
    pass

安装

  • 创建虚拟环境

python3 -m venv venv3
  • 激活它

source venv3/bin/activate
  • 使用pip安装temppathlib

pip3 install temppathlib

开发

  • 查看存储库。

  • 在存储库根目录下,创建虚拟环境

python3 -m venv venv3
  • 激活虚拟环境

source venv3/bin/activate
  • 安装开发依赖项

pip3 install -e .[dev]
  • 我们使用tox进行测试和打包分发。假设已经激活了虚拟环境并且安装了开发依赖项,运行

tox
  • 我们还提供了一套预提交检查,用于代码格式化和检查。在已激活的开发依赖项的虚拟环境中本地运行它们

./precommit.py
  • 预提交脚本还可以自动格式化代码

./precommit.py  --overwrite

版本控制

我们遵循 语义版本控制。版本 X.Y.Z 表示

  • X 是主版本(不兼容回滚),

  • Y 是次要版本(兼容回滚),

  • Z 是补丁版本(兼容回滚的 bug 修复)。

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定选择哪个,请了解有关 安装软件包 的更多信息。

源分发

temppathlib-1.2.0.tar.gz (6.3 kB 查看哈希)

上传时间

由以下组织支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面