Python pickle.Unpickler() Examples

The following are 30 code examples of pickle.Unpickler(). 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: inflateScript.py    From 3D-HourGlass-Network with MIT License 6 votes vote down vote up
def inflate(opt = None):
	if opt is not None:
		model3d = HourglassNet3D(opt.nChannels, opt.nStack, opt.nModules, opt.numReductions, ref.nJoints)
		Inflate.nChannels = opt.nChannels
		Inflate.nStack = opt.nStack
		Inflate.nModules = opt.nModules
		Inflate.nRegFrames = opt.nRegFrames
		Inflate.nJoints = ref.nJoints
	else :
		model3d = HourglassNet3D()
	pickle.Unpickler = partial(pickle.Unpickler, encoding="latin1")
	pickle.load = partial(pickle.load, encoding="latin1")
	if opt is not None:
		model = torch.load(opt.Model2D)
	else:
		model = torch.load('models/hgreg-3d.pth') #, map_location=lambda storage, loc: storage)

	Inflate.inflateHourglassNet(model3d, model)

	torch.save(model3d,open('inflatedModel.pth','wb'))

	return model3d 
Example #2
Source File: pkl_utils.py    From D-VAE with MIT License 6 votes vote down vote up
def load(f, persistent_load=PersistentNdarrayLoad):
    """Load a file that was dumped to a zip file.

    :param f: The file handle to the zip file to load the object from.
    :type f: file

    :param persistent_load: The persistent loading function to use for
        unpickling. This must be compatible with the `persisten_id` function
        used when pickling.
    :type persistent_load: callable, optional

    .. versionadded:: 0.8
    """
    with closing(zipfile.ZipFile(f, 'r')) as zip_file:
        p = pickle.Unpickler(BytesIO(zip_file.open('pkl').read()))
        p.persistent_load = persistent_load(zip_file)
        return p.load() 
Example #3
Source File: pkl_utils.py    From attention-lvcsr with MIT License 6 votes vote down vote up
def load(f, persistent_load=PersistentNdarrayLoad):
    """Load a file that was dumped to a zip file.

    :param f: The file handle to the zip file to load the object from.
    :type f: file

    :param persistent_load: The persistent loading function to use for
        unpickling. This must be compatible with the `persisten_id` function
        used when pickling.
    :type persistent_load: callable, optional

    .. versionadded:: 0.8
    """
    with closing(zipfile.ZipFile(f, 'r')) as zip_file:
        p = pickle.Unpickler(BytesIO(zip_file.open('pkl').read()))
        p.persistent_load = persistent_load(zip_file)
        return p.load() 
Example #4
Source File: validate.py    From pySHACL with Apache License 2.0 6 votes vote down vote up
def meta_validate(shacl_graph, inference='rdfs', **kwargs):
    shacl_shacl_graph = meta_validate.shacl_shacl_graph
    if shacl_shacl_graph is None:
        from os import path
        import pickle
        import sys
        if getattr( sys, 'frozen', False ) :
                # runs in a pyinstaller bundle
                here_dir = sys._MEIPASS
                pickle_file = path.join(here_dir, "shacl-shacl.pickle")
        else :
                here_dir = path.dirname(__file__)
                pickle_file = path.join(here_dir, "shacl-shacl.pickle")
        with open(pickle_file, 'rb') as shacl_pickle:
            u = pickle.Unpickler(shacl_pickle, fix_imports=False)
            shacl_shacl_store = u.load()
        shacl_shacl_graph = rdflib.Graph(store=shacl_shacl_store, identifier="http://www.w3.org/ns/shacl-shacl")
        meta_validate.shacl_shacl_graph = shacl_shacl_graph
    shacl_graph = load_from_source(shacl_graph, rdf_format=kwargs.pop('shacl_graph_format', None),
                                   multigraph=True)
    _ = kwargs.pop('meta_shacl', None)
    return validate(shacl_graph, shacl_graph=shacl_shacl_graph, inference=inference, **kwargs) 
Example #5
Source File: serial_pickle.py    From krnnt with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, file: BinaryIO, stop: int=-1, start: int =0, ids: Iterable = None):
        """

        :param file:
        :param start: unpickle objects starting from index start
        :param stop: unpickle objects ending with index stop
        :param ids: unpickle objects with indexes in ids
        """
        if ids is None:
            ids = []
        self.file = file
        self.p = pickle.Unpickler(file)
        self.c = 0
        self.stop = stop
        self.start = start
        self.ids = set(ids) 
