Python pickle.dumps() Examples

The following are 30 code examples of pickle.dumps(). 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: SockPuppet.py    From ALF with Apache License 2.0 7 votes vote down vote up
def run_code(self, function, *args, **kwargs):
        log.debug("%s() args:%s kwargs:%s on target", function.func_name, args, kwargs)
        data = {"cmd":self.CODE,
                "code":marshal.dumps(function.func_code),
                "name":function.func_name,
                "args":args,
                "kwargs":kwargs,
                "defaults":function.__defaults__,
                "closure":function.__closure__}
        self.send_data(data)
        log.debug("waiting for code to execute...")
        data = self.recv_data()
        if data["cmd"] == self.EXCEPT:
            log.debug("received exception")
            raise self._process_target_except(data)
        assert data["cmd"] == self.RETURN
        return data["value"] 
Example #2
Source File: cache.py    From vergeml with MIT License 6 votes vote down vote up
def _serialize_data(self, data):

        # Default to raw bytes
        type_ = _BYTES

        if isinstance(data, np.ndarray):
        # When the data is a numpy array, use the more compact native
        # numpy format.
            buf = io.BytesIO()
            np.save(buf, data)
            data = buf.getvalue()
            type_ = _NUMPY

        elif not isinstance(data, (bytearray, bytes)):
        # Everything else except byte data is serialized in pickle format.
            data = pickle.dumps(data)
            type_ = _PICKLE

        if self.compress:
        # Optional compression
            data = lz4.frame.compress(data)

        return type_, data 
Example #3
Source File: adventure.py    From Dumb-Cogs with MIT License 6 votes vote down vote up
def t_suspend(self, verb, obj):
        if isinstance(obj, str):
            if os.path.exists(obj):  # pragma: no cover
                self.write('I refuse to overwrite an existing file.')
                return
            savefile = open(obj, 'wb')
        else:
            savefile = obj
        r = self.random_generator  # must replace live object with static state
        self.random_state = r.getstate()
        try:
            del self.random_generator
            savefile.write(zlib.compress(pickle.dumps(self), 9))
        finally:
            self.random_generator = r
            if savefile is not obj:
                savefile.close()
        self.write('Game saved') 
Example #4
Source File: handler.py    From pywren-ibm-cloud with Apache License 2.0 6 votes vote down vote up
def _send_status_os(self):
        """
        Send the status event to the Object Storage
        """
        executor_id = self.response['executor_id']
        job_id = self.response['job_id']
        call_id = self.response['call_id']
        act_id = self.response['activation_id']

        if self.response['type'] == '__init__':
            init_key = create_init_key(JOBS_PREFIX, executor_id, job_id, call_id, act_id)
            self.internal_storage.put_data(init_key, '')

        elif self.response['type'] == '__end__':
            status_key = create_status_key(JOBS_PREFIX, executor_id, job_id, call_id)
            dmpd_response_status = json.dumps(self.response)
            drs = sizeof_fmt(len(dmpd_response_status))
            logger.info("Storing execution stats - Size: {}".format(drs))
            self.internal_storage.put_data(status_key, dmpd_response_status) 
Example #5
Source File: test_analyze.py    From me-ica with GNU Lesser General Public License v2.1 6 votes vote down vote up
def test_pickle(self):
        # Test that images pickle
        # Image that is not proxied can pickle
        img_klass = self.image_class
        img = img_klass(np.zeros((2,3,4)), None)
        img_str = pickle.dumps(img)
        img2 = pickle.loads(img_str)
        assert_array_equal(img.get_data(), img2.get_data())
        assert_equal(img.get_header(), img2.get_header())
        # Save / reload using bytes IO objects
        for key, value in img.file_map.items():
            value.fileobj = BytesIO()
        img.to_file_map()
        img_prox = img.from_file_map(img.file_map)
        img_str = pickle.dumps(img_prox)
        img2_prox = pickle.loads(img_str)
        assert_array_equal(img.get_data(), img2_prox.get_data()) 
