Python pickle.Unpickler() Examples

The following are code examples for showing how to use pickle.Unpickler(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: 3D-HourGlass-Network   Author: Naman-ntc   File: inflateScript.py    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: D-VAE   Author: muhanzhang   File: pkl_utils.py    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
Project: pyblish-win   Author: pyblish   File: shelve.py    GNU Lesser General Public License v3.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 4
Project: pyblish-win   Author: pyblish   File: shelve.py    GNU Lesser General Public License v3.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 5
Project: pyblish-win   Author: pyblish   File: shelve.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def next(self):
        (key, value) = self.dict.next()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example 6
Project: pyblish-win   Author: pyblish   File: shelve.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def previous(self):
        (key, value) = self.dict.previous()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example 7
Project: pyblish-win   Author: pyblish   File: shelve.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def first(self):
        (key, value) = self.dict.first()
        f = StringIO(value)
        return (key, Unpickler(f).load()) 
Example 8
Project: pyblish-win   Author: pyblish   File: test_pickle.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def loads(self, buf):
        f = StringIO(buf)
        u = pickle.Unpickler(f)
        return u.load() 
Example 9
Project: pyblish-win   Author: pyblish   File: test_pickle.py    GNU Lesser General Public License v3.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 10
Project: FalltoSkyBot   Author: Sakiut   File: casino.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def start():
	if os.path.exists(fileName):
		f = open(fileName, "rb")
		d = pickle.Unpickler(f)
		data = d.load()
		f.close()
	else:
		data = {}

	return data

######################################################################################################################## 
Example 11
Project: FalltoSkyBot   Author: Sakiut   File: moderation.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def start():
	if os.path.exists(fileName):
		f = open(fileName, "rb")
		d = pickle.Unpickler(f)
		data = d.load()
		f.close()
	else:
		data = {}

	return data

##################################################################################################################################################### 
Example 12
Project: WhooshSearch   Author: rokartnaz   File: loading.py    BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, f, objmap, shortcuts=None):
        pickle.Unpickler.__init__(self, f)

        if shortcuts:
            objmap = dict((k % shortcuts, v % shortcuts)
                          for k, v in objmap.items())
        self._objmap = objmap 
Example 13
Project: 3D-HourGlass-Network   Author: Naman-ntc   File: inflateScript.py    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 14
Project: NiujiaoDebugger   Author: MrSrc   File: shelve.py    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 15
Project: NiujiaoDebugger   Author: MrSrc   File: shelve.py    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 16
Project: NiujiaoDebugger   Author: MrSrc   File: shelve.py    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 17
Project: NiujiaoDebugger   Author: MrSrc   File: shelve.py    GNU General Public License v3.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 18
Project: NiujiaoDebugger   Author: MrSrc   File: shelve.py    GNU General Public License v3.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 19
Project: recruit   Author: Frank-qlu   File: pickle_compat.py    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 20
Project: recruit   Author: Frank-qlu   File: pickle_compat.py    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 21
Project: rainmachine-developer-resources   Author: sprinkler   File: rmParserUserData.py    GNU General Public License v3.0 5 votes vote down vote up
def RMUserData_convertFromSQLite(data):
    if data == None:
        return None

    inputStream = StringIO(data)
    unpickler = pickle.Unpickler(inputStream)
    return unpickler.load() 
Example 22
Project: rainmachine-developer-resources   Author: sprinkler   File: rmParserParams.py    GNU General Public License v3.0 5 votes vote down vote up
def RMParserParams_convertFromSQLite(data):
    if data == None:
        return None

    inputStream = StringIO(data)
    unpickler = pickle.Unpickler(inputStream)
    return unpickler.load() 
