Python pickle.Pickler() Examples

The following are code examples for showing how to use pickle.Pickler(). 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: RSSMOSPipeline   Author: mattyowl   File: fixRefModel.py    GNU General Public License v3.0 6 votes vote down vote up
def makeModelArcSpectrum(data, approxCoordsDict, outFileName, yRow, sigmaCut = 3.0, thresholdSigma = 5.0, 
                         featureMinPix = 30):
    """Make reference model arc spectrum. This has wavelengths of features identified in a table. We also
    save the middle row of the spectrum.
    
    """
    
    # Detect and tag features with known wavelengths in reference spectrum
    featureTable, segMap=detectLines(data)
    featureTable=tagWavelengthFeatures(featureTable, approxCoordsDict)
    data_centreRow=data[yRow]
    
    # Save reference model as a pickled dictionary
    refModelDict={'featureTable': featureTable, 'arc_centreRow': data_centreRow}
    pickleFile=file(outFileName, "wb")
    pickler=pickle.Pickler(pickleFile)
    pickler.dump(refModelDict)
    pickleFile.close()
    
#------------------------------------------------------------------------------------------------------------- 
Example 2
Project: FalltoSkyBot   Author: Sakiut   File: casino.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def post(server, user, data, money):
	if server.name in data.keys():
		if user.name in data[server.name].keys():
			data[server.name][user.name] = money
		else:
			uniqueData = {user.name: 500}
			data[server.name].update(uniqueData)
	else:
		uniqueData = {server.name:{user.name: 500}}
		data.update(uniqueData)

	f = open(fileName, "wb")
	p = pickle.Pickler(f)
	p.dump(data)
	f.close()

	return data

######################################################################################################################## 
Example 3
Project: FalltoSkyBot   Author: Sakiut   File: moderation.py    GNU Affero General Public License v3.0 6 votes vote down vote up
def warn(server, user, data):
	if server.name in data.keys():
		if user.name in data[server.name].keys():
			data[server.name][user.name] += 1
		else:
			uniqueData = {user.name: 1}
			data[server.name].update(uniqueData)
	else:
		uniqueData = {server.name:{user.name: 1}}
		data.update(uniqueData)

	f = open(fileName, "wb")
	p = pickle.Pickler(f)
	p.dump(data)
	f.close()

	return data

##################################################################################################################################################### 
Example 4
Project: NiujiaoDebugger   Author: MrSrc   File: test_pickle.py    GNU General Public License v3.0 6 votes vote down vote up
def test_pickler(self):
            basesize = support.calcobjsize('6P2n3i2n3iP')
            p = _pickle.Pickler(io.BytesIO())
            self.assertEqual(object.__sizeof__(p), basesize)
            MT_size = struct.calcsize('3nP0n')
            ME_size = struct.calcsize('Pn0P')
            check = self.check_sizeof
            check(p, basesize +
                MT_size + 8 * ME_size +  # Minimal memo table size.
                sys.getsizeof(b'x'*4096))  # Minimal write buffer size.
            for i in range(6):
                p.dump(chr(i))
            check(p, basesize +
                MT_size + 32 * ME_size +  # Size of memo table required to
                                          # save references to 6 objects.
                0)  # Write buffer is cleared after every dump(). 
Example 5
Project: D-VAE   Author: muhanzhang   File: pkl_utils.py    MIT License 6 votes vote down vote up
def save(self, obj):
        # Remove the tag.trace attribute from Variable and Apply nodes
        if isinstance(obj, theano.gof.utils.scratchpad):
            for tag in self.tag_to_remove:
                if hasattr(obj, tag):
                    del obj.__dict__[tag]
        # Remove manually-added docstring of Elemwise ops
        elif (isinstance(obj, theano.tensor.Elemwise)):
            if '__doc__' in obj.__dict__:
                del obj.__dict__['__doc__']

        return Pickler.save(self, obj)


