Python pickle.html() Examples

The following are 12 code examples of pickle.html(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pickle , or try the search function .
Example #1
Source File: MainClass.py    From gist-alfred with MIT License 5 votes vote down vote up
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) 
Example #2
Source File: commbase.py    From spyder-kernels with MIT License 5 votes vote down vote up
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) 
Example #3
Source File: exporters.py    From transistor with MIT License 5 votes vote down vote up
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 
Example #4
Source File: array.py    From unyt with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #5
Source File: gpickle.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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) 
Example #6
Source File: gpickle.py    From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #7
Source File: gpickle.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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) 
Example #8
Source File: gpickle.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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 
Example #9
Source File: gpickle.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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) 
Example #10
Source File: gpickle.py    From aws-kube-codesuite with Apache License 2.0 5 votes vote down vote up
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 
Example #11
Source File: session.py    From dcc with Apache License 2.0 4 votes vote down vote up
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 
Example #12
Source File: base.py    From chainer-chemistry with MIT License 4 votes vote down vote up
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)