Example 23
Project: FUTU_Stop_Loss   Author: BigtoC   File: pickle_compat.py    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 24
Project: FUTU_Stop_Loss   Author: BigtoC   File: pickle_compat.py    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 
Example 25
Project: ironpython2   Author: IronLanguages   File: shelve.py    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 26
Project: ironpython2   Author: IronLanguages   File: shelve.py    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 27
Project: ironpython2   Author: IronLanguages   File: shelve.py    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 28
Project: ironpython2   Author: IronLanguages   File: shelve.py    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 29
Project: ironpython2   Author: IronLanguages   File: shelve.py    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 30
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    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 31
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    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 32
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    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 33
Project: ironpython2   Author: IronLanguages   File: test_cPickle.py    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 34
Project: Reinforcement-Learning-Branch-and-Bound   Author: yudasong   File: Coach.py    MIT License 5 votes vote down vote up
def loadTrainExamples(self):
        modelFile = os.path.join(self.args.load_folder_file[0], self.args.load_folder_file[1])
        examplesFile = modelFile+".examples"
        if not os.path.isfile(examplesFile):
            print(examplesFile)
            r = input("File with trainExamples not found. Continue? [y|n]")
            if r != "y":
                sys.exit()
        else:
            print("File with trainExamples found. Read it.")
            with open(examplesFile, "rb") as f:
                self.trainExamplesHistory = Unpickler(f).load()
            f.closed
            # examples based on the model were already collected (loaded)
            self.skipFirstSelfPlay = True 
Example 35
Project: Reinforcement-Learning-Branch-and-Bound   Author: yudasong   File: REINFORCE.py    MIT License 5 votes vote down vote up
def loadTrainExamples(self):
        modelFile = os.path.join(self.args.load_folder_file[0], self.args.load_folder_file[1])
        examplesFile = modelFile+".examples"
        if not os.path.isfile(examplesFile):
            print(examplesFile)
            r = input("File with trainExamples not found. Continue? [y|n]")
            if r != "y":
                sys.exit()
        else:
            print("File with trainExamples found. Read it.")
            with open(examplesFile, "rb") as f:
                self.trainExamplesHistory = Unpickler(f).load()
            f.closed
            # examples based on the model were already collected (loaded)
            self.skipFirstSelfPlay = True 
Example 36
Project: Reinforcement-Learning-Branch-and-Bound   Author: yudasong   File: ActorCritic.py    MIT License 5 votes vote down vote up
def loadTrainExamples(self):
        modelFile = os.path.join(self.args.load_folder_file[0], self.args.load_folder_file[1])
        examplesFile = modelFile+".examples"
        if not os.path.isfile(examplesFile):
            print(examplesFile)
            r = input("File with trainExamples not found. Continue? [y|n]")
            if r != "y":
                sys.exit()
        else:
            print("File with trainExamples found. Read it.")
            with open(examplesFile, "rb") as f:
                self.trainExamplesHistory = Unpickler(f).load()
            f.closed
            # examples based on the model were already collected (loaded)
            self.skipFirstSelfPlay = True 
Example 37
Project: vnpy_crypto   Author: birforce   File: pickle_compat.py    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 38
Project: vnpy_crypto   Author: birforce   File: pickle_compat.py    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 
Example 39
Project: Blockly-rduino-communication   Author: technologiescollege   File: shelve.py    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 40
Project: Blockly-rduino-communication   Author: technologiescollege   File: shelve.py    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 41
Project: Blockly-rduino-communication   Author: technologiescollege   File: shelve.py    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 42
Project: Blockly-rduino-communication   Author: technologiescollege   File: shelve.py    GNU General Public License v3.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 43
Project: Blockly-rduino-communication   Author: technologiescollege   File: shelve.py    GNU General Public License v3.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 44
Project: IronHydra   Author: microdee   File: shelve.py    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 45
Project: IronHydra   Author: microdee   File: shelve.py    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 46
Project: IronHydra   Author: microdee   File: shelve.py    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 47
Project: IronHydra   Author: microdee   File: shelve.py    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 48
Project: IronHydra   Author: microdee   File: shelve.py    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 49
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def checkExec():
        if "--forkExecProc" in sys.argv:
            try:
                import better_exchook
            except ImportError:
                pass    # Doesn't matter.
            else:
                better_exchook.install()
            argidx = sys.argv.index("--forkExecProc")
            writeFileNo = int(sys.argv[argidx + 1])
            readFileNo = int(sys.argv[argidx + 2])
            readend = os.fdopen(readFileNo, "r")
            writeend = os.fdopen(writeFileNo, "w")
            unpickler = Unpickler(readend)
            name = unpickler.load()
            if ExecingProcess.Verbose: print("ExecingProcess child %s (pid %i)" % (name, os.getpid()))
            try:
                target = unpickler.load()
                args = unpickler.load()
            except EOFError:
                print("Error: unpickle incomplete")
                raise SystemExit
            ret = target(*args)
            # IOError is probably broken pipe. That probably means that the parent died.
            try: Pickler(writeend).dump(ret)
            except IOError: pass
            try: readend.close()
            except IOError: pass
            try: writeend.close()
            except IOError: pass
            if ExecingProcess.Verbose: print("ExecingProcess child %s (pid %i) finished" % (name, os.getpid()))
            raise SystemExit 
