Python pickle.Unpickler() Examples

The following are 30 code examples for showing how to use pickle.Unpickler(). These examples are extracted from open source projects. 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 check out the related API usage on the sidebar.

You may also want to check out all available functions/classes of the module pickle , or try the search function .

Example 1
Project: 3D-HourGlass-Network   Author: Naman-ntc   File: inflateScript.py    License: 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
Project: pySHACL   Author: RDFLib   File: validate.py    License: 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 3
Project: D-VAE   Author: muhanzhang   File: pkl_utils.py    License: 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
Project: attention-lvcsr   Author: rizar   File: pkl_utils.py    License: 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 5
Project: krnnt   Author: kwrobel-nlp   File: serial_pickle.py    License: 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
Project: ccs-calendarserver   Author: apple   File: memcacheclient.py    License: 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
Project: ALF   Author: blackberry   File: _qemu.py    License: 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 8
Project: 3D-HourGlass-Network   Author: Naman-ntc   File: inflateScript.py    License: MIT License 5 votes vote down vote up
def inflate(opt = None):
	if opt is not None:
		model3d = Pose3D(opt.nChannels, opt.nStack, opt.nModules, opt.numReductions, opt.nRegModules, opt.nRegFrames, ref.nJoints, ref.temporal)
		Inflate.nChannels = opt.nChannels
		Inflate.nStack = opt.nStack
		Inflate.nModules = opt.nModules
		Inflate.nRegFrames = opt.nRegFrames
		Inflate.nJoints = ref.nJoints
		Inflate.scheme = opt.scheme
		Inflate.mult = opt.mult
	else :
		opt = opts().parse()
		Inflate.nChannels = opt.nChannels
		Inflate.nStack = opt.nStack
		Inflate.nModules = opt.nModules
		Inflate.nRegFrames = opt.nRegFrames
		Inflate.nJoints = ref.nJoints
		Inflate.scheme = opt.scheme
		model3d = Pose3D(opt.nChannels, opt.nStack, opt.nModules, opt.numReductions, opt.nRegModules, opt.nRegFrames, ref.nJoints, ref.temporal)
	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/xingy.pth') #, map_location=lambda storage, loc: storage)

	Inflate.inflatePose3D(model3d, model)

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

	return model3d


#inflate() 
Example 9
Project: recruit   Author: Frank-qlu   File: pickle_compat.py    License: 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 10
Project: recruit   Author: Frank-qlu   File: pickle_compat.py    License: 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 11
def RMUserData_convertFromSQLite(data):
    if data == None:
        return None

    inputStream = StringIO(data)
    unpickler = pickle.Unpickler(inputStream)
    return unpickler.load() 
Example 12
def RMParserParams_convertFromSQLite(data):
    if data == None:
        return None

    inputStream = StringIO(data)
    unpickler = pickle.Unpickler(inputStream)
    return unpickler.load() 
Example 13
Project: rekall   Author: google   File: utils.py    License: GNU General Public License v2.0 5 votes vote down vote up
def SafeUnpickler(data):
    """An unpickler for serialized tuple/lists/strings etc.

    Does not support recovering instances.
    """
    unpickler = pickle.Unpickler(io.StringIO(data))
    unpickler.find_global = None

    return unpickler.load() 
Example 14
Project: rekall   Author: google   File: cache.py    License: GNU General Public License v2.0 5 votes vote down vote up
def RestrictedPickler(raw):
    if six.PY3:
        return RestrictedUnpickler(io.BytesIO(utils.SmartStr(raw)))

    unpickler = pickle.Unpickler(io.BytesIO(utils.SmartStr(raw)))
    unpickler.find_global = None
    return unpickler 
Example 15
Project: meddle   Author: glmcdona   File: shelve.py    License: 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 
Example 16
Project: meddle   Author: glmcdona   File: shelve.py    License: 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 17
Project: meddle   Author: glmcdona   File: shelve.py    License: 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 18
Project: meddle   Author: glmcdona   File: shelve.py    License: 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 19
Project: meddle   Author: glmcdona   File: shelve.py    License: 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 20
Project: ironpython2   Author: IronLanguages   File: shelve.py    License: Apache License 2.0 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 
Example 21
Project: ironpython2   Author: IronLanguages   File: shelve.py    License: Apache License 2.0 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 22
Project: ironpython2   Author: IronLanguages   File: shelve.py    License: Apache License 2.0 5 votes vote down vote up
def next(self):
        (key, value) = self.dict.next()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example 23
Project: ironpython2   Author: IronLanguages   File: shelve.py    License: Apache License 2.0 5 votes vote down vote up
def previous(self):
        (key, value) = self.dict.previous()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example 24
Project: ironpython2   Author: IronLanguages   File: shelve.py    License: Apache License 2.0 5 votes vote down vote up
def first(self):
        (key, value) = self.dict.first()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example 25
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    License: Apache License 2.0 5 votes vote down vote up
def loads(self, buf):
        f = StringIO(buf)
        u = pickle.Unpickler(f)
        return u.load() 
Example 26
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    License: Apache License 2.0 5 votes vote down vote up
def loads(self, buf):
        f = StringIO(buf)
        u = pickle.Unpickler(f)
        return u.load() 
Example 27
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    License: Apache License 2.0 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 28
Project: ironpython2   Author: IronLanguages   File: test_cPickle.py    License: Apache License 2.0 5 votes vote down vote up
def test_unpickler(self, module=cPickle, verbose=True):
        s = StringIO()
        TestBank.selph = self
        for test in TestBank.tests:
            obj, pickle_lists, display_name = TestBank.normalize(test)
            expected = normalized_repr(obj)

            if verbose: print "Testing %s..." % display_name,

            for proto in range(3):
                if proto not in pickle_lists:
                    continue

                if verbose: print proto,

                pickles = pickle_lists[proto]
                if not isinstance(pickles, tuple):
                    pickles = (pickles,)

                pickle_num = 0

                for pickle in pickles:
                    unpickler = module.Unpickler(s)
                    s.truncate(0)
                    s.write(pickle.replace('<', '').replace('>', ''))
                    s.seek(0)
                    unpickled_obj = unpickler.load()
                    actual = normalized_repr(unpickled_obj)

                    if expected != actual:
                        print
                        self.fail('Wrong unpickled value:\n'
                            'with pickle %d %r\n'
                            'expected\n'
                            '%r, got\n'
                            '%r' % (pickle_num, pickle, expected, actual))

                    pickle_num += 1

            if verbose: print 'ok' 
Example 29
Project: vnpy_crypto   Author: birforce   File: pickle_compat.py    License: MIT License 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 30
Project: vnpy_crypto   Author: birforce   File: pickle_compat.py    License: MIT License 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:
        raise