Example #6
Source File: smartcache.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def __getstate__(self):
        d = dict(self.__dict__)

        def get_pickleable_dict(cacheDict):
            pickleableCache = dict()
            for k, v in cacheDict.items():
                try:
                    _pickle.dumps(v)
                    pickleableCache[k] = v
                except TypeError as e:
                    if isinstance(v, dict):
                        self.unpickleable.add(str(k[0]) + str(type(v)) + str(e) + str(list(v.keys())))
                    else:
                        self.unpickleable.add(str(k[0]) + str(type(v)) + str(e))  # + str(list(v.__dict__.keys())))
                except _pickle.PicklingError as e:
                    self.unpickleable.add(str(k[0]) + str(type(v)) + str(e))
            return pickleableCache

        d['cache'] = get_pickleable_dict(self.cache)
        d['outargs'] = get_pickleable_dict(self.outargs)
        return d 
Example #7
Source File: parallel.py    From dataflow with Apache License 2.0 6 votes vote down vote up
def run(self):
            enable_death_signal(_warn=self.idx == 0)
            self.ds.reset_state()
            itr = _repeat_iter(lambda: self.ds)

            context = zmq.Context()
            socket = context.socket(zmq.PUSH)
            socket.set_hwm(self.hwm)
            socket.connect(self.conn_name)
            try:
                while True:
                    try:
                        dp = next(itr)
                        socket.send(dumps(dp), copy=False)
                    except Exception:
                        dp = _ExceptionWrapper(sys.exc_info()).pack()
                        socket.send(dumps(dp), copy=False)
                        raise
            # sigint could still propagate here, e.g. when nested
            except KeyboardInterrupt:
                pass
            finally:
                socket.close(0)
                context.destroy(0) 
Example #8
Source File: vachat.py    From The-chat-room with MIT License 6 votes vote down vote up
def run(self):
        while True:
            try:
                self.sock.connect(self.ADDR)
                break
            except:
                time.sleep(3)
                continue
        print("AUDIO client connected...")
        self.stream = self.p.open(format=FORMAT,
                             channels=CHANNELS,
                             rate=RATE,
                             input=True,
                             frames_per_buffer=CHUNK)
        while self.stream.is_active():
            frames = []
            for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
                data = self.stream.read(CHUNK)
                frames.append(data)
            senddata = pickle.dumps(frames)
            try:
                self.sock.sendall(struct.pack("L", len(senddata)) + senddata)
            except:
                break 
Example #9
Source File: test_model.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def test_pickle(self):
        # XXX what exactly does this cover and is it needed?  EGN: this tests that the individual pieces (~dicts) within a model can be pickled; it's useful for debuggin b/c often just one of these will break.
        p = pickle.dumps(self.model.preps)
        preps = pickle.loads(p)
        self.assertEqual(list(preps.keys()), list(self.model.preps.keys()))

        p = pickle.dumps(self.model.povms)
        povms = pickle.loads(p)
        self.assertEqual(list(povms.keys()), list(self.model.povms.keys()))

        p = pickle.dumps(self.model.operations)
        gates = pickle.loads(p)
        self.assertEqual(list(gates.keys()), list(self.model.operations.keys()))

        self.model._clean_paramvec()
        p = pickle.dumps(self.model)
        g = pickle.loads(p)
        g._clean_paramvec()
        self.assertAlmostEqual(self.model.frobeniusdist(g), 0.0) 
Example #10
Source File: test_case.py    From jawfish with MIT License 6 votes vote down vote up
def testPickle(self):
        # Issue 10326

        # Can't use TestCase classes defined in Test class as
        # pickle does not work with inner classes
        test = unittest.TestCase('run')
        for protocol in range(pickle.HIGHEST_PROTOCOL + 1):

            # blew up prior to fix
            pickled_test = pickle.dumps(test, protocol=protocol)
            unpickled_test = pickle.loads(pickled_test)
            self.assertEqual(test, unpickled_test)

            # exercise the TestCase instance in a way that will invoke
            # the type equality lookup mechanism
            unpickled_test.assertEqual(set(), set()) 