# Make an unpickler that tries encoding byte streams before raising TypeError.
# This is useful with python 3, in order to unpickle files created with
# python 2.
# This code is taken from Pandas, https://github.com/pydata/pandas,
# under the same 3-clause BSD license. 
Example 6
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 6 votes vote down vote up
def save(self, obj):
        if isinstance(obj, (types.MethodType, type({}.pop))):
            # the Pickler cannot pickle instance methods; here we decompose
            # them into components that make them uniquely identifiable
            if hasattr(obj, '__func__'):
                func_name = obj.__func__.__name__
            else:
                func_name = obj.__name__
            inst = obj.__self__
            if type(inst) == type(pickle):
                obj = _MyHash(func_name, inst.__name__)
            elif inst is None:
                # type(None) or type(module) do not pickle
                obj = _MyHash(func_name, inst)
            else:
                cls = obj.__self__.__class__
                obj = _MyHash(func_name, inst, cls)
        Pickler.save(self, obj) 
Example 7
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 6 votes vote down vote up
def save_global(self, obj, name=None, pack=struct.pack):
        # We have to override this method in order to deal with objects
        # defined interactively in IPython that are not injected in
        # __main__
        kwargs = dict(name=name, pack=pack)
        if sys.version_info >= (3, 4):
            del kwargs['pack']
        try:
            Pickler.save_global(self, obj, **kwargs)
        except pickle.PicklingError:
            Pickler.save_global(self, obj, **kwargs)
            module = getattr(obj, "__module__", None)
            if module == '__main__':
                my_name = name
                if my_name is None:
                    my_name = obj.__name__
                mod = sys.modules[module]
                if not hasattr(mod, my_name):
                    # IPython doesn't inject the variables define
                    # interactively in __main__
                    setattr(mod, my_name, obj) 
Example 8
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 6 votes vote down vote up
def save(self, obj):
        if isinstance(obj, (types.MethodType, type({}.pop))):
            # the Pickler cannot pickle instance methods; here we decompose
            # them into components that make them uniquely identifiable
            if hasattr(obj, '__func__'):
                func_name = obj.__func__.__name__
            else:
                func_name = obj.__name__
            inst = obj.__self__
            if type(inst) == type(pickle):
                obj = _MyHash(func_name, inst.__name__)
            elif inst is None:
                # type(None) or type(module) do not pickle
                obj = _MyHash(func_name, inst)
            else:
                cls = obj.__self__.__class__
                obj = _MyHash(func_name, inst, cls)
        Pickler.save(self, obj) 
Example 9
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 6 votes vote down vote up
def save_global(self, obj, name=None, pack=struct.pack):
        # We have to override this method in order to deal with objects
        # defined interactively in IPython that are not injected in
        # __main__
        kwargs = dict(name=name, pack=pack)
        if sys.version_info >= (3, 4):
            del kwargs['pack']
        try:
            Pickler.save_global(self, obj, **kwargs)
        except pickle.PicklingError:
            Pickler.save_global(self, obj, **kwargs)
            module = getattr(obj, "__module__", None)
            if module == '__main__':
                my_name = name
                if my_name is None:
                    my_name = obj.__name__
                mod = sys.modules[module]
                if not hasattr(mod, my_name):
                    # IPython doesn't inject the variables define
                    # interactively in __main__
                    setattr(mod, my_name, obj) 
Example 10
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_pickle.py    GNU General Public License v2.0 6 votes vote down vote up
def test_pickler(self):
            basesize = support.calcobjsize('6P2n3i2n3iP')
            p = _pickle.Pickler(io.BytesIO())
            self.assertEqual(object.__sizeof__(p), basesize)
            MT_size = struct.calcsize('3nP0n')
            ME_size = struct.calcsize('Pn0P')
            check = self.check_sizeof
            check(p, basesize +
                MT_size + 8 * ME_size +  # Minimal memo table size.
                sys.getsizeof(b'x'*4096))  # Minimal write buffer size.
            for i in range(6):
                p.dump(chr(i))
            check(p, basesize +
                MT_size + 32 * ME_size +  # Size of memo table required to
                                          # save references to 6 objects.
                0)  # Write buffer is cleared after every dump(). 
