本文整理匯總了Python中pickle.Pickler方法的典型用法代碼示例。如果您正苦於以下問題:Python pickle.Pickler方法的具體用法?Python pickle.Pickler怎麽用?Python pickle.Pickler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pickle
的用法示例。
在下文中一共展示了pickle.Pickler方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: save
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def save(self, obj):
# Remove the tag.trace attribute from Variable and Apply nodes
if isinstance(obj, theano.gof.utils.scratchpad):
for tag in self.tag_to_remove:
if hasattr(obj, tag):
del obj.__dict__[tag]
# Remove manually-added docstring of Elemwise ops
elif (isinstance(obj, theano.tensor.Elemwise)):
if '__doc__' in obj.__dict__:
del obj.__dict__['__doc__']
return Pickler.save(self, obj)
# Make an unpickler that tries encoding byte streams before raising TypeError.
# This is useful with python 3, in order to unpickle files created with
# python 2.
# This code is taken from Pandas, https://github.com/pydata/pandas,
# under the same 3-clause BSD license.
示例2: save
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def save(self, obj):
if isinstance(obj, (types.MethodType, type({}.pop))):
# the Pickler cannot pickle instance methods; here we decompose
# them into components that make them uniquely identifiable
if hasattr(obj, '__func__'):
func_name = obj.__func__.__name__
else:
func_name = obj.__name__
inst = obj.__self__
if type(inst) == type(pickle):
obj = _MyHash(func_name, inst.__name__)
elif inst is None:
# type(None) or type(module) do not pickle
obj = _MyHash(func_name, inst)
else:
cls = obj.__self__.__class__
obj = _MyHash(func_name, inst, cls)
Pickler.save(self, obj)
示例3: save_global
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def save_global(self, obj, name=None, pack=struct.pack):
# We have to override this method in order to deal with objects
# defined interactively in IPython that are not injected in
# __main__
kwargs = dict(name=name, pack=pack)
if sys.version_info >= (3, 4):
del kwargs['pack']
try:
Pickler.save_global(self, obj, **kwargs)
except pickle.PicklingError:
Pickler.save_global(self, obj, **kwargs)
module = getattr(obj, "__module__", None)
if module == '__main__':
my_name = name
if my_name is None:
my_name = obj.__name__
mod = sys.modules[module]
if not hasattr(mod, my_name):
# IPython doesn't inject the variables define
# interactively in __main__
setattr(mod, my_name, obj)
示例4: test_priming_pickler_memo
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def test_priming_pickler_memo(self):
# Verify that we can set the Pickler's memo attribute.
data = ["abcdefg", "abcdefg", 44]
f = io.BytesIO()
pickler = self.pickler_class(f)
pickler.dump(data)
first_pickled = f.getvalue()
f = io.BytesIO()
primed = self.pickler_class(f)
primed.memo = pickler.memo
primed.dump(data)
primed_pickled = f.getvalue()
self.assertNotEqual(first_pickled, primed_pickled)
示例5: getClient
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def getClient(
cls, servers, debug=0, pickleProtocol=0,
pickler=pickle.Pickler, unpickler=pickle.Unpickler,
pload=None, pid=None
):
if cls.allowTestCache:
return TestClient(
servers, debug=debug,
pickleProtocol=pickleProtocol, pickler=pickler,
unpickler=unpickler, pload=pload, pid=pid)
elif config.Memcached.Pools.Default.ClientEnabled:
return Client(
servers, debug=debug, pickleProtocol=pickleProtocol,
pickler=pickler, unpickler=unpickler, pload=pload, pid=pid)
else:
return None
示例6: RMUserData_adaptToSQLite
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def RMUserData_adaptToSQLite(userData):
if userData == None:
return None
outputStream = StringIO()
pickler = pickle.Pickler(outputStream)
pickler.dump(userData)
return outputStream.getvalue()
示例7: RMParserParams_adaptToSQLite
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def RMParserParams_adaptToSQLite(params):
if params == None:
return None
outputStream = StringIO()
pickler = pickle.Pickler(outputStream)
pickler.dump(params)
return outputStream.getvalue()
示例8: __setitem__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def __setitem__(self, key, value):
if self.writeback:
self.cache[key] = value
f = StringIO()
p = Pickler(f, self._protocol)
p.dump(value)
self.dict[key] = f.getvalue()
示例9: dumps
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def dumps(self, arg, proto=0, fast=0):
f = StringIO()
p = pickle.Pickler(f, proto)
if fast:
p.fast = fast
p.dump(arg)
f.seek(0)
return f.read()
示例10: test_pers_load
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def test_pers_load(self):
for binary in [True, False]:
src = StringIO()
p = cPickle.Pickler(src)
p.persistent_id = persistent_id
p.binary = binary
value = MyData('abc')
p.dump(value)
up = cPickle.Unpickler(StringIO(src.getvalue()))
up.persistent_load = persistent_load
res = up.load()
self.assertEqual(res.value, value.value)
# errors
src = StringIO()
p = cPickle.Pickler(src)
p.persistent_id = persistent_id
p.binary = binary
value = MyData('abc')
p.dump(value)
up = cPickle.Unpickler(StringIO(src.getvalue()))
# exceptions vary betwee cPickle & Pickle
try:
up.load()
self.assertUnreachable()
except Exception, e:
pass
示例11: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def __init__(self, file, protocol=0, extra_tag_to_remove=None):
# Can't use super as Pickler isn't a new style class
Pickler.__init__(self, file, protocol)
self.tag_to_remove = ['trace', 'test_value']
if extra_tag_to_remove:
self.tag_to_remove.extend(extra_tag_to_remove)
示例12: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def __init__(self, hash_name='md5'):
self.stream = io.BytesIO()
# By default we want a pickle protocol that only changes with
# the major python version and not the minor one
protocol = (pickle.DEFAULT_PROTOCOL if PY3_OR_LATER
else pickle.HIGHEST_PROTOCOL)
Pickler.__init__(self, self.stream, protocol=protocol)
# Initialise the hash obj
self._hash = hashlib.new(hash_name)
示例13: memoize
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def memoize(self, obj):
# We want hashing to be sensitive to value instead of reference.
# For example we want ['aa', 'aa'] and ['aa', 'aaZ'[:2]]
# to hash to the same value and that's why we disable memoization
# for strings
if isinstance(obj, _bytes_or_unicode):
return
Pickler.memoize(self, obj)
# The dispatch table of the pickler is not accessible in Python
# 3, as these lines are only bugware for IPython, we skip them.
示例14: _batch_setitems
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import Pickler [as 別名]
def _batch_setitems(self, items):
# forces order of keys in dict to ensure consistent hash.
try:
# Trying first to compare dict assuming the type of keys is
# consistent and orderable.
# This fails on python 3 when keys are unorderable
# but we keep it in a try as it's faster.
Pickler._batch_setitems(self, iter(sorted(items)))
except TypeError:
# If keys are unorderable, sorting them using their hash. This is
# slower but works in any case.
Pickler._batch_setitems(self, iter(sorted((hash(k), v)
for k, v in items)))