在测试期间保留文件和目录的内容。
fileguard的Python项目详细描述
文件保护:保留文件和目录的内容
在一定范围内保护文件和目录的内容。
在该范围内,可以根据需要更改文件/目录的内容。
作用域结束后,文件的原始内容和/或
将还原目录。"scope"可以是带块的或
功能/方法。
下面是保护allure.txt
文件内容的示例。
在with guard('allure.txt')
块中,文件的内容是
改变。但是,在其作用域结束后,文件的前一个内容是
恢复:
>>>fromfileguardimportguard>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore>>>withguard('allure.txt'):withopen('allure.txt','w')asf:f.write('Still Dre Day')withopen('allure.txt','r')asf:print(f.read())StillDreDay>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore
安装
要安装fileguard,只需使用pip:
pip install fileguard
要求
- python
3.6+
。它应该也适用于其他python3.x
版本。
此库没有外部依赖项。
fileguard何时何地有用?< >
这个库在各种场景中都很有用,特别是在测试中,因为 允许您保存大量样板文件和容易出错的代码。
例如, 假设您正在开发的程序读写 配置文件。在测试中,手动创建配置文件 然后要测试您的程序是否在预期的 方式:
在其中一个测试函数中,您希望在 用无效数据更新配置文件,然后 你的程序读取它。在另一个测试函数中,您要测试 如果删除配置文件,程序不会崩溃。另一个测试 函数测试更新已删除配置文件的情况。又一次 要测试更改配置内容的 文件按预期工作。你只想测试你的程序 在每个配置文件的范围内对配置文件的更改做出响应 测试功能。在那之后,你需要原稿的内容 要还原的配置文件。
如果手动完成,您有两个选项:
- 编写样板代码以备份和还原文件内容 在每个测试函数之前和之后,分别
- 有很多配置文件的副本(一个用于无效数据,一个用于 空数据,一个不存在的数据等)。最后,你还是会 保留还原已编辑/删除文件内容的任务
请记住,上面描述的示例相当简单。如果:< /P>
- 要保留多个文件的内容
- 你想保存的内容,不仅仅是一个文件,而是整个 目录
- 您处理的不是
utf-8
编码文本文件,而是 二进制文件、音乐文件、图像等 - 希望在函数内部的作用域中保留文件的内容 (例如,在带有块的内)而不是整个函数的
所有这些都增加了易出错样板代码的复杂性,并使 它脏,复杂,难以维护和混淆。
fileguard库允许您彻底解决这些问题,
蟒蛇的方式。您所要做的就是用@guard
API文档和示例
在下面的文本中,"fileguarded"表示 将保留文件和/或目录。换句话说,他们 作用域结束后的内容将与正确的内容相同 在范围开始之前。
要使用fileguard,只需导入guard
:
fromfileguardimportguard
防护罩
可用作:
函数/方法装饰器
- 函数的范围将由文件保护
@guard('my_file.txt')defchange_my_file():# Within this function, change the contents of the file as you wish.# You can even delete it.# After the function has executed, 'my_file.txt's contents will be# the same as they were right before the execution of this function.
类装饰符
- 我们所有人er定义的方法将被文件保护。 以下两个代码段是等效的:
@guard('my_file.txt')classMyClass(object):def__init__(self,my_arg_1,my_arg_2):self._my_arg_1=my_arg_1self._my_arg_2=my_arg_2defmy_method_1(self):# code heredefmy_method_2(self):# code here
classMyClass(object):def__init__(self,my_arg_1,my_arg_2):self._my_arg_1=my_arg_1self._my_arg_2=my_arg_2@guard('my_file.txt')defmy_method_1(self):# code here@guard('my_file.txt')defmy_method_2(self):# code here
上下文管理器
- 带有块的
中的所有代码都有文件保护,包括 调用更改文件保护的内容的函数 文件和目录。
withguard('my_file.txt'):# Within "with" block, change the contents of the file as you wish.# You can even delete it.# After the "with" scope has ended, 'my_file.txt's contents will be# the same as they were right before the execution of this with block.
- 带有块的
如果文件/目录被删除了怎么办?
即使您删除文件,也会还原这些文件。
目录的完整内容将被还原,即使您 删除整个目录或其中的一些文件。
参数
guard()
接受fileguard的文件和目录列表。它可以采取
一个参数:
@guard('file.txt')defmy_function(arg1,arg2):# code here
或多个:
@guard('file_1.txt','file_2.txt')defmy_function(arg1,arg2):# code here
上面的代码片段相当于:
>>>fromfileguardimportguard>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore>>>withguard('allure.txt'):withopen('allure.txt','w')asf:f.write('Still Dre Day')withopen('allure.txt','r')asf:print(f.read())StillDreDay>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore0
在这种情况下,作为参数传递的所有文件的内容将是 文件保护。
您可以将文件、目录或它们的混合作为参数传递:
>>>fromfileguardimportguard>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore>>>withguard('allure.txt'):withopen('allure.txt','w')asf:f.write('Still Dre Day')withopen('allure.txt','r')asf:print(f.read())StillDreDay>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore1
在这种情况下,文件的内容/file_1.txt
,directory/directory_1
,
目录/home/iluxonchik/directory_2
和文件/home/iluxonchik/file_2.txt
会有文件保护。
文件保护的文件和目录在修饰时不需要存在。
您可以在函数、方法或类中对文件或目录进行文件保护(使用guard()
对其进行修饰)。
还没有那个文件或目录。你必须确保文件
执行修饰的函数或方法时存在。
但是,如果使用guard()
作为上下文管理器,则必须确保
使用时受保护的文件或目录确实存在
带防护装置():
支持的文件类型
支持任何文件类型。你可以保护文本文件,二进制文件,音乐 原始文件,原始文件 由其副本备份。
目录
就像文件一样,目录可以包含任意文件。原文 目录及其内容将被还原。在引擎盖下,原来的 目录由其所有内容的副本备份。
文件保护函数调用文件保护函数(嵌套调用)
保留备份顺序。在内部,使用堆栈。最好的 举例说明这一点。
假设您有一个文件 内容:
>>>fromfileguardimportguard>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore>>>withguard('allure.txt'):withopen('allure.txt','w')asf:f.write('Still Dre Day')withopen('allure.txt','r')asf:print(f.read())StillDreDay>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore2
另外,fileguard\u demo.py
内容如下:
>>>fromfileguardimportguard>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore>>>withguard('allure.txt'):withopen('allure.txt','w')asf:f.write('Still Dre Day')withopen('allure.txt','r')asf:print(f.read())StillDreDay>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore3
运行python fileguard_demo.py的输出如下:
>>>fromfileguardimportguard>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore>>>withguard('allure.txt'):withopen('allure.txt','w')asf:f.write('Still Dre Day')withopen('allure.txt','r')asf:print(f.read())StillDreDay>>>withopen('allure.txt','r')asf:print(f.read())TheallureofbreakingthelawWasalwaystoomuchformetoeverignore4