Example 11
Project: poker   Author: surgebiswas   File: pkl_utils.py    MIT License 6 votes vote down vote up
def save(self, obj):
        # Remove the tag.trace attribute from Variable and Apply nodes
        if isinstance(obj, theano.gof.utils.scratchpad):
            for tag in self.tag_to_remove:
                if hasattr(obj, tag):
                    del obj.__dict__[tag]
        # Remove manually-added docstring of Elemwise ops
        elif (isinstance(obj, theano.tensor.Elemwise)):
            if '__doc__' in obj.__dict__:
                del obj.__dict__['__doc__']

        return Pickler.save(self, obj)


# Make an unpickler that tries encoding byte streams before raising TypeError.
# This is useful with python 3, in order to unpickle files created with
# python 2.
# This code is taken from Pandas, https://github.com/pydata/pandas,
# under the same 3-clause BSD license. 
Example 12
Project: RPGOne   Author: RTHMaK   File: hashing.py    Apache License 2.0 6 votes vote down vote up
def save(self, obj):
        if isinstance(obj, (types.MethodType, type({}.pop))):
            # the Pickler cannot pickle instance methods; here we decompose
            # them into components that make them uniquely identifiable
            if hasattr(obj, '__func__'):
                func_name = obj.__func__.__name__
            else:
                func_name = obj.__name__
            inst = obj.__self__
            if type(inst) == type(pickle):
                obj = _MyHash(func_name, inst.__name__)
            elif inst is None:
                # type(None) or type(module) do not pickle
                obj = _MyHash(func_name, inst)
            else:
                cls = obj.__self__.__class__
                obj = _MyHash(func_name, inst, cls)
        Pickler.save(self, obj) 
Example 13
Project: RPGOne   Author: RTHMaK   File: hashing.py    Apache License 2.0 6 votes vote down vote up
def save_global(self, obj, name=None, pack=struct.pack):
        # We have to override this method in order to deal with objects
        # defined interactively in IPython that are not injected in
        # __main__
        kwargs = dict(name=name, pack=pack)
        if sys.version_info >= (3, 4):
            del kwargs['pack']
        try:
            Pickler.save_global(self, obj, **kwargs)
        except pickle.PicklingError:
            Pickler.save_global(self, obj, **kwargs)
            module = getattr(obj, "__module__", None)
            if module == '__main__':
                my_name = name
                if my_name is None:
                    my_name = obj.__name__
                mod = sys.modules[module]
                if not hasattr(mod, my_name):
                    # IPython doesn't inject the variables define
                    # interactively in __main__
                    setattr(mod, my_name, obj) 
Example 14
Project: attention-lvcsr   Author: rizar   File: pkl_utils.py    MIT License 6 votes vote down vote up
def save(self, obj):
        # Remove the tag.trace attribute from Variable and Apply nodes
        if isinstance(obj, theano.gof.utils.scratchpad):
            for tag in self.tag_to_remove:
                if hasattr(obj, tag):
                    del obj.__dict__[tag]
        # Remove manually-added docstring of Elemwise ops
        elif (isinstance(obj, theano.tensor.Elemwise)):
            if '__doc__' in obj.__dict__:
                del obj.__dict__['__doc__']

        return Pickler.save(self, obj)


# Make an unpickler that tries encoding byte streams before raising TypeError.
# This is useful with python 3, in order to unpickle files created with
# python 2.
# This code is taken from Pandas, https://github.com/pydata/pandas,
# under the same 3-clause BSD license. 
Example 15
Project: pyblish-win   Author: pyblish   File: shelve.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = StringIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key] = f.getvalue() 
Example 16
Project: pyblish-win   Author: pyblish   File: test_pickle.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def dumps(self, arg, proto=0, fast=0):
        f = StringIO()
        p = pickle.Pickler(f, proto)
        if fast:
            p.fast = fast
        p.dump(arg)
        f.seek(0)
        return f.read() 
Example 17
Project: pyblish-win   Author: pyblish   File: test_pickle.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def dumps(self, arg, proto=0, fast=0):
        class PersPickler(pickle.Pickler):
            def persistent_id(subself, obj):
                return self.persistent_id(obj)
        f = StringIO()
        p = PersPickler(f, proto)
        if fast:
            p.fast = fast
        p.dump(arg)
        f.seek(0)
        return f.read() 
