The python binding for base16384
- 编码/解码文本
>>>importpybase16384aspybs>>>pybs.encode_string('hello!!')
'栙擆羼漡'>>>pybs.decode_string('栙擆羼漡')
'hello!!'- 编码文件
fromioimportBytesIOimportpybase16384aspybswithopen("input.pcm", "rb") asf:
data=f.read()
foriinrange(1):
pybs.encode_file(BytesIO(data), open("output2.pcm", 'wb'), True)- 解码文件
fromioimportBytesIOimportpybase16384aspybswithopen("output2.pcm", "rb") asf:
data=f.read()
foriinrange(1):
pybs.decode_file(BytesIO(data), open("input2.pcm", 'wb'))fromtypingimportIOdefencode_len(dlen: int) ->int: ...
defdecode_len(dlen: int, offset: int) ->int: ...
ENCBUFSZ: intDECBUFSZ: intFLAG_NOHEADER: intFLAG_SUM_CHECK_ON_REMAIN: intFLAG_DO_SUM_CHECK_FORCELY: intdefis_64bits() ->bool: ...
defencode_file(input: IO, output: IO, write_head: bool= ..., buf_rate: int= ...): ...
defencode_file_safe(input: IO, output: IO, write_head: bool= ..., buf_rate: int= ...): ...
defdecode_file(input: IO, output: IO, buf_rate: int= ...): ...
defdecode_file_safe(input: IO, output: IO, buf_rate: int= ...): ...
defensure_bytes(inp) ->bytes: ...
defencode_local_file(inp, out) ->None: ...
defdecode_local_file(inp, out) ->None: ...
defencode_fd(inp: int, out: int) ->None: ...
defdecode_fd(inp: int, out: int) ->None: ...
defencode_local_file_detailed(inp, out, flag: int) ->None: ...
defdecode_local_file_detailed(inp, out, flag: int) ->None: ...
defencode_fd_detailed(inp: int, out: int, flag: int) ->None: ...
defdecode_fd_detailed(inp: int, out: int, flag: int) ->None: ...
defencode(data: bytes) ->bytes: ...
defencode_safe(data: bytes) ->bytes: ...
defdecode(data: bytes) ->bytes: ...
defdecode_safe(data: bytes) ->bytes: ...
defencode_from_string(data: str, write_head: bool= ...) ->bytes: ...
defencode_from_string_safe(data: str, write_head: bool= ...) ->bytes: ...
defencode_to_string(data: bytes) ->str: ...
defencode_to_string_safe(data: bytes) ->str: ...
defencode_string(data: str) ->str: ...
defencode_string_safe(data: str) ->str: ...
defdecode_from_bytes(data: bytes) ->str: ...
defdecode_from_bytes_safe(data: bytes) ->str: ...
defdecode_from_string(data: str) ->bytes: ...
defdecode_from_string_safe(data: str) ->bytes: ...
defdecode_string(data: str) ->str: ...
defdecode_string_safe(data: str) ->str: ...
defencode_stream_detailed(inp, out, flag: int): ...
defdecode_stream_detailed(inp, out, flag: int): ...write_head将显式指明编码出的文本格式(utf16be),以便文本编辑器(如记事本)能够正确渲染,一般在写入文件时使用。
buf_rate指定读取文件的策略。当它为n时,则表示一次读取7n或者8n个字节。如果读到的字节长度小于预期,则说明长度不够, 此时,n将减半,恢复文件指针,重新读取。如果当n=1时长度仍然不够,就地encode/decode处理之。
encode_len和decode_len用于计算输出的长度
- 他们直接来自底层的C库,高性能,但是一般不需要在外部使用(除非是增加性能)
def_encode(data: BufferProtocol) ->bytes: ...
def_encode_safe(data: BufferProtocol) ->bytes: ...
def_decode(data: BufferProtocol) ->bytes: ...
def_decode_safe(data: BufferProtocol) ->bytes: ...
def_encode_into(data: BufferProtocol, dest: BufferProtocol) ->int: ...
def_encode_into_safe(data: BufferProtocol, dest: BufferProtocol) ->int: ...
def_decode_into(data: BufferProtocol, dest: BufferProtocol) ->int: ...
def_decode_into_safe(data: BufferProtocol, dest: BufferProtocol) ->int: ...
defis_64bits() ->bool: ..._decode在解码b'='开头的数据时不安全:解释器异常_encode_into和_decode_into直接操作缓冲区对象的底层指针,0拷贝,当然也和上面一样的问题,他们是没有检查的
融合了 CFFI 版本的成果,现在一个包可以同时在cpython和pypy上运行
python -m pip install setuptools wheel cython cffi
git clone https://github.com/synodriver/pybase16384
cd pybase16384
git submodule update --init --recursive
python setup.py bdist_wheel --use-cython --use-cffi
- 为了在windows上编译,需要加点料,把 这个 放进msvc的目录
默认由py实现决定,在cpython上自动选择cython后端,在pypy上自动选择cffi后端,使用B14_USE_CFFI环境变量可以强制选择cffi