Example 50
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def recv(self):
        self._check_closed()
        self._check_readable()
        buf = self.conn.recv_bytes()
        f = StringIO(buf)
        return Unpickler(f).load() 
Example 51
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: shelve.py    GNU General Public License v2.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 52
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: shelve.py    GNU General Public License v2.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 53
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: shelve.py    GNU General Public License v2.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 54
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: shelve.py    GNU General Public License v2.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 55
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: shelve.py    GNU General Public License v2.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 56
Project: aws-lambda-runtime-pypy   Author: uscheller   File: shelve.py    Apache License 2.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 57
Project: aws-lambda-runtime-pypy   Author: uscheller   File: shelve.py    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 58
Project: aws-lambda-runtime-pypy   Author: uscheller   File: shelve.py    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 59
Project: aws-lambda-runtime-pypy   Author: uscheller   File: shelve.py    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 60
Project: aws-lambda-runtime-pypy   Author: uscheller   File: shelve.py    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 61
Project: xuemc   Author: skycucumber   File: loading.py    GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, f, objmap, shortcuts=None):
        pickle.Unpickler.__init__(self, f)

        if shortcuts:
            objmap = dict((k % shortcuts, v % shortcuts)
                          for k, v in objmap.items())
        self._objmap = objmap 
Example 62
Project: Computable   Author: ktraunmueller   File: session.py    MIT License 5 votes vote down vote up
def load(variables=None, file=SAVEFILE, dictionary=None, verbose=False):

    """load a numpy session from a file and store the specified
    'variables' into 'dictionary'.

    'variables'       a string of comma seperated variables: e.g. "a,b,c"
                      Defaults to dictionary.keys().

    'file'            a filename or file object for the session file.

    'dictionary'      the dictionary in which to look up the variables.
                      Defaults to the caller's globals()

    'verbose'         print additional debug output when True.
    """

    global VERBOSE
    VERBOSE = verbose

    if isinstance(file, str):
        file = open(file, "rb")
    if dictionary is None:
        dictionary = _callers_globals()
    values = []
    p = pickle.Unpickler(file)
    while True:
        o = p.load()
        if isinstance(o, _SaveSession):
            session = dict(zip(o.keys, values))
            _verbose("updating dictionary with session variables.")
            if variables is None:
                keys = list(session.keys())
            else:
                keys = variables.split(",")
            for k in keys:
                dictionary[k] = session[k]
            return None
        else:
            _verbose("unpickled object", str(o))
            values.append(o) 
Example 63
Project: Computable   Author: ktraunmueller   File: compat.py    MIT License 5 votes vote down vote up
def __init__(self, *a, **kw):
            raise NotImplementedError(
                "numpy.oldnumeric.Unpickler is not supported on Python 3") 
Example 64
Project: Computable   Author: ktraunmueller   File: pickle_compat.py    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:
        if compat:
            pandas.core.series.Series = DeprecatedSeries
            pandas.core.series.TimeSeries = DeprecatedTimeSeries
            pandas.sparse.series.SparseSeries = DeprecatedSparseSeries
            pandas.sparse.series.SparseTimeSeries = DeprecatedSparseTimeSeries
        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
    finally:
        if compat:
            pandas.core.series.Series = Series
            pandas.core.series.Series = TimeSeries
            pandas.sparse.series.SparseSeries = SparseSeries
            pandas.sparse.series.SparseTimeSeries = SparseTimeSeries 
