用于创建和包装类似文件的对象的类

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

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java接口中的每个方法都是抽象的,但在抽象类中,我们也只能使用抽象方法   初始化Java中声明的、未初始化的变量会发生什么情况?   java BouncyCastle openPGP将字节[]数组加密为csv文件   在Java中将类A(和所有子类)映射到类B的实例的字典   RSA公钥编码,在Java和Android中,代码相同,结果不同   java在安卓中实现数字检测语音识别   java取消选择复选框   java如何在其他配置中重用Maven配置XML片段   java有没有一种有效的方法来检查HashMap是否包含映射到相同值的键?   spring处理程序调度失败;嵌套的例外是java。lang.NoClassDefFoundError:org/apache/http/client/HttpClient   带有ehcache的java多层缓存   java如何访问chromium(或任何其他浏览器)cookie   java通过将两个集合与spring data mongodb data中的条件合并来获取计数   安卓中R.java的语法错误