用于创建和包装类似文件的对象的类
filelike的Python项目详细描述
file like:创建和处理类文件对象的python模块。
此模块负责实现和操作的基础工作 提供丰富的类文件接口的对象,包括读、写, 寻找和迭代。它还提供了许多有用的类 最重要的功能。
主要类是filelikebase,它实现了整个类文件接口 在基本的read()、write()、seek()、tell()和truncate()方法之上。 子类可以实现任何或所有这些方法来获取 更高级别的文件行为。
它还提供一些漂亮的文件处理功能:
open: mirrors the standard open() function but is much cleverer; URLs are automatically fetched, .bz2 files are transparently decompressed, and so-on. join: concatenate multiple file-like objects together so that they act like a single file. slice: access a section of a file-like object as if it were an independent file.
“wrappers”子包包含构建在 这个框架的顶部。其中包括:
Translate: pass file contents through an arbitrary translation function (e.g. compression, encryption, …) Decrypt: on-the-fly reading and writing to an encrypted file (using PEP272 cipher API) UnBZip2: on-the-fly decompression of bzip’d files (like the standard library’s bz2 module, but accepts any file-like object)
作为这个模块设计要实现的类型的一个例子,下面是 如何使用解密包装器透明地访问加密的 文件:
# Create the decryption key from Crypto.Cipher import DES cipher = DES.new('abcdefgh',DES.MODE_ECB) # Open the encrypted file from filelike.wrappers import Decrypt f = Decrypt(file("some_encrypted_file.bin","r"),cipher)
“f”中的对象现在表现为类似文件的对象,可以透明地解密 在读取文件的过程中,文件处于动态状态。
“pipeline”子包包含用于组合这些包装的工具 以unix管道的形式。在下面的示例中,“f”将读取 加密文件的前五行:
from filelike.pipeline import Decrypt, Head f = file("some_encrypted_file.bin") > Decrypt(cipher) | Head(lines=5)
最后,当代码希望处理 类文件对象:
is_filelike(obj): checks that an object is file-like to_filelike(obj): wraps a variety of objects in a file-like interface