本文整理汇总了Python中cPickle.dumps方法的典型用法代码示例。如果您正苦于以下问题:Python cPickle.dumps方法的具体用法?Python cPickle.dumps怎么用?Python cPickle.dumps使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类cPickle
的用法示例。
在下文中一共展示了cPickle.dumps方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _augment_images_worker
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def _augment_images_worker(self, augseq, queue_source, queue_result):
"""Worker function that endlessly queries the source queue (input
batches), augments batches in it and sends the result to the output
queue."""
while True:
# wait for a new batch in the source queue and load it
batch_str = queue_source.get()
batch = pickle.loads(batch_str)
# augment the batch
if batch.images is not None and batch.keypoints is not None:
augseq_det = augseq.to_deterministic()
batch.images_aug = augseq_det.augment_images(batch.images)
batch.keypoints_aug = augseq_det.augment_keypoints(batch.keypoints)
elif batch.images is not None:
batch.images_aug = augseq.augment_images(batch.images)
elif batch.keypoints is not None:
batch.keypoints_aug = augseq.augment_keypoints(batch.keypoints)
# send augmented batch to output queue
queue_result.put(pickle.dumps(batch, protocol=-1))
示例2: pickle_dict
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def pickle_dict(items):
'''Returns a new dictionary where values which aren't instances of
basestring are pickled. Also, a new key '_pickled' contains a comma
separated list of keys corresponding to the pickled values.
'''
ret = {}
pickled_keys = []
for key, val in items.items():
if isinstance(val, basestring):
ret[key] = val
else:
pickled_keys.append(key)
ret[key] = pickle.dumps(val)
if pickled_keys:
ret['_pickled'] = ','.join(pickled_keys)
return ret
示例3: put
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def put(self, o):
"""Encode object ``o`` and write it to the pipe.
Block gevent-cooperatively until all data is written. The default
encoder is ``pickle.dumps``.
:arg o: a Python object that is encodable with the encoder of choice.
Raises:
- :exc:`GIPCError`
- :exc:`GIPCClosed`
- :exc:`pickle.PicklingError`
"""
self._validate()
with self._lock:
bindata = self._encoder(o)
self._write(struct.pack("!i", len(bindata)) + bindata)
示例4: to_metadata
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def to_metadata(self):
metadata = dict()
metadata['outputs'] = self.outputs
metadata['inputs'] = self.inputs
metadata['N'] = self.N
metadata['data'] = {'x': self.data.x, 'Y': self.data.Y}
metadata['distargs'] = self.get_distargs()
metadata['params'] = self.get_params()
metadata['factory'] = ('cgpm.regressions.ols', 'OrdinaryLeastSquares')
# Pickle the sklearn regressor.
regressor = metadata['params']['regressor']
regressor_binary = base64.b64encode(cPickle.dumps(regressor))
metadata['params']['regressor_binary'] = regressor_binary
del metadata['params']['regressor']
return metadata
示例5: to_metadata
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def to_metadata(self):
metadata = dict()
metadata['outputs'] = self.outputs
metadata['inputs'] = self.inputs
metadata['N'] = self.N
metadata['data'] = {'x': self.data.x, 'Y': self.data.Y}
metadata['counts'] = self.counts
metadata['distargs'] = self.get_distargs()
metadata['hypers'] = self.get_hypers()
metadata['params'] = self.get_params()
metadata['factory'] = ('cgpm.regressions.forest', 'RandomForest')
# Pickle the sklearn forest.
forest = metadata['params']['forest']
forest_binary = base64.b64encode(cPickle.dumps(forest))
metadata['params']['forest_binary'] = forest_binary
del metadata['params']['forest']
return metadata
示例6: setData
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def setData(self, data):
"""Add some data.
Args:
data (object): Object to add as data. This object has to be pickable.
Qt objects don't work!
Raises:
TypeError if data is not pickable
"""
try:
bytestream = pickle.dumps(data)
super(MimeData, self).setData(self._mimeType, bytestream)
except TypeError:
raise TypeError, self.tr("can not pickle added data")
except:
raise
示例7: __call__
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def __call__(self, *args, **kwargs):
arguments = self.base_dict.copy()
if not self.pass_arguments:
if len(args) > 0:
arguments.update(args[0])
if kwargs:
arguments.update(kwargs)
else:
spooler_args = {}
for key in ('message_dict', 'spooler', 'priority', 'at', 'body'):
if key in kwargs:
spooler_args.update({key: kwargs.pop(key)})
arguments.update(spooler_args)
arguments.update(
{'args': pickle.dumps(args), 'kwargs': pickle.dumps(kwargs)})
return uwsgi.spool(_encode_to_spooler(arguments))
# For backward compatibility (uWSGI < 1.9.13)
示例8: _build_model
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def _build_model(self):
model_path = self._get_model_path()
if os.path.isfile(model_path):
return
freq = {}
print('building model..')
for path, _ in tqdm.tqdm(iterate_data_files(self.from_dtm, self.to_dtm),
mininterval=1):
for line in open(path):
seen = line.strip().split()[1:]
for s in seen:
freq[s] = freq.get(s, 0) + 1
freq = sorted(freq.items(), key=lambda x: x[1], reverse=True)
open(model_path, 'wb').write(cPickle.dumps(freq, 2))
print('model built')
示例9: _memoize
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def _memoize(func, *args, **kw):
# should we refresh the cache?
refresh = False
refresh_kw = func.mrefresh_keyword
# kw is not always set - check args
if refresh_kw in func.__code__.co_varnames:
if args[func.__code__.co_varnames.index(refresh_kw)]:
refresh = True
# check in kw if not already set above
if not refresh and refresh_kw in kw:
if kw[refresh_kw]:
refresh = True
key = pickle.dumps(args, 1) + pickle.dumps(kw, 1)
cache = func.mcache
if not refresh and key in cache:
return cache[key]
else:
cache[key] = result = func(*args, **kw)
return result
示例10: apply
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def apply(self, callback, route):
dumps = self.json_dumps
if not dumps: return callback
def wrapper(*a, **ka):
try:
rv = callback(*a, **ka)
except HTTPError:
rv = _e()
if isinstance(rv, dict):
#Attempt to serialize, raises exception on failure
json_response = dumps(rv)
#Set content type only if serialization succesful
response.content_type = 'application/json'
return json_response
elif isinstance(rv, HTTPResponse) and isinstance(rv.body, dict):
rv.body = dumps(rv.body)
rv.content_type = 'application/json'
return rv
return wrapper
示例11: pickle_dict
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def pickle_dict(items):
"""Convert `items` values into pickled values.
Args:
items (dict): A dictionary
Returns:
dict: Values which aren't instances of basestring are pickled. Also,
a new key '_pickled' contains a comma separated list of keys
corresponding to the pickled values.
"""
ret = {}
pickled_keys = []
for k, v in items.items():
if isinstance(v, basestring):
ret[k] = v
else:
pickled_keys.append(k)
ret[k] = pickle.dumps(v)
if pickled_keys:
ret['_pickled'] = ','.join(pickled_keys)
return ret
示例12: makePickle
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def makePickle(self, record):
"""
Pickles the record in binary format with a length prefix, and
returns it ready for transmission across the socket.
"""
ei = record.exc_info
if ei:
# just to get traceback text into record.exc_text ...
dummy = self.format(record)
record.exc_info = None # to avoid Unpickleable error
# See issue #14436: If msg or args are objects, they may not be
# available on the receiving end. So we convert the msg % args
# to a string, save it as msg and zap the args.
d = dict(record.__dict__)
d['msg'] = record.getMessage()
d['args'] = None
s = cPickle.dumps(d, 1)
if ei:
record.exc_info = ei # for next handler
slen = struct.pack(">L", len(s))
return slen + s
示例13: TestCustomFormat
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def TestCustomFormat():
OpenClipboard()
try:
# Just for the fun of it pickle Python objects through the clipboard
fmt = RegisterClipboardFormat("Python Pickle Format")
import cPickle
pickled_object = Foo(a=1, b=2, Hi=3)
SetClipboardData(fmt, cPickle.dumps( pickled_object ) )
# Now read it back.
data = GetClipboardData(fmt)
loaded_object = cPickle.loads(data)
assert cPickle.loads(data) == pickled_object, "Didnt get the correct data!"
print "Clipboard custom format tests worked correctly"
finally:
CloseClipboard()
示例14: test_float
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def test_float(self):
for_bin_protos = [4.94e-324, 1e-310]
neg_for_bin_protos = [-x for x in for_bin_protos]
test_values = [0.0, 7e-308, 6.626e-34, 0.1, 0.5,
3.14, 263.44582062374053, 6.022e23, 1e30]
test_proto0_values = test_values + [-x for x in test_values]
test_values = test_proto0_values + for_bin_protos + neg_for_bin_protos
for value in test_proto0_values:
pickle = self.dumps(value, 0)
got = self.loads(pickle)
self.assertEqual(value, got)
for proto in pickletester.protocols[1:]:
for value in test_values:
pickle = self.dumps(value, proto)
got = self.loads(pickle)
self.assertEqual(value, got)
# Backwards compatibility was explicitly broken in r67934 to fix a bug.
示例15: test_pytype_long_ready
# 需要导入模块: import cPickle [as 别名]
# 或者: from cPickle import dumps [as 别名]
def test_pytype_long_ready(self):
# Testing SF bug 551412 ...
# This dumps core when SF bug 551412 isn't fixed --
# but only when test_descr.py is run separately.
# (That can't be helped -- as soon as PyType_Ready()
# is called for PyLong_Type, the bug is gone.)
class UserLong(object):
def __pow__(self, *args):
pass
try:
pow(0L, UserLong(), 0L)
except:
pass
# Another segfault only when run early
# (before PyType_Ready(tuple) is called)
type.mro(tuple)