Example 65
Project: Computable   Author: ktraunmueller   File: shelve.py    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 66
Project: Computable   Author: ktraunmueller   File: shelve.py    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 67
Project: Computable   Author: ktraunmueller   File: shelve.py    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 68
Project: Computable   Author: ktraunmueller   File: shelve.py    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 69
Project: Computable   Author: ktraunmueller   File: shelve.py    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 70
Project: oss-ftp   Author: aliyun   File: shelve.py    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 71
Project: oss-ftp   Author: aliyun   File: shelve.py    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 72
Project: oss-ftp   Author: aliyun   File: shelve.py    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 73
Project: oss-ftp   Author: aliyun   File: shelve.py    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 74
Project: oss-ftp   Author: aliyun   File: shelve.py    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 75
Project: oss-ftp   Author: aliyun   File: test_pickle.py    MIT License 5 votes vote down vote up
def loads(self, buf):
        f = StringIO(buf)
        u = pickle.Unpickler(f)
        return u.load() 
Example 76
Project: oss-ftp   Author: aliyun   File: test_pickle.py    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 77
Project: RSSMOSPipeline   Author: mattyowl   File: RSSMOSTools.py    GNU General Public License v3.0 4 votes vote down vote up
def selectBestRefModel(modelFileNameList, arcData, thresholdSigma = 2.0):
    """Returns the reference arc model which maximises the cross correlation with the arc data.
    
    """

    bestCorrMaxList=[]
    bestFitShiftList=[]
    bestFitScaleList=[]
    refModelDictsList=[]
    arcFeatureTablesList=[]
    arcSegMapsList=[]
    fitDictList=[]
    for modelFileName in modelFileNameList:
    
        # Load reference model
        pickleFile=open(modelFileName, "rb")
        unpickler=pickle.Unpickler(pickleFile, **PICKLE_OPTIONS)
        refModelDict=unpickler.load()
        refModelDictsList.append(refModelDict)
        pickleFile.close()
        
        # Helps for tracking later
        refModelDict['modelFileName']=os.path.split(modelFileName)[-1]
        
        # First need to find arc features
        arcFeatureTable, arcSegMap=detectLines(arcData, thresholdSigma = thresholdSigma)
        arcFeatureTablesList.append(arcFeatureTable)
        arcSegMapsList.append(arcSegMap)
        
        # Replaced np.correlate with fft based correlation
        # Find shift and wavelength dependent scale change (stretch, then shift)
        arcRow=arcData[int(arcData.shape[0]/2)]
        bestCorrMax, bestFitScale, bestFitShift=findScaleAndShift(arcRow, refModelDict, numScales = 11)
        bestCorrMaxList.append(bestCorrMax)
        bestFitScaleList.append(bestFitScale)
        bestFitShiftList.append(bestFitShift)
 
    # So... which arc model worked best? Use it...
    bestModelIndex=np.argmax(bestCorrMaxList)
    refModelDict=refModelDictsList[bestModelIndex]
    arcFeatureTable=arcFeatureTablesList[bestModelIndex]
    arcSegMap=arcSegMapsList[bestModelIndex]
    bestFitScale=bestFitScaleList[bestModelIndex]
    bestFitShift=bestFitShiftList[bestModelIndex]
    
    print("... using refModel = %s ..." % (modelFileNameList[bestModelIndex]))
    
    return refModelDict, arcFeatureTable, arcSegMap
    