Example 18
Project: NiujiaoDebugger   Author: MrSrc   File: shelve.py    GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = BytesIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key.encode(self.keyencoding)] = f.getvalue() 
Example 19
Project: NiujiaoDebugger   Author: MrSrc   File: test_pickle.py    GNU General Public License v3.0 5 votes vote down vote up
def test_pickler_reference_cycle(self):
        def check(Pickler):
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                f = io.BytesIO()
                pickler = Pickler(f, proto)
                pickler.dump('abc')
                self.assertEqual(self.loads(f.getvalue()), 'abc')
            pickler = Pickler(io.BytesIO())
            self.assertEqual(pickler.persistent_id('def'), 'def')
            r = weakref.ref(pickler)
            del pickler
            self.assertIsNone(r())

        class PersPickler(self.pickler):
            def persistent_id(subself, obj):
                return obj
        check(PersPickler)

        class PersPickler(self.pickler):
            @classmethod
            def persistent_id(cls, obj):
                return obj
        check(PersPickler)

        class PersPickler(self.pickler):
            @staticmethod
            def persistent_id(obj):
                return obj
        check(PersPickler) 
Example 20
Project: 2018-1-OSS-E7   Author: 18-1-SKKU-OSS   File: CNCList.py    MIT License 5 votes vote down vote up
def copy(self, event=None):
		sio = StringIO()
		pickler = pickle.Pickler(sio)
		#sio.write(_PLOT_CLIP)
		for block,line in self.getCleanSelection():
			if line is None:
				pickler.dump(self.gcode.blocks[block].dump())
			else:
				pickler.dump(self.gcode.blocks[block][line])
		self.clipboard_clear()
		self.clipboard_append(sio.getvalue())
		return "break"

	# ---------------------------------------------------------------------- 
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_adaptToSQLite(userData):
    if userData == None:
        return None

    outputStream = StringIO()
    pickler = pickle.Pickler(outputStream)
    pickler.dump(userData)
    return outputStream.getvalue() 
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_adaptToSQLite(params):
    if params == None:
        return None

    outputStream = StringIO()
    pickler = pickle.Pickler(outputStream)
    pickler.dump(params)
    return outputStream.getvalue() 
Example 23
Project: ironpython2   Author: IronLanguages   File: shelve.py    Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = StringIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key] = f.getvalue() 
Example 24
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    Apache License 2.0 5 votes vote down vote up
def dumps(self, arg, proto=0, fast=0):
        f = StringIO()
        p = pickle.Pickler(f, proto)
        if fast:
            p.fast = fast
        p.dump(arg)
        f.seek(0)
        return f.read() 
Example 25
Project: ironpython2   Author: IronLanguages   File: test_pickle.py    Apache License 2.0 5 votes vote down vote up
def dumps(self, arg, proto=0, fast=0):
        class PersPickler(pickle.Pickler):
            def persistent_id(subself, obj):
                return self.persistent_id(obj)
        f = StringIO()
        p = PersPickler(f, proto)
        if fast:
            p.fast = fast
        p.dump(arg)
        f.seek(0)
        return f.read() 
Example 26
Project: ironpython2   Author: IronLanguages   File: test_cPickle.py    Apache License 2.0 5 votes vote down vote up
def test_pers_load(self):
        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 betwee cPickle & Pickle
            try:
                up.load()
                self.assertUnreachable()
            except Exception, e:
                pass 
Example 27
Project: D-VAE   Author: muhanzhang   File: pkl_utils.py    MIT License 5 votes vote down vote up
def __init__(self, file, protocol=0, extra_tag_to_remove=None):
        # Can't use super as Pickler isn't a new style class
        Pickler.__init__(self, file, protocol)
        self.tag_to_remove = ['trace', 'test_value']
        if extra_tag_to_remove:
            self.tag_to_remove.extend(extra_tag_to_remove) 
Example 28
Project: open-recipe   Author: dspray95   File: gencache.py    The Unlicense 5 votes vote down vote up
def _SaveDicts():
	if is_readonly:
		raise RuntimeError("Trying to write to a readonly gencache ('%s')!" \
		                    % win32com.__gen_path__)
	f = open(os.path.join(GetGeneratePath(), "dicts.dat"), "wb")
	try:
		p = pickle.Pickler(f)
		p.dump(pickleVersion)
		p.dump(clsidToTypelib)
	finally:
		f.close() 
