将tempfile包装起来,为您提供pathlib.Path。
项目描述
temppathlib
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 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 993356dce9544b432a6202227640fc8b5aced3fbd828dff0f5d5ebaac2330f58 |
|
MD5 | 1bbe0ac2c4c6d83bb59fb8fa3f20bd0b |
|
BLAKE2b-256 | 9125f852f068b152d92f41351f0b2d5ea834f3b654d98eb9aaedaf28addf28e5 |