#------------------------------------------------------------------------------------------------------------- 
Example 78
Project: ironpython2   Author: IronLanguages   File: test_cPickle.py    Apache License 2.0 4 votes vote down vote up
def test_persistent_load(self):
        class MyData(object):
            def __init__(self, value):
                self.value = value

        def persistent_id(obj):
            if hasattr(obj, 'value'):
                return 'MyData: %s' % obj.value
            return None

        def persistent_load(id):
            return MyData(id[8:])


        for binary in [True, False]:
            src = StringIO()
            p = cPickle.Pickler(src)
            p.persistent_id = persistent_id
            p.binary = binary

            value = MyData('abc')
            p.dump(value)

            up = cPickle.Unpickler(StringIO(src.getvalue()))
            up.persistent_load = persistent_load
            res = up.load()

            self.assertEqual(res.value, value.value)

            # errors
            src = StringIO()
            p = cPickle.Pickler(src)
            p.persistent_id = persistent_id
            p.binary = binary

            value = MyData('abc')
            p.dump(value)

            up = cPickle.Unpickler(StringIO(src.getvalue()))

            # exceptions vary between cPickle & Pickle
            try:
                up.load()
                self.assertUnreachable()
            except Exception, e:
                pass 
Example 79
Project: avocado-vt   Author: avocado-framework   File: messenger.py    GNU General Public License v2.0 4 votes vote down vote up
def read_msg(self, timeout=None):
        """
        Read data from com interface.

        :param timeout: timeout for reading data.
        :type timeout: float
        :return: (True, data) when reading is successful.
                 (False, None) when other side is closed.
                 (None, None) when reading is timeouted.
        """
        data = self._read_until_len(timeout)
        if data is None:
            return (None, None)
        if len(data) == 0:
            return (False, None)
        rdata = None
        try:
            cmd_len = int(data)
            rdata = ""
            rdata_len = 0
            while (rdata_len < cmd_len):
                rdata += self.stdin.read(cmd_len - rdata_len)
                rdata_len = len(rdata)
            rdataIO = StringIO(self.stdin.decode(rdata))
            unp = cPickle.Unpickler(rdataIO)
            if cPickle.__name__ == 'pickle':
                unp.find_class = _map_path
            else:
                unp.find_global = _map_path
            data = unp.load()
        except Exception as e:
            logging.error("ERROR data:%s rdata:%s" % (data, rdata))
            try:
                self.write_msg(remote_interface.MessengerError("Communication "
                                                               "failed.%s" % (e)))
            except OSError:
                pass
            self.flush_stdin()
            raise
        # Debugging commands.
        # if (isinstance(data, remote_interface.BaseCmd)):
        #    print data.func
        return (True, data) 
Example 80
Project: open-recipe   Author: dspray95   File: gencache.py    The Unlicense 4 votes vote down vote up
def _LoadDicts():
	# Load the dictionary from a .zip file if that is where we live.
	if is_zip:
		import io as io
		loader = win32com.__loader__
		arc_path = loader.archive
		dicts_path = os.path.join(win32com.__gen_path__, "dicts.dat")
		if dicts_path.startswith(arc_path):
			dicts_path = dicts_path[len(arc_path)+1:]
		else:
			# Hm. See below.
			return
		try:
			data = loader.get_data(dicts_path)
		except AttributeError:
			# The __loader__ has no get_data method.  See below.
			return
		except IOError:
			# Our gencache is in a .zip file (and almost certainly readonly)
			# but no dicts file.  That actually needn't be fatal for a frozen
			# application.  Assuming they call "EnsureModule" with the same
			# typelib IDs they have been frozen with, that EnsureModule will
			# correctly re-build the dicts on the fly.  However, objects that
			# rely on the gencache but have not done an EnsureModule will
			# fail (but their apps are likely to fail running from source
			# with a clean gencache anyway, as then they would be getting
			# Dynamic objects until the cache is built - so the best answer
			# for these apps is to call EnsureModule, rather than freezing
			# the dict)
			return
		f = io.StringIO(data)
	else:
		# NOTE: IOError on file open must be caught by caller.
		f = open(os.path.join(win32com.__gen_path__, "dicts.dat"), "rb")
	try:
		p = pickle.Unpickler(f)
		version = p.load()
		global clsidToTypelib
		clsidToTypelib = p.load()
		versionRedirectMap.clear()
	finally:
		f.close()