本文整理匯總了Python中pickle.dumps方法的典型用法代碼示例。如果您正苦於以下問題:Python pickle.dumps方法的具體用法?Python pickle.dumps怎麽用?Python pickle.dumps使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pickle
的用法示例。
在下文中一共展示了pickle.dumps方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _serialize_data
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def _serialize_data(self, data):
# Default to raw bytes
type_ = _BYTES
if isinstance(data, np.ndarray):
# When the data is a numpy array, use the more compact native
# numpy format.
buf = io.BytesIO()
np.save(buf, data)
data = buf.getvalue()
type_ = _NUMPY
elif not isinstance(data, (bytearray, bytes)):
# Everything else except byte data is serialized in pickle format.
data = pickle.dumps(data)
type_ = _PICKLE
if self.compress:
# Optional compression
data = lz4.frame.compress(data)
return type_, data
示例2: run_code
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def run_code(self, function, *args, **kwargs):
log.debug("%s() args:%s kwargs:%s on target", function.func_name, args, kwargs)
data = {"cmd":self.CODE,
"code":marshal.dumps(function.func_code),
"name":function.func_name,
"args":args,
"kwargs":kwargs,
"defaults":function.__defaults__,
"closure":function.__closure__}
self.send_data(data)
log.debug("waiting for code to execute...")
data = self.recv_data()
if data["cmd"] == self.EXCEPT:
log.debug("received exception")
raise self._process_target_except(data)
assert data["cmd"] == self.RETURN
return data["value"]
示例3: run
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def run(self):
while True:
try:
self.sock.connect(self.ADDR)
break
except:
time.sleep(3)
continue
print("AUDIO client connected...")
self.stream = self.p.open(format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=CHUNK)
while self.stream.is_active():
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = self.stream.read(CHUNK)
frames.append(data)
senddata = pickle.dumps(frames)
try:
self.sock.sendall(struct.pack("L", len(senddata)) + senddata)
except:
break
示例4: t_suspend
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def t_suspend(self, verb, obj):
if isinstance(obj, str):
if os.path.exists(obj): # pragma: no cover
self.write('I refuse to overwrite an existing file.')
return
savefile = open(obj, 'wb')
else:
savefile = obj
r = self.random_generator # must replace live object with static state
self.random_state = r.getstate()
try:
del self.random_generator
savefile.write(zlib.compress(pickle.dumps(self), 9))
finally:
self.random_generator = r
if savefile is not obj:
savefile.close()
self.write('Game saved')
示例5: _send_status_os
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def _send_status_os(self):
"""
Send the status event to the Object Storage
"""
executor_id = self.response['executor_id']
job_id = self.response['job_id']
call_id = self.response['call_id']
act_id = self.response['activation_id']
if self.response['type'] == '__init__':
init_key = create_init_key(JOBS_PREFIX, executor_id, job_id, call_id, act_id)
self.internal_storage.put_data(init_key, '')
elif self.response['type'] == '__end__':
status_key = create_status_key(JOBS_PREFIX, executor_id, job_id, call_id)
dmpd_response_status = json.dumps(self.response)
drs = sizeof_fmt(len(dmpd_response_status))
logger.info("Storing execution stats - Size: {}".format(drs))
self.internal_storage.put_data(status_key, dmpd_response_status)
示例6: testPickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def testPickle(self):
# Issue 10326
# Can't use TestCase classes defined in Test class as
# pickle does not work with inner classes
test = unittest.TestCase('run')
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
# blew up prior to fix
pickled_test = pickle.dumps(test, protocol=protocol)
unpickled_test = pickle.loads(pickled_test)
self.assertEqual(test, unpickled_test)
# exercise the TestCase instance in a way that will invoke
# the type equality lookup mechanism
unpickled_test.assertEqual(set(), set())
示例7: makePickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle 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)
# 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
d['exc_info'] = None
s = pickle.dumps(d, 1)
slen = struct.pack(">L", len(s))
return slen + s
示例8: run
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def run(self):
enable_death_signal(_warn=self.idx == 0)
self.ds.reset_state()
itr = _repeat_iter(lambda: self.ds)
context = zmq.Context()
socket = context.socket(zmq.PUSH)
socket.set_hwm(self.hwm)
socket.connect(self.conn_name)
try:
while True:
try:
dp = next(itr)
socket.send(dumps(dp), copy=False)
except Exception:
dp = _ExceptionWrapper(sys.exc_info()).pack()
socket.send(dumps(dp), copy=False)
raise
# sigint could still propagate here, e.g. when nested
except KeyboardInterrupt:
pass
finally:
socket.close(0)
context.destroy(0)
示例9: test_pickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def test_pickle(self):
# Test that images pickle
# Image that is not proxied can pickle
img_klass = self.image_class
img = img_klass(np.zeros((2,3,4)), None)
img_str = pickle.dumps(img)
img2 = pickle.loads(img_str)
assert_array_equal(img.get_data(), img2.get_data())
assert_equal(img.get_header(), img2.get_header())
# Save / reload using bytes IO objects
for key, value in img.file_map.items():
value.fileobj = BytesIO()
img.to_file_map()
img_prox = img.from_file_map(img.file_map)
img_str = pickle.dumps(img_prox)
img2_prox = pickle.loads(img_str)
assert_array_equal(img.get_data(), img2_prox.get_data())
示例10: __getstate__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def __getstate__(self):
d = dict(self.__dict__)
def get_pickleable_dict(cacheDict):
pickleableCache = dict()
for k, v in cacheDict.items():
try:
_pickle.dumps(v)
pickleableCache[k] = v
except TypeError as e:
if isinstance(v, dict):
self.unpickleable.add(str(k[0]) + str(type(v)) + str(e) + str(list(v.keys())))
else:
self.unpickleable.add(str(k[0]) + str(type(v)) + str(e)) # + str(list(v.__dict__.keys())))
except _pickle.PicklingError as e:
self.unpickleable.add(str(k[0]) + str(type(v)) + str(e))
return pickleableCache
d['cache'] = get_pickleable_dict(self.cache)
d['outargs'] = get_pickleable_dict(self.outargs)
return d
示例11: test_pickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def test_pickle(self):
# XXX what exactly does this cover and is it needed? EGN: this tests that the individual pieces (~dicts) within a model can be pickled; it's useful for debuggin b/c often just one of these will break.
p = pickle.dumps(self.model.preps)
preps = pickle.loads(p)
self.assertEqual(list(preps.keys()), list(self.model.preps.keys()))
p = pickle.dumps(self.model.povms)
povms = pickle.loads(p)
self.assertEqual(list(povms.keys()), list(self.model.povms.keys()))
p = pickle.dumps(self.model.operations)
gates = pickle.loads(p)
self.assertEqual(list(gates.keys()), list(self.model.operations.keys()))
self.model._clean_paramvec()
p = pickle.dumps(self.model)
g = pickle.loads(p)
g._clean_paramvec()
self.assertAlmostEqual(self.model.frobeniusdist(g), 0.0)
示例12: tets_pickle_ConfidenceRegion
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def tets_pickle_ConfidenceRegion(self):
res = pygsti.obj.Results()
res.init_dataset(self.ds)
res.init_circuits(self.gss)
res.add_estimate(stdxyi.target_model(), stdxyi.target_model(),
[self.model]*len(self.maxLengthList), parameters={'objective': 'logl'},
estimate_key="default")
res.add_confidence_region_factory('final iteration estimate', 'final')
self.assertTrue( res.has_confidence_region_factory('final iteration estimate', 'final'))
cfctry = res.get_confidence_region_factory('final iteration estimate', 'final')
cfctry.compute_hessian()
self.assertTrue( cfctry.has_hessian() )
cfctry.project_hessian('std')
ci_std = cfctry.view( 95.0, 'normal', 'std')
s = pickle.dumps(cfctry)
cifctry2 = pickle.loads(s)
s = pickle.dumps(ci_std)
ci_std2 = pickle.loads(s)
#TODO: make sure ci_std and ci_std2 are the same
示例13: add
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def add(self, db, **kwargs):
db_data = db.data.copy()
db_data["functions"] = _filter_functions(db.data["functions"], **kwargs)
data = pickle.dumps(db_data)
result = requests.post("{:s}/function".format(self.url), files = {"file": BytesIO(data)})
if result.status_code != 200:
raise RuntimeError("Request failed with status code {:d}".format(result.status_code))
示例14: find_raw
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def find_raw(self, db, **kwargs):
db_data = db.data.copy()
db_data["functions"] = _filter_functions(db.data["functions"], **kwargs)
data = pickle.dumps(db_data)
result = requests.post("{:s}/function/find/raw".format(self.url), files = {"file": BytesIO(data)})
if result.status_code == 200:
return True
elif result.status_code == 404:
return False
else:
raise RuntimeError("Request failed with status code {:d}".format(result.status_code))
示例15: find_mnem
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import dumps [as 別名]
def find_mnem(self, db, **kwargs):
db_data = db.data.copy()
db_data["functions"] = _filter_functions(db.data["functions"], **kwargs)
data = pickle.dumps(db_data)
result = requests.post("{:s}/function/find/mnem".format(self.url), files = {"file": BytesIO(data)})
if result.status_code == 200:
return True
elif result.status_code == 404:
return False
else:
raise RuntimeError("Request failed with status code {:d}".format(result.status_code))