本文整理匯總了Python中pickle.html方法的典型用法代碼示例。如果您正苦於以下問題:Python pickle.html方法的具體用法?Python pickle.html怎麽用?Python pickle.html使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pickle
的用法示例。
在下文中一共展示了pickle.html方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: dump
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def dump(self, obj, file, protocol=0):
"""
Dumps (pickles) a PyGithub object to a file-like object.
Some effort is made to not pickle sensitive informations like the Github credentials used in the :class:`Github` instance.
But NO EFFORT is made to remove sensitive information from the object's attributes.
:param obj: the object to pickle
:param file: the file-like object to pickle to
:param protocol: the `pickling protocol <http://docs.python.org/2.7/library/pickle.html#data-stream-format>`_
"""
pickle.dump((obj.__class__, obj.raw_data, obj.raw_headers), file, protocol)
示例2: _comm_message
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def _comm_message(self, msg):
"""
Handle internal spyder messages.
"""
self.calling_comm_id = msg['content']['comm_id']
# Get message dict
msg_dict = msg['content']['data']
# Load the buffer. Only one is supported.
try:
if PY3:
# https://docs.python.org/3/library/pickle.html#pickle.loads
# Using encoding='latin1' is required for unpickling
# NumPy arrays and instances of datetime, date and time
# pickled by Python 2.
buffer = cloudpickle.loads(msg['buffers'][0],
encoding='latin-1')
else:
buffer = cloudpickle.loads(msg['buffers'][0])
except Exception as e:
logger.debug(
"Exception in cloudpickle.loads : %s" % str(e))
buffer = CommsErrorWrapper(
msg_dict['content']['call_name'],
msg_dict['content']['call_id'])
msg_dict['content']['is_error'] = True
spyder_msg_type = msg_dict['spyder_msg_type']
if spyder_msg_type in self._message_handlers:
self._message_handlers[spyder_msg_type](
msg_dict, buffer)
else:
logger.debug("No such spyder message type: %s" % spyder_msg_type)
示例3: __init__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def __init__(self, file, protocol=2, **kwargs):
"""
:param file: the file-like object to use for exporting the data.
It's write method should accept bytes (a disk file opened in
binary mode, a io.BytesIO object, etc)
:param protocol:int(): the pickle protocol to use.
For more info, refer to documentation:
https://docs.python.org/3/library/pickle.html
"""
super().__init__(**kwargs)
self.file = file
self.protocol = protocol
示例4: __reduce__
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def __reduce__(self):
"""Pickle reduction method
See the documentation for the standard library pickle module:
http://docs.python.org/2/library/pickle.html
Unit metadata is encoded in the zeroth element of third element of the
returned tuple, itself a tuple used to restore the state of the
ndarray. This is always defined for numpy arrays.
"""
np_ret = super(unyt_array, self).__reduce__()
obj_state = np_ret[2]
unit_state = (((str(self.units), self.units.registry.lut),) + obj_state[:],)
new_ret = np_ret[:2] + unit_state + np_ret[3:]
return new_ret
示例5: write_gpickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def write_gpickle(G, path, protocol=pickle.HIGHEST_PROTOCOL):
"""Write graph in Python pickle format.
Pickles are a serialized byte stream of a Python object [1]_.
This format will preserve Python objects used as nodes or edges.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
protocol : integer
Pickling protocol to use. Default value: ``pickle.HIGHEST_PROTOCOL``.
Examples
--------
>>> G = nx.path_graph(4)
>>> nx.write_gpickle(G, "test.gpickle")
References
----------
.. [1] http://docs.python.org/library/pickle.html
"""
pickle.dump(G, path, protocol)
示例6: read_gpickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def read_gpickle(path):
"""Read graph object in Python pickle format.
Pickles are a serialized byte stream of a Python object [1]_.
This format will preserve Python objects used as nodes or edges.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be uncompressed.
Returns
-------
G : graph
A NetworkX graph
Examples
--------
>>> G = nx.path_graph(4)
>>> nx.write_gpickle(G, "test.gpickle")
>>> G = nx.read_gpickle("test.gpickle")
References
----------
.. [1] http://docs.python.org/library/pickle.html
"""
return pickle.load(path)
# fixture for nose tests
示例7: write_gpickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def write_gpickle(G, path, protocol=pickle.HIGHEST_PROTOCOL):
"""Write graph in Python pickle format.
Pickles are a serialized byte stream of a Python object [1]_.
This format will preserve Python objects used as nodes or edges.
Parameters
----------
G : graph
A NetworkX graph
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be compressed.
protocol : integer
Pickling protocol to use. Default value: ``pickle.HIGHEST_PROTOCOL``.
Examples
--------
>>> G = nx.path_graph(4)
>>> nx.write_gpickle(G, "test.gpickle")
References
----------
.. [1] https://docs.python.org/2/library/pickle.html
"""
pickle.dump(G, path, protocol)
示例8: read_gpickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def read_gpickle(path):
"""Read graph object in Python pickle format.
Pickles are a serialized byte stream of a Python object [1]_.
This format will preserve Python objects used as nodes or edges.
Parameters
----------
path : file or string
File or filename to write.
Filenames ending in .gz or .bz2 will be uncompressed.
Returns
-------
G : graph
A NetworkX graph
Examples
--------
>>> G = nx.path_graph(4)
>>> nx.write_gpickle(G, "test.gpickle")
>>> G = nx.read_gpickle("test.gpickle")
References
----------
.. [1] https://docs.python.org/2/library/pickle.html
"""
return pickle.load(path)
# fixture for nose tests
示例9: Save
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def Save(session, filename=None):
"""
save your session to use it later.
Returns the filename of the written file.
If not filename is given, a file named `androguard_session_<DATE>.ag` will
be created in the current working directory.
`<DATE>` is a timestamp with the following format: `%Y-%m-%d_%H%M%S`.
This function will overwrite existing files without asking.
If the file could not written, None is returned.
example::
s = session.Session()
session.Save(s, "msession.ag")
:param session: A Session object to save
:param filename: output filename to save the session
:type filename: string
"""
if not filename:
filename = "androguard_session_{:%Y-%m-%d_%H%M%S}.ag".format(datetime.datetime.now())
if os.path.isfile(filename):
log.warning("{} already exists, overwriting!")
# Setting the recursion limit according to the documentation:
# https://docs.python.org/3/library/pickle.html#what-can-be-pickled-and-unpickled
#
# Some larger APKs require a high recursion limit.
# Tested to be above 35000 for some files, setting to 50k to be sure.
# You might want to set this even higher if you encounter problems
reclimit = sys.getrecursionlimit()
sys.setrecursionlimit(50000)
saved = False
try:
with open(filename, "wb") as fd:
pickle.dump(session, fd)
saved = True
except RecursionError:
log.exception("Recursion Limit hit while saving. "
"Current Recursion limit: {}. "
"Please report this error!".format(sys.getrecursionlimit()))
# Remove partially written file
os.unlink(filename)
sys.setrecursionlimit(reclimit)
return filename if saved else None
示例10: save_pickle
# 需要導入模塊: import pickle [as 別名]
# 或者: from pickle import html [as 別名]
def save_pickle(self, filepath, protocol=None):
"""Save the model to `filepath` as a pickle file
This function send the parameters to CPU before saving the model so
that the pickled file can be loaded with in CPU-only environment.
After the model is saved, it is sent back to the original device.
Saved pickle file can be loaded with `load_pickle` static method.
Note that the transportability of the saved file follows the
specification of `pickle` module, namely serialized data depends on the
specific class or attribute structure when saved. The file may not be
loaded in different environment (version of python or dependent
libraries), or after large refactoring of the pickled object class.
If you want to avoid it, use `chainer.serializers.save_npz`
method instead to save only model parameters.
.. admonition:: Example
>>> from chainer_chemistry.models import BaseForwardModel
>>> class DummyForwardModel(BaseForwardModel):
>>>
>>> def __init__(self, device=-1):
>>> super(DummyForwardModel, self).__init__()
>>> with self.init_scope():
>>> self.l = chainer.links.Linear(3, 10)
>>> self.initialize(device)
>>>
>>> def __call__(self, x):
>>> return self.l(x)
>>>
>>> model = DummyForwardModel()
>>> filepath = 'model.pkl'
>>> model.save_pickle(filepath)
Args:
filepath (str): file path of pickle file.
protocol (int or None): protocol version used in `pickle`.
Use 2 if you need python2/python3 compatibility.
3 or higher is used for python3.
Please refer the official document [1] for more details.
[1]: https://docs.python.org/3.6/library/pickle.html#module-interface
""" # NOQA
current_device = self.device
# --- Move the model to CPU for saving ---
self.update_device(-1)
with open(filepath, mode='wb') as f:
pickle.dump(self, f, protocol=protocol)
# --- Revert the model to original device ---
self.update_device(current_device)