Example #6
Source File: memcacheclient.py    From ccs-calendarserver with Apache License 2.0 6 votes vote down vote up
def getClient(
        cls, servers, debug=0, pickleProtocol=0,
        pickler=pickle.Pickler, unpickler=pickle.Unpickler,
        pload=None, pid=None
    ):

        if cls.allowTestCache:
            return TestClient(
                servers, debug=debug,
                pickleProtocol=pickleProtocol, pickler=pickler,
                unpickler=unpickler, pload=pload, pid=pid)
        elif config.Memcached.Pools.Default.ClientEnabled:
            return Client(
                servers, debug=debug, pickleProtocol=pickleProtocol,
                pickler=pickler, unpickler=unpickler, pload=pload, pid=pid)
        else:
            return None 
Example #7
Source File: pickle_compat.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def find_class(self, module, name):
            # override superclass
            key = (module, name)
            module, name = _class_locations_map.get(key, key)
            return super(Unpickler, self).find_class(module, name) 
Example #8
Source File: _qemu.py    From ALF with Apache License 2.0 5 votes vote down vote up
def loads(data, from_module=None):
    up = pickle.Unpickler(StringIO(data))
    def find_global(module, cls):
        if module == "copy_reg" and cls == "_reconstructor":
            return copy_reg._reconstructor
        if module == "__builtin__":
            return getattr(__builtins__, cls)
        if from_module is not None:
            return getattr(from_module, cls)
        return globals()[cls]
    up.find_global = find_global
    return up.load() 
Example #9
Source File: netrpclib.py    From oerplib with GNU Lesser General Public License v3.0 5 votes vote down vote up
def receive(self):
        buf = ''
        while len(buf) < 8:
            chunk = self.sock.recv(8 - len(buf))
            if chunk == '':
                raise NetRPCError("RuntimeError", "Socket connection broken")
            buf += chunk
        size = int(buf)
        buf = self.sock.recv(1)
        if buf != "0":
            exception = buf
        else:
            exception = False
        msg = ''
        while len(msg) < size:
            chunk = self.sock.recv(size - len(msg))
            if chunk == '':
                raise NetRPCError("RuntimeError", "Socket connection broken")
            msg = msg + chunk
        msgio = cStringIO.StringIO(msg)
        unpickler = pickle.Unpickler(msgio)
        unpickler.find_global = None
        res = unpickler.load()

        if isinstance(res[0], BaseException):
            if exception:
                raise NetRPCError(res[0], res[1])
            raise res[0]
        else:
            return res[0]

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: 
Example #10
Source File: shelve.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def first(self):
        (key, value) = self.dict.first()
        f = BytesIO(value)
        return (key.decode(self.keyencoding), Unpickler(f).load()) 
Example #11
Source File: shelve.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def previous(self):
        (key, value) = self.dict.previous()
        f = BytesIO(value)
        return (key.decode(self.keyencoding), Unpickler(f).load()) 
Example #12
Source File: shelve.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def next(self):
        (key, value) = next(self.dict)
        f = BytesIO(value)
        return (key.decode(self.keyencoding), Unpickler(f).load()) 
Example #13
Source File: shelve.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def set_location(self, key):
        (key, value) = self.dict.set_location(key)
        f = BytesIO(value)
        return (key.decode(self.keyencoding), Unpickler(f).load()) 
Example #14
Source File: pickletester.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
def test_bad_init(self):
        # Test issue3664 (pickle can segfault from a badly initialized Pickler).
        # Override initialization without calling __init__() of the superclass.
        class BadPickler(pickle.Pickler):
            def __init__(self): pass

        class BadUnpickler(pickle.Unpickler):
            def __init__(self): pass

        self.assertRaises(pickle.PicklingError, BadPickler().dump, 0)
        self.assertRaises(pickle.UnpicklingError, BadUnpickler().load) 
Example #15
Source File: shelve.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def next(self):
        (key, value) = next(self.dict)
        f = BytesIO(value)
        return (key.decode(self.keyencoding), Unpickler(f).load()) 
Example #16
Source File: shelve.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def set_location(self, key):
        (key, value) = self.dict.set_location(key)
        f = BytesIO(value)
        return (key.decode(self.keyencoding), Unpickler(f).load()) 
Example #17
Source File: shelve.py    From Computable with MIT License 5 votes vote down vote up
def previous(self):
        (key, value) = self.dict.previous()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #18
Source File: shelve.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def __getitem__(self, key):
        try:
            value = self.cache[key]
        except KeyError:
            f = BytesIO(self.dict[key.encode(self.keyencoding)])
            value = Unpickler(f).load()
            if self.writeback:
                self.cache[key] = value
        return value 
Example #19
Source File: pickle_compat.py    From predictive-maintenance-using-machine-learning with Apache License 2.0 5 votes vote down vote up
def load(fh, encoding=None, compat=False, is_verbose=False):
    """load a pickle, with a provided encoding

    if compat is True:
       fake the old class hierarchy
       if it works, then return the new type objects

    Parameters
    ----------
    fh : a filelike object
    encoding : an optional encoding
    compat : provide Series compatibility mode, boolean, default False
    is_verbose : show exception output
    """

    try:
        fh.seek(0)
        if encoding is not None:
            up = Unpickler(fh, encoding=encoding)
        else:
            up = Unpickler(fh)
        up.is_verbose = is_verbose

        return up.load()
    except (ValueError, TypeError):
        raise 
