本文整理汇总了Python中bz2.BZ2Compressor.compress方法的典型用法代码示例。如果您正苦于以下问题:Python BZ2Compressor.compress方法的具体用法?Python BZ2Compressor.compress怎么用?Python BZ2Compressor.compress使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bz2.BZ2Compressor
的用法示例。
在下文中一共展示了BZ2Compressor.compress方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testCompress
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompress(self):
# "Test BZ2Compressor.compress()/flush()"
bz2c = BZ2Compressor()
self.assertRaises(TypeError, bz2c.compress)
data = bz2c.compress(self.TEXT)
data += bz2c.flush()
self.assertEqual(self.decompress(data), self.TEXT)
示例2: testCompressEmptyString
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompressEmptyString(self):
# "Test BZ2Compressor.compress()/flush() of empty string"
bz2c = BZ2Compressor()
data = bz2c.compress('')
data += bz2c.flush()
self.assertEqual(data, self.EMPTY_DATA)
示例3: testCompressChunks10
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompressChunks10(self):
# "Test BZ2Compressor.compress()/flush() with chunks of 10 bytes"
bz2c = BZ2Compressor()
n = 0
data = ''
while 1:
str = self.TEXT[n*10:(n+1)*10]
if not str:
break
data += bz2c.compress(str)
n += 1
data += bz2c.flush()
self.assertEqual(self.decompress(data), self.TEXT)
示例4: testBigmem
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testBigmem(self, size):
text = "a" * size
bz2c = bz2.BZ2Compressor()
data = bz2c.compress(text) + bz2c.flush()
del text
text = self.decompress(data)
self.assertEqual(len(text), size)
self.assertEqual(text.strip("a"), "")
示例5: testCompressBigmem
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompressBigmem(self, size):
text = "a" * size
data = bz2.compress(text)
del text
text = self.decompress(data)
self.assertEqual(len(text), size)
self.assertEqual(text.strip("a"), "")
示例6: testDecompressBigmem
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testDecompressBigmem(self, size):
# Issue #14398: decompression fails when output data is >=2GB.
if size < _4G:
self.skipTest("Test needs 5GB of memory to run.")
compressed = bz2.compress("a" * _4G)
text = bz2.decompress(compressed)
self.assertEqual(len(text), _4G)
self.assertEqual(text.strip("a"), "")
示例7: testWriteNonDefaultCompressLevel
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testWriteNonDefaultCompressLevel(self):
expected = bz2.compress(self.TEXT, compresslevel=5)
with BZ2File(self.filename, "w", compresslevel=5) as bz2f:
bz2f.write(self.TEXT)
with open(self.filename, "rb") as f:
self.assertEqual(f.read(), expected)
示例8: testDecompressLimited
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testDecompressLimited(self):
"""Decompressed data buffering should be limited"""
bomb = bz2.compress(bytes(int(2e6)), compresslevel=9)
self.assertLess(len(bomb), _compression.BUFFER_SIZE)
decomp = BZ2File(BytesIO(bomb))
self.assertEqual(bytes(1), decomp.read(1))
max_decomp = 1 + DEFAULT_BUFFER_SIZE
self.assertLessEqual(decomp._buffer.raw.tell(), max_decomp,
"Excessive amount of data was decompressed")
# Tests for a BZ2File wrapping another file object:
示例9: testCompress
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompress(self):
bz2c = BZ2Compressor()
self.assertRaises(TypeError, bz2c.compress)
data = bz2c.compress(self.TEXT)
data += bz2c.flush()
self.assertEqual(self.decompress(data), self.TEXT)
示例10: testCompressEmptyString
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompressEmptyString(self):
bz2c = BZ2Compressor()
data = bz2c.compress(b'')
data += bz2c.flush()
self.assertEqual(data, self.EMPTY_DATA)
示例11: testCompressChunks10
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testCompressChunks10(self):
bz2c = BZ2Compressor()
n = 0
data = b''
while True:
str = self.TEXT[n*10:(n+1)*10]
if not str:
break
data += bz2c.compress(str)
n += 1
data += bz2c.flush()
self.assertEqual(self.decompress(data), self.TEXT)
示例12: testDecompress4G
# 需要导入模块: from bz2 import BZ2Compressor [as 别名]
# 或者: from bz2.BZ2Compressor import compress [as 别名]
def testDecompress4G(self, size):
# "Test BZ2Decompressor.decompress() with >4GiB input"
blocksize = 10 * 1024 * 1024
block = random.getrandbits(blocksize * 8).to_bytes(blocksize, 'little')
try:
data = block * (size // blocksize + 1)
compressed = bz2.compress(data)
bz2d = BZ2Decompressor()
decompressed = bz2d.decompress(compressed)
self.assertTrue(decompressed == data)
finally:
data = None
compressed = None
decompressed = None