python 3的超快速数据序列化
tnetstring3的Python项目详细描述
这是一个数据序列化库。它很像json,但它使用 称为“typed netstrings”的新语法,zed建议在 Mongrel2网络服务器。它被设计成更简单更容易实现 比json更快,在很多情况下也更快。
普通的netstring是一个数据块,其前缀是长度,后缀是 用一个正确的逗号。字符串“hello world”编码如下:
11:hello world,
类型化的netstring通过用类型标记替换逗号来添加其他数据类型。 下面是编码为tnetstring的整数12345:
5:12345#
下面的列表[12345,true,0]混合了整数和布尔值:
19:5:12345#4:true!1:0#]
够简单吗?本模块提供以下功能:
dump: dump an object as a tnetstring to a file dumps: dump an object as a tnetstring to a string load: load a tnetstring-encoded object from a file loads: load a tnetstring-encoded object from a string pop: pop a tnetstring-encoded object from the front of a string
注意,由于解析tnetstring需要将所有数据读入内存 同时,使用这些基于文件的版本不会提高效率 功能。它们只在这里,所以您可以使用load()精确地读取一个 不消耗任何额外数据的文件或套接字中的项。
tnetstrings规范明确声明字符串是二进制blob 禁止在协议级别使用Unicode。为了方便 python程序员,这个库允许您指定应用程序级编码 要将python的unicode字符串转换为二进制blob或从二进制blob转换为unicode字符串,请执行以下操作:
>>> print repr(tnetstring.loads("2:\xce\xb1,")) '\xce\xb1' >>> >>> print repr(tnetstring.loads("2:\xce\xb1,", "utf8")) u'\u03b1'
Copyright: |
|
---|---|
Copyright: |
|
License: | MIT, see LICENCE for more details. |