Example #11
Source File: handlers.py    From jawfish with MIT License 6 votes vote down vote up
def makePickle(self, record):
        """
        Pickles the record in binary format with a length prefix, and
        returns it ready for transmission across the socket.
        """
        ei = record.exc_info
        if ei:
            # just to get traceback text into record.exc_text ...
            dummy = self.format(record)
        # See issue #14436: If msg or args are objects, they may not be
        # available on the receiving end. So we convert the msg % args
        # to a string, save it as msg and zap the args.
        d = dict(record.__dict__)
        d['msg'] = record.getMessage()
        d['args'] = None
        d['exc_info'] = None
        s = pickle.dumps(d, 1)
        slen = struct.pack(">L", len(s))
        return slen + s 
Example #12
Source File: testHessian.py    From pyGSTi with Apache License 2.0 6 votes vote down vote up
def tets_pickle_ConfidenceRegion(self):
        res = pygsti.obj.Results()
        res.init_dataset(self.ds)
        res.init_circuits(self.gss)
        res.add_estimate(stdxyi.target_model(), stdxyi.target_model(),
                         [self.model]*len(self.maxLengthList), parameters={'objective': 'logl'},
                         estimate_key="default")

        res.add_confidence_region_factory('final iteration estimate', 'final')
        self.assertTrue( res.has_confidence_region_factory('final iteration estimate', 'final'))

        cfctry = res.get_confidence_region_factory('final iteration estimate', 'final')
        cfctry.compute_hessian()
        self.assertTrue( cfctry.has_hessian() )

        cfctry.project_hessian('std')
        ci_std = cfctry.view( 95.0, 'normal', 'std')

        s = pickle.dumps(cfctry)
        cifctry2 = pickle.loads(s)

        s = pickle.dumps(ci_std)
        ci_std2 = pickle.loads(s)

        #TODO: make sure ci_std and ci_std2 are the same 
Example #13
Source File: test_verbosityprinter.py    From pyGSTi with Apache License 2.0 5 votes vote down vote up
def test_pickle(self):
        s = pickle.dumps(self.vbp)
        vbp_pickled = pickle.loads(s)
        # TODO assert correctness 
Example #14
Source File: test_smartcache.py    From pyGSTi with Apache License 2.0 5 votes vote down vote up
def test_pickle(self):
        a = pickle.dumps(slow_fib.cache)
        newcache = pickle.loads(a)
        # TODO assert correctness 
Example #15
Source File: serialize.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def loads(buf):
        """
        Args:
            bytes
        """
        return pickle.loads(buf)


# Define the default serializer to be used that dumps data to bytes 
Example #16
Source File: test_multidataset.py    From pyGSTi with Apache License 2.0 5 votes vote down vote up
def test_pickle(self):
        s = pickle.dumps(self.mds)
        mds_unpickle = pickle.loads(s)
        # TODO assert correctness 
Example #17
Source File: serialize.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def loads(buf):
        """
        Args:
            buf: the output of `dumps` or `dumps_bytes`.
        """
        import pyarrow as pa
        return pa.deserialize(buf) 
Example #18
Source File: object_storage.py    From jawfish with MIT License 5 votes vote down vote up
def __contains__(self, key):
        return pickle.dumps(key) in self.storage 
Example #19
Source File: serialize.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def dumps_bytes(obj):
        """
        Returns:
            bytes
        """
        return PyarrowSerializer.dumps(obj).to_pybytes() 
Example #20
Source File: test_labeldicts.py    From pyGSTi with Apache License 2.0 5 votes vote down vote up
def test_outcome_label_dict_pickles(self):
        d = ld.OutcomeLabelDict([(('0',), 90), (('1',), 10)])
        s = pickle.dumps(d)
        d_pickle = pickle.loads(s)
        self.assertEqual(d, d_pickle) 
