如果我有几个带有压缩 zlib 数据的二进制字符串,有没有办法有效地将它们组合成一个压缩字符串而不解压缩所有内容?
我现在必须做的示例:
c1 = zlib.compress("The quick brown fox jumped over the lazy dog. ")
c2 = zlib.compress("We ride at dawn! ")
c = zlib.compress(zlib.decompress(c1)+zlib.decompress(c2)) # Warning: Inefficient!
d1 = zlib.decompress(c1)
d2 = zlib.decompress(c2)
d = zlib.decompress(c)
assert d1+d2 == d # This will pass!
Run Code Online (Sandbox Code Playgroud)
我想要的例子:
c1 = zlib.compress("The quick brown fox jumped over the lazy dog. ")
c2 = zlib.compress("We ride at dawn! ")
c = magic_zlib_add(c1+c2) # Magical method of combining compressed streams
d1 = …Run Code Online (Sandbox Code Playgroud) 我有一个关于Numpy数组内存管理的问题.假设我使用以下内容从缓冲区创建一个numpy数组:
>>> s = "abcd"
>>> arr = numpy.frombuffer(buffer(s), dtype = numpy.uint8)
>>> arr.flags
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : False
WRITEABLE : False
ALIGNED : True
UPDATEIFCOPY : False
>>> del s # What happens to arr?
Run Code Online (Sandbox Code Playgroud)
在上面的情况中,'arr'是否持有's'的引用?如果我删除's',这会释放为's'分配的内存,从而使'arr'可能引用未分配的内存吗?
我还有其他一些问题:
我正在写一个C模块,我遇到了一个我以前从未见过的有趣问题.
// Many other operations before this point
fseek(samples_file, 0, SEEK_SET);
printf("ftell A1 %llu\n", ftell(samples_file));
count = fwrite(channel_buffer+chan_type.size*set_index, 1, chan_type.size, samples_file);
printf("count %llu\n", count);
printf("ftell A2 %llu\n", ftell(samples_file));
// Many more operations to come after this point
Run Code Online (Sandbox Code Playgroud)
当我运行模块时,我得到如下打印输出:
ftell A1 0
count 8
ftell A2 6018
Run Code Online (Sandbox Code Playgroud)
我已将文件指针设置为文件的开头.当我写一些数据时,它应该在我寻找的位置写出数据,然后用写入的字节数增加文件位置(在本例中为8).然而,当我做一个ftell时,似乎该位置突然跳到6018(恰好是文件的原始大小加上8).
为什么会发生这种情况,如何防止这种行为?
我正在尝试编译一个共享对象(最终在带有 ctypes 的 Python 中使用)。用于构建对象的命令行是:
gcc -Wall -O3 -shared -Wl,-soname,borg_stream -lm -m128bit-long-double -fPIC \
-D_FILE_OFFSET_BITS=64 -o borg_stream.so data_stream.c data_types.c \
file_operations.c float_half.c channels.c statistics.c index_stream.c helpers.c
Run Code Online (Sandbox Code Playgroud)
该库在 32 位操作系统上正确构建,并且可以为小文件执行所需的操作。但是,它无法通过大于 4GB 的文件的单元测试。此外,它在执行 fseek/ftell 时将 errno 设置为 EOVERFLOW。但是,如果我 printf sizeof(off_t),它返回 8。如果我 remove -D_FILE_OFFSET_BITS=64,那么它打印 4。所以它似乎-D_FILE_OFFSET_BITS是正确地完成它的工作。
为什么大文件支持仍然不起作用?我究竟做错了什么?