def serialize(obj):
return pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
python类HIGHEST_PROTOCOL的实例源码
def get_users_orders(self, prior_or_train):
'''
get users' prior detailed orders
'''
if os.path.exists(self.cache_dir + 'users_orders.pkl'):
with open(self.cache_dir + 'users_orders.pkl', 'rb') as f:
users_orders = pickle.load(f)
else:
orders = self.get_orders()
order_products_prior = self.get_orders_items(prior_or_train)
users_orders = pd.merge(order_products_prior, orders[['user_id', 'order_id', 'order_number', 'days_up_to_last']],
on = ['order_id'], how = 'left')
with open(self.cache_dir + 'users_orders.pkl', 'wb') as f:
pickle.dump(users_orders, f, pickle.HIGHEST_PROTOCOL)
return users_orders
def get_users_products(self, prior_or_train):
'''
get users' all purchased products
'''
if os.path.exists(self.cache_dir + 'users_products.pkl'):
with open(self.cache_dir + 'users_products.pkl', 'rb') as f:
users_products = pickle.load(f)
else:
users_products = self.get_users_orders(prior_or_train)[['user_id', 'product_id']].drop_duplicates()
users_products['product_id'] = users_products.product_id.astype(int)
users_products['user_id'] = users_products.user_id.astype(int)
users_products = users_products.groupby(['user_id'])['product_id'].apply(list).reset_index()
with open(self.cache_dir + 'users_products.pkl', 'wb') as f:
pickle.dump(users_products, f, pickle.HIGHEST_PROTOCOL)
return users_products
def get_baskets(self, prior_or_train, reconstruct = False, reordered = False, none_idx = 49689):
'''
get users' baskets
'''
if reordered:
filepath = self.cache_dir + './reorder_basket_' + prior_or_train + '.pkl'
else:
filepath = self.cache_dir + './basket_' + prior_or_train + '.pkl'
if (not reconstruct) and os.path.exists(filepath):
with open(filepath, 'rb') as f:
up_basket = pickle.load(f)
else:
up = self.get_users_orders(prior_or_train).sort_values(['user_id', 'order_number', 'product_id'], ascending = True)
uid_oid = up[['user_id', 'order_number']].drop_duplicates()
up = up[up.reordered == 1][['user_id', 'order_number', 'product_id']] if reordered else up[['user_id', 'order_number', 'product_id']]
up_basket = up.groupby(['user_id', 'order_number'])['product_id'].apply(list).reset_index()
up_basket = pd.merge(uid_oid, up_basket, on = ['user_id', 'order_number'], how = 'left')
for row in up_basket.loc[up_basket.product_id.isnull(), 'product_id'].index:
up_basket.at[row, 'product_id'] = [none_idx]
up_basket = up_basket.sort_values(['user_id', 'order_number'], ascending = True).groupby(['user_id'])['product_id'].apply(list).reset_index()
up_basket.columns = ['user_id', 'reorder_basket'] if reordered else ['user_id', 'basket']
#pdb.set_trace()
with open(filepath, 'wb') as f:
pickle.dump(up_basket, f, pickle.HIGHEST_PROTOCOL)
return up_basket
def donations(filename='donationdata.pickle'):
try:
print("donation data pickled already. Grabbing data from donationdata.picke")
with open(filename, 'rb') as handle:
donations = pickle.load(handle)
return donations
except EOFError:
print("donation data not pickled, grabbing directly from FEC and ProPublica APIs")
donations = donations_helper()
with open(filename, 'wb') as handle:
pickle.dump(donations, handle, protocol=pickle.HIGHEST_PROTOCOL)
return donations
def __init__(self, process_obj):
# create pipe for communication with child
rfd, wfd = os.pipe()
# get handle for read end of the pipe and make it inheritable
rhandle = duplicate(msvcrt.get_osfhandle(rfd), inheritable=True)
os.close(rfd)
# start process
cmd = get_command_line() + [rhandle]
cmd = ' '.join('"%s"' % x for x in cmd)
hp, ht, pid, tid = _subprocess.CreateProcess(
_python_exe, cmd, None, None, 1, 0, None, None, None
)
ht.Close()
close(rhandle)
# set attributes of self
self.pid = pid
self.returncode = None
self._handle = hp
# send information to child
prep_data = get_preparation_data(process_obj._name)
to_child = os.fdopen(wfd, 'wb')
Popen._tls.process_handle = int(hp)
try:
dump(prep_data, to_child, HIGHEST_PROTOCOL)
dump(process_obj, to_child, HIGHEST_PROTOCOL)
finally:
del Popen._tls.process_handle
to_child.close()
def save_pickle(filename, save):
try:
f = open(filename, 'wb')
pickle.dump(save, f, pickle.HIGHEST_PROTOCOL)
f.close()
except Exception as e:
logging.error(f'Unable to save data to {filename}: {e}')
raise
def _write_entries(self, entries):
log_file = open(self._filename, 'wb')
try:
log_file.seek(0)
for entry in entries:
pickle.dump(entry, log_file, pickle.HIGHEST_PROTOCOL)
log_file.flush()
os.fsync(log_file.fileno())
finally:
log_file.close()
def record(self, var_name, var_value):
"""
Call this method each time you want to record a variable with name `var_name` and value `var_value`.
Usually, there is one plot for each `var_name`.
:param var_name: Name of variable to record
:param var_value: Value of variable to record
"""
assert not isinstance(var_value, type(SENTINEL)) or var_value != SENTINEL, \
"You cannot record a value {} since this conflicts with the internal SENTINEL string"
topic = pickle.dumps(var_name, protocol=pickle.HIGHEST_PROTOCOL)
messagedata = pickle.dumps(var_value, protocol=pickle.HIGHEST_PROTOCOL)
self.socket.send_multipart([topic, messagedata])
rlogger.debug("Sent message to topic %s", var_name)
def close(self, var_name):
"""
Call this method for each variable name `var_name` to clean up the plotting process
:param var_name: Name of variable to clean up.
"""
topic = pickle.dumps(var_name, protocol=pickle.HIGHEST_PROTOCOL)
messagedata = pickle.dumps(SENTINEL, protocol=pickle.HIGHEST_PROTOCOL)
self.socket.send_multipart([topic, messagedata])
rlogger.debug("Sent close message to topic %s", var_name)
def snapshot(self, sess, iter):
net = self.net
if not os.path.exists(self.output_dir):
os.makedirs(self.output_dir)
# Store the model snapshot
filename = cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(iter) + '.ckpt'
filename = os.path.join(self.output_dir, filename)
self.saver.save(sess, filename)
print('Wrote snapshot to: {:s}'.format(filename))
# Also store some meta information, random state, etc.
nfilename = cfg.TRAIN.SNAPSHOT_PREFIX + '_iter_{:d}'.format(iter) + '.pkl'
nfilename = os.path.join(self.output_dir, nfilename)
# current state of numpy random
st0 = np.random.get_state()
# current position in the database
cur = self.data_layer._cur
# current shuffled indexes of the database
perm = self.data_layer._perm
# current position in the validation database
cur_val = self.data_layer_val._cur
# current shuffled indexes of the validation database
perm_val = self.data_layer_val._perm
# Dump the meta info
with open(nfilename, 'wb') as fid:
pickle.dump(st0, fid, pickle.HIGHEST_PROTOCOL)
pickle.dump(cur, fid, pickle.HIGHEST_PROTOCOL)
pickle.dump(perm, fid, pickle.HIGHEST_PROTOCOL)
pickle.dump(cur_val, fid, pickle.HIGHEST_PROTOCOL)
pickle.dump(perm_val, fid, pickle.HIGHEST_PROTOCOL)
pickle.dump(iter, fid, pickle.HIGHEST_PROTOCOL)
return filename, nfilename
def gt_roidb(self):
"""
Return the database of ground-truth regions of interest.
This function loads/saves from/to a cache file to speed up future calls.
"""
cache_file = osp.join(self.cache_path, self.name + '_gt_roidb.pkl')
if osp.exists(cache_file):
with open(cache_file, 'rb') as fid:
roidb = pickle.load(fid)
print('{} gt roidb loaded from {}'.format(self.name, cache_file))
return roidb
gt_roidb = [self._load_coco_annotation(index)
for index in self._image_index]
with open(cache_file, 'wb') as fid:
pickle.dump(gt_roidb, fid, pickle.HIGHEST_PROTOCOL)
print('wrote gt roidb to {}'.format(cache_file))
return gt_roidb
def _do_detection_eval(self, res_file, output_dir):
ann_type = 'bbox'
coco_dt = self._COCO.loadRes(res_file)
coco_eval = COCOeval(self._COCO, coco_dt)
coco_eval.params.useSegm = (ann_type == 'segm')
coco_eval.evaluate()
coco_eval.accumulate()
self._print_detection_eval_metrics(coco_eval)
eval_file = osp.join(output_dir, 'detection_results.pkl')
with open(eval_file, 'wb') as fid:
pickle.dump(coco_eval, fid, pickle.HIGHEST_PROTOCOL)
print('Wrote COCO eval results to: {}'.format(eval_file))
def save_pickle(obj, dir, filename):
path = os.path.join(dir + filename + '.pkl')
with open(path, 'wb') as f:
pickle.dump(obj, f, pickle.HIGHEST_PROTOCOL)
def kepler_lcdict_to_pkl(lcdict,
outfile=None):
'''This simply writes the lcdict to a pickle.
'''
if not outfile:
outfile = '%s-keplc.pkl' % lcdict['objectid'].replace(' ','-')
# we're using pickle.HIGHEST_PROTOCOL here, this will make Py3 pickles
# unreadable for Python 2.7
with open(outfile,'wb') as outfd:
pickle.dump(lcdict, outfd, protocol=pickle.HIGHEST_PROTOCOL)
return os.path.abspath(outfile)
def test_unicode_pickle(self):
# A tree containing Unicode characters can be pickled.
html = u"<b>\N{SNOWMAN}</b>"
soup = self.soup(html)
dumped = pickle.dumps(soup, pickle.HIGHEST_PROTOCOL)
loaded = pickle.loads(dumped)
self.assertEqual(loaded.decode(), soup.decode())
def test_unicode_pickle(self):
# A tree containing Unicode characters can be pickled.
html = "<b>\N{SNOWMAN}</b>"
soup = self.soup(html)
dumped = pickle.dumps(soup, pickle.HIGHEST_PROTOCOL)
loaded = pickle.loads(dumped)
self.assertEqual(loaded.decode(), soup.decode())
def save_binary(obj, path):
with open(path,'wb') as f:
pickle.dump(obj, f, protocol=pickle.HIGHEST_PROTOCOL)
def save(self, session):
fn = self.get_session_filename(session.sid)
fd, tmp = tempfile.mkstemp(suffix=_fs_transaction_suffix,
dir=self.path)
f = os.fdopen(fd, 'wb')
try:
dump(dict(session), f, HIGHEST_PROTOCOL)
finally:
f.close()
try:
rename(tmp, fn)
os.chmod(fn, self.mode)
except (IOError, OSError):
pass
def set(self, key, value, timeout=None):
expires = self._get_expiration(timeout)
self._prune()
self._cache[key] = (expires, pickle.dumps(value,
pickle.HIGHEST_PROTOCOL))
return True