Example #21
Source File: serialize.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def dumps(obj):
        """
        Serialize an object.

        Returns:
            Implementation-dependent bytes-like object.
            May not be compatible across different versions of pyarrow.
        """
        import pyarrow as pa
        return pa.serialize(obj).to_buffer() 
Example #22
Source File: serialize.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def loads(buf):
        """
        Args:
            buf: the output of `dumps`.
        """
        # Since 0.6, the default max size was set to 1MB.
        # We change it to approximately 1G.
        return msgpack.loads(buf, raw=False,
                             max_bin_len=MAX_MSGPACK_LEN,
                             max_array_len=MAX_MSGPACK_LEN,
                             max_map_len=MAX_MSGPACK_LEN,
                             max_str_len=MAX_MSGPACK_LEN) 
Example #23
Source File: serialize.py    From dataflow with Apache License 2.0 5 votes vote down vote up
def dumps(obj):
        """
        Serialize an object.

        Returns:
            Implementation-dependent bytes-like object.
        """
        return msgpack.dumps(obj, use_bin_type=True) 
Example #24
Source File: wordnet_app.py    From razzy-spinner with GNU General Public License v3.0 5 votes vote down vote up
def encode(self):
        """
        Encode this reference into a string to be used in a URL.
        """
        # This uses a tuple rather than an object since the python
        # pickle representation is much smaller and there is no need
        # to represent the complete object.
        string = pickle.dumps((self.word, self.synset_relations), -1)
        return base64.urlsafe_b64encode(string).decode() 
Example #25
Source File: graph_canvas.py    From networkx_viewer with GNU General Public License v3.0 5 votes vote down vote up
def dump_visualization(self):
        """Record currently visable nodes, their position, and their widget's
        state.  Used by undo functionality and to memorize speicific displays"""

        ans = self.dispG.copy()

        # Add current x,y info to the graph
        for n, d in ans.nodes_iter(data=True):
            (d['x'],d['y']) = self.coords(d['token_id'])

        # Pickle the whole thing up
        ans = pickle.dumps(ans)

        return ans 
Example #26
Source File: url_checker.py    From YaYaGen with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __store(self, domain, detections):
        """
        Store VT detection results on a sqlite DB
        """
        query_write = '''INSERT OR REPLACE INTO `detections` VALUES(?, ?)'''
        self.db.execute(query_write, (domain, pickle.dumps(detections)))
        self.db.commit() 
Example #27
Source File: report.py    From YaYaGen with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __store(self):
        """
        Store a report in a local db, using the SHA256 as a key
        """
        query_write = '''INSERT OR REPLACE INTO `reports` VALUES(?, ?)'''
        self.__db.execute(query_write, (self.__sha256,
                                        pickle.dumps(self.__yara_rule)))
        self.__db.commit() 
Example #28
Source File: object_storage.py    From jawfish with MIT License 5 votes vote down vote up
def pop(self, key, default=__UnProvided()):
        if type(default) is __UnProvided or pickle.dumps(key) in self.storage:
            return pickle.loads(self.storage.pop(pickle.dumps(key)))
        return default 
Example #29
Source File: object_storage.py    From jawfish with MIT License 5 votes vote down vote up
def get(self, key, default=None):
        if pickle.dumps(key) in self.storage:
            return self.storage[pickle.dumps(key)]
        return default 
Example #30
Source File: funcdb.py    From BASS with GNU General Public License v2.0 5 votes vote down vote up
def add(self, db, **kwargs):
        db_data = db.data.copy()
        db_data["functions"] = _filter_functions(db.data["functions"], **kwargs)
        data = pickle.dumps(db_data)
        result = requests.post("{:s}/function".format(self.url), files = {"file": BytesIO(data)})
        if result.status_code != 200:
            raise RuntimeError("Request failed with status code {:d}".format(result.status_code))