本文整理汇总了Python中pickletools.optimize方法的典型用法代码示例。如果您正苦于以下问题:Python pickletools.optimize方法的具体用法?Python pickletools.optimize怎么用?Python pickletools.optimize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pickletools
的用法示例。
在下文中一共展示了pickletools.optimize方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_optimize_binput_and_memoize
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def test_optimize_binput_and_memoize(self):
pickled = (b'\x80\x04\x95\x15\x00\x00\x00\x00\x00\x00\x00'
b']\x94(\x8c\x04spamq\x01\x8c\x03ham\x94h\x02e.')
# 0: \x80 PROTO 4
# 2: \x95 FRAME 21
# 11: ] EMPTY_LIST
# 12: \x94 MEMOIZE
# 13: ( MARK
# 14: \x8c SHORT_BINUNICODE 'spam'
# 20: q BINPUT 1
# 22: \x8c SHORT_BINUNICODE 'ham'
# 27: \x94 MEMOIZE
# 28: h BINGET 2
# 30: e APPENDS (MARK at 13)
# 31: . STOP
self.assertIn(pickle.BINPUT, pickled)
unpickled = pickle.loads(pickled)
self.assertEqual(unpickled, ['spam', 'ham', 'ham'])
self.assertIs(unpickled[1], unpickled[2])
pickled2 = pickletools.optimize(pickled)
unpickled2 = pickle.loads(pickled2)
self.assertEqual(unpickled2, ['spam', 'ham', 'ham'])
self.assertIs(unpickled2[1], unpickled2[2])
self.assertNotIn(pickle.BINPUT, pickled2)
示例2: dumps
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def dumps(self, arg, proto=0, fast=0):
return pickletools.optimize(pickle.dumps(arg, proto))
示例3: optimize
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def optimize(picklestring):
return picklestring
# There is no HIGHEST_PROTOCOL in cerealizer.
示例4: dumps
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def dumps(obj, protocol=HIGHEST_PROTOCOL):
return zlib.compress(optimize(pickle.dumps(obj)), 9)
示例5: dumps
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def dumps(self, arg, proto=None):
return pickletools.optimize(pickle.dumps(arg, proto))
示例6: test_optimize_long_binget
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def test_optimize_long_binget(self):
data = [str(i) for i in range(257)]
data.append(data[-1])
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(data, proto)
unpickled = pickle.loads(pickled)
self.assertEqual(unpickled, data)
self.assertIs(unpickled[-1], unpickled[-2])
pickled2 = pickletools.optimize(pickled)
unpickled2 = pickle.loads(pickled2)
self.assertEqual(unpickled2, data)
self.assertIs(unpickled2[-1], unpickled2[-2])
self.assertNotIn(pickle.LONG_BINGET, pickled2)
self.assertNotIn(pickle.LONG_BINPUT, pickled2)
示例7: serialize
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def serialize(vector):
""" Serializer a vector using pickle.
:param np.array vector:
:return bytes:
"""
return pickletools.optimize(pickle.dumps(vector, pickle.HIGHEST_PROTOCOL))
示例8: marshall_value
# 需要导入模块: import pickletools [as 别名]
# 或者: from pickletools import optimize [as 别名]
def marshall_value(self, value, storeMemoryMap = False):
"""
Marshalls a Crash object to be used in the database.
By default the C{memoryMap} member is B{NOT} stored here.
@warning: Setting the C{storeMemoryMap} argument to C{True} can lead to
a severe performance penalty!
@type value: L{Crash}
@param value: Object to convert.
@type storeMemoryMap: bool
@param storeMemoryMap: C{True} to store the memory map, C{False}
otherwise.
@rtype: str
@return: Converted object.
"""
if hasattr(value, 'memoryMap'):
crash = value
memoryMap = crash.memoryMap
try:
crash.memoryMap = None
if storeMemoryMap and memoryMap is not None:
# convert the generator to a list
crash.memoryMap = list(memoryMap)
if self.optimizeValues:
value = pickle.dumps(crash, protocol = HIGHEST_PROTOCOL)
value = optimize(value)
else:
value = pickle.dumps(crash, protocol = 0)
finally:
crash.memoryMap = memoryMap
del memoryMap
del crash
if self.compressValues:
value = zlib.compress(value, zlib.Z_BEST_COMPRESSION)
if self.escapeValues:
value = value.encode('hex')
if self.binaryValues:
value = buffer(value)
return value