Example 29
Project: Blockly-rduino-communication   Author: technologiescollege   File: shelve.py    GNU General Public License v3.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = BytesIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key.encode(self.keyencoding)] = f.getvalue() 
Example 30
Project: IronHydra   Author: microdee   File: shelve.py    MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = StringIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key] = f.getvalue() 
Example 31
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 5 votes vote down vote up
def __init__(self, hash_name='md5'):
        self.stream = io.BytesIO()
        # By default we want a pickle protocol that only changes with
        # the major python version and not the minor one
        protocol = (pickle.DEFAULT_PROTOCOL if PY3_OR_LATER
                    else pickle.HIGHEST_PROTOCOL)
        Pickler.__init__(self, self.stream, protocol=protocol)
        # Initialise the hash obj
        self._hash = hashlib.new(hash_name) 
Example 32
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 5 votes vote down vote up
def memoize(self, obj):
        # We want hashing to be sensitive to value instead of reference.
        # For example we want ['aa', 'aa'] and ['aa', 'aaZ'[:2]]
        # to hash to the same value and that's why we disable memoization
        # for strings
        if isinstance(obj, _bytes_or_unicode):
            return
        Pickler.memoize(self, obj)

    # The dispatch table of the pickler is not accessible in Python
    # 3, as these lines are only bugware for IPython, we skip them. 
Example 33
Project: estimators   Author: fridiculous   File: hashing.py    MIT License 5 votes vote down vote up
def _batch_setitems(self, items):
        # forces order of keys in dict to ensure consistent hash.
        try:
            # Trying first to compare dict assuming the type of keys is
            # consistent and orderable.
            # This fails on python 3 when keys are unorderable
            # but we keep it in a try as it's faster.
            Pickler._batch_setitems(self, iter(sorted(items)))
        except TypeError:
            # If keys are unorderable, sorting them using their hash. This is
            # slower but works in any case.
            Pickler._batch_setitems(self, iter(sorted((hash(k), v)
                                                      for k, v in items))) 
Example 34
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def __init__(self, *args, **kwargs):
        if not "protocol" in kwargs:
            kwargs["protocol"] = pickle.HIGHEST_PROTOCOL
        pickle.Pickler.__init__(self, *args, **kwargs) 
Example 35
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 36
Project: deepjets   Author: deepjets   File: tasksystem.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def send(self, value):
        self._check_closed()
        self._check_writable()
        buf = StringIO()
        Pickler(buf).dump(value)
        self.conn.send_bytes(buf.getvalue()) 
Example 37
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 5 votes vote down vote up
def __init__(self, hash_name='md5'):
        self.stream = io.BytesIO()
        # By default we want a pickle protocol that only changes with
        # the major python version and not the minor one
        protocol = (pickle.DEFAULT_PROTOCOL if PY3_OR_LATER
                    else pickle.HIGHEST_PROTOCOL)
        Pickler.__init__(self, self.stream, protocol=protocol)
        # Initialise the hash obj
        self._hash = hashlib.new(hash_name) 
Example 38
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 5 votes vote down vote up
def memoize(self, obj):
        # We want hashing to be sensitive to value instead of reference.
        # For example we want ['aa', 'aa'] and ['aa', 'aaZ'[:2]]
        # to hash to the same value and that's why we disable memoization
        # for strings
        if isinstance(obj, _bytes_or_unicode):
            return
        Pickler.memoize(self, obj)

    # The dispatch table of the pickler is not accessible in Python
    # 3, as these lines are only bugware for IPython, we skip them. 
Example 39
Project: mlens   Author: flennerhag   File: hashing.py    MIT License 5 votes vote down vote up
def _batch_setitems(self, items):
        # forces order of keys in dict to ensure consistent hash.
        try:
            # Trying first to compare dict assuming the type of keys is
            # consistent and orderable.
            # This fails on python 3 when keys are unorderable
            # but we keep it in a try as it's faster.
            Pickler._batch_setitems(self, iter(sorted(items)))
        except TypeError:
            # If keys are unorderable, sorting them using their hash. This is
            # slower but works in any case.
            Pickler._batch_setitems(self, iter(sorted((hash(k), v)
                                                      for k, v in items))) 