Example #20
Source File: shelve.py    From Computable with MIT License 5 votes vote down vote up
def last(self):
        (key, value) = self.dict.last()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #21
Source File: serialization.py    From attention-lvcsr with MIT License 5 votes vote down vote up
def load(file_, name='_pkl', use_cpickle=False):
    """Loads an object saved using the `dump` function.

    By default, this function loads the object saved by the `dump`
    function. If some objects have been added to the archive using the
    `add_to_dump` function, then you can load them by passing their name
    to the `name` parameter.

    Parameters
    ----------
    file_ : file
        The file that contains the object to load.
    name : str
        Name of the object to load. Default is `_pkl`, meaning that it is
        the original object which have been dumped that is loaded.
    use_cpickle : bool
        Use cPickle instead of pickle. Default: False.

    Returns
    -------
    The object saved in file_.

    """
    file_.seek(0)  # To be able to read several objects in one file
    if use_cpickle:
        unpickler = cPickle.Unpickler
    else:
        unpickler = pickle.Unpickler
    with tarfile.open(fileobj=file_, mode='r') as tar_file:
        p = unpickler(
            tar_file.extractfile(tar_file.getmember(name)))
        if '_parameters' in tar_file.getnames():
            p.persistent_load = _PersistentLoad(tar_file)
        return p.load() 
Example #22
Source File: memcacheclient.py    From ccs-calendarserver with Apache License 2.0 5 votes vote down vote up
def __init__(self, servers, debug=0, pickleProtocol=0,
                 pickler=pickle.Pickler, unpickler=pickle.Unpickler,
                 pload=None, pid=None):
        """
        Create a new Client object with the given list of servers.

        @param servers: C{servers} is passed to L{set_servers}.
        @param debug: whether to display error messages when a server can't be
        contacted.
        @param pickleProtocol: number to mandate protocol used by (c)Pickle.
        @param pickler: optional override of default Pickler to allow subclassing.
        @param unpickler: optional override of default Unpickler to allow subclassing.
        @param pload: optional persistent_load function to call on pickle loading.
        Useful for cPickle since subclassing isn't allowed.
        @param pid: optional persistent_id function to call on pickle storing.
        Useful for cPickle since subclassing isn't allowed.
        """
        local.__init__(self)
        self.set_servers(servers)
        self.debug = debug
        self.stats = {}

        # Allow users to modify pickling/unpickling behavior
        self.pickleProtocol = pickleProtocol
        self.pickler = pickler
        self.unpickler = unpickler
        self.persistent_load = pload
        self.persistent_id = pid

        #  figure out the pickler style
        file = StringIO()
        try:
            pickler = self.pickler(file, protocol=self.pickleProtocol)
            self.picklerIsKeyword = True
        except TypeError:
            self.picklerIsKeyword = False 
Example #23
Source File: test_pickle.py    From oss-ftp with MIT License 5 votes vote down vote up
def loads(self, buf):
        class PersUnpickler(pickle.Unpickler):
            def persistent_load(subself, obj):
                return self.persistent_load(obj)
        f = StringIO(buf)
        u = PersUnpickler(f)
        return u.load() 
Example #24
Source File: shelve.py    From Computable with MIT License 5 votes vote down vote up
def first(self):
        (key, value) = self.dict.first()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #25
Source File: test_pickle.py    From oss-ftp with MIT License 5 votes vote down vote up
def loads(self, buf):
        f = StringIO(buf)
        u = pickle.Unpickler(f)
        return u.load() 
Example #26
Source File: shelve.py    From oss-ftp with MIT License 5 votes vote down vote up
def first(self):
        (key, value) = self.dict.first()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #27
Source File: shelve.py    From oss-ftp with MIT License 5 votes vote down vote up
def previous(self):
        (key, value) = self.dict.previous()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #28
Source File: shelve.py    From oss-ftp with MIT License 5 votes vote down vote up
def next(self):
        (key, value) = self.dict.next()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #29
Source File: shelve.py    From oss-ftp with MIT License 5 votes vote down vote up
def set_location(self, key):
        (key, value) = self.dict.set_location(key)
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example #30
Source File: shelve.py    From oss-ftp with MIT License 5 votes vote down vote up
def __getitem__(self, key):
        try:
            value = self.cache[key]
        except KeyError:
            f = StringIO(self.dict[key])
            value = Unpickler(f).load()
            if self.writeback:
                self.cache[key] = value
        return value