Example 40
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 __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = BytesIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key.encode(self.keyencoding)] = f.getvalue() 
Example 41
Project: cqp-sdk-for-py37-native   Author: crud-boy   File: test_pickle.py    GNU General Public License v2.0 5 votes vote down vote up
def test_pickler_reference_cycle(self):
        def check(Pickler):
            for proto in range(pickle.HIGHEST_PROTOCOL + 1):
                f = io.BytesIO()
                pickler = Pickler(f, proto)
                pickler.dump('abc')
                self.assertEqual(self.loads(f.getvalue()), 'abc')
            pickler = Pickler(io.BytesIO())
            self.assertEqual(pickler.persistent_id('def'), 'def')
            r = weakref.ref(pickler)
            del pickler
            self.assertIsNone(r())

        class PersPickler(self.pickler):
            def persistent_id(subself, obj):
                return obj
        check(PersPickler)

        class PersPickler(self.pickler):
            @classmethod
            def persistent_id(cls, obj):
                return obj
        check(PersPickler)

        class PersPickler(self.pickler):
            @staticmethod
            def persistent_id(obj):
                return obj
        check(PersPickler) 
Example 42
Project: aws-lambda-runtime-pypy   Author: uscheller   File: shelve.py    Apache License 2.0 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = BytesIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key.encode(self.keyencoding)] = f.getvalue() 
Example 43
Project: Computable   Author: ktraunmueller   File: shelve.py    MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = StringIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key] = f.getvalue() 
Example 44
Project: oss-ftp   Author: aliyun   File: shelve.py    MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = StringIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key] = f.getvalue() 
Example 45
Project: oss-ftp   Author: aliyun   File: test_pickle.py    MIT License 5 votes vote down vote up
def dumps(self, arg, proto=0, fast=0):
        f = StringIO()
        p = pickle.Pickler(f, proto)
        if fast:
            p.fast = fast
        p.dump(arg)
        f.seek(0)
        return f.read() 
Example 46
Project: oss-ftp   Author: aliyun   File: test_pickle.py    MIT License 5 votes vote down vote up
def dumps(self, arg, proto=0, fast=0):
        class PersPickler(pickle.Pickler):
            def persistent_id(subself, obj):
                return self.persistent_id(obj)
        f = StringIO()
        p = PersPickler(f, proto)
        if fast:
            p.fast = fast
        p.dump(arg)
        f.seek(0)
        return f.read() 
Example 47
Project: poker   Author: surgebiswas   File: pkl_utils.py    MIT License 5 votes vote down vote up
def __init__(self, file, protocol=0, extra_tag_to_remove=None):
        # Can't use super as Pickler isn't a new style class
        Pickler.__init__(self, file, protocol)
        self.tag_to_remove = ['trace', 'test_value']
        if extra_tag_to_remove:
            self.tag_to_remove.extend(extra_tag_to_remove) 
Example 48
Project: goldminer   Author: adael   File: filesave.py    MIT License 5 votes vote down vote up
def save_world(world):
    with open(filename, 'wb') as fout:
        pickler = pickle.Pickler(fout, -1)
        pickler.dump(world) 
Example 49
Project: MachineLearningSamples-ImageClassificationUsingCntk   Author: Azure-Samples   File: utilities_general_v2.py    MIT License 5 votes vote down vote up
def writePickle(outputFile, data):
    p = pickle.Pickler(open(outputFile,"wb"))
    p.fast = True
    p.dump(data) 
Example 50
Project: setup   Author: mindbender-studio   File: shelve.py    MIT License 5 votes vote down vote up
def __setitem__(self, key, value):
        if self.writeback:
            self.cache[key] = value
        f = BytesIO()
        p = Pickler(f, self._protocol)
        p.dump(value)
        self.dict[key.encode(self.keyencoding)] = f.getvalue()