當前位置: 首頁>>代碼示例>>Python>>正文


Python cPickle.dumps方法代碼示例

本文整理匯總了Python中six.moves.cPickle.dumps方法的典型用法代碼示例。如果您正苦於以下問題:Python cPickle.dumps方法的具體用法?Python cPickle.dumps怎麽用?Python cPickle.dumps使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在six.moves.cPickle的用法示例。


在下文中一共展示了cPickle.dumps方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_pickle_chunk_V1_read

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_pickle_chunk_V1_read():
    data = {'foo': b'abcdefghijklmnopqrstuvwxyz'}
    version = {'_id': sentinel._id,
               'blob': '__chunked__'}
    coll = Mock()
    arctic_lib = Mock()
    datap = compressHC(cPickle.dumps(data, protocol=cPickle.HIGHEST_PROTOCOL))
    data_1 = datap[0:5]
    data_2 = datap[5:]
    coll.find.return_value = [{'data': Binary(data_1),
                               'symbol': 'sentinel.symbol',
                               'segment': 0},
                              {'data': Binary(data_2),
                               'symbol': 'sentinel.symbol',
                               'segment': 1},
                              ]
    arctic_lib.get_top_level_collection.return_value = coll

    ps = PickleStore()
    assert(data == ps.read(arctic_lib, version, sentinel.symbol)) 
開發者ID:man-group,項目名稱:arctic,代碼行數:22,代碼來源:test_pickle_store.py

示例2: test_pickle_store_future_version

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_pickle_store_future_version():
    data = {'foo': b'abcdefghijklmnopqrstuvwxyz'}
    version = {'_id': sentinel._id,
               'blob': '__chunked__VERSION_ONE_MILLION'}
    coll = Mock()
    arctic_lib = Mock()
    datap = compressHC(cPickle.dumps(data, protocol=cPickle.HIGHEST_PROTOCOL))
    data_1 = datap[0:5]
    data_2 = datap[5:]
    coll.find.return_value = [{'data': Binary(data_1),
                               'symbol': 'sentinel.symbol',
                               'segment': 0},
                              {'data': Binary(data_2),
                               'symbol': 'sentinel.symbol',
                               'segment': 1},
                              ]
    arctic_lib.get_top_level_collection.return_value = coll

    ps = PickleStore()
    with pytest.raises(UnsupportedPickleStoreVersion) as e:
        ps.read(arctic_lib, version, sentinel.symbol)
    assert('unsupported version of pickle store' in str(e.value)) 
開發者ID:man-group,項目名稱:arctic,代碼行數:24,代碼來源:test_pickle_store.py

示例3: verify_tee

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def verify_tee(n, original, seed):
    try:
        state = random.getstate()
        iterators = list(tee(original, n=n))
        results = [[] for _ in range(n)]
        exhausted = [False] * n
        while not all(exhausted):
            # Upper argument of random.randint is inclusive. Argh.
            i = random.randint(0, n - 1)
            if not exhausted[i]:
                if len(results[i]) == len(original):
                    assert_raises(StopIteration, next, iterators[i])
                    assert results[i] == original
                    exhausted[i] = True
                else:
                    if random.randint(0, 1):
                        iterators[i] = cPickle.loads(
                            cPickle.dumps(iterators[i]))
                    elem = next(iterators[i])
                    results[i].append(elem)
    finally:
        random.setstate(state) 
開發者ID:mila-iqia,項目名稱:picklable-itertools,代碼行數:24,代碼來源:__init__.py

示例4: test_xrange

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_xrange():
    yield assert_equal, list(xrange(10)), list(_xrange(10))
    yield assert_equal, list(xrange(10, 15)), list(_xrange(10, 15))
    yield assert_equal, list(xrange(10, 20, 2)), list(_xrange(10, 20, 2))
    yield assert_equal, list(xrange(5, 1, -1)), list(_xrange(5, 1, -1))
    yield (assert_equal, list(xrange(5, 55, 3)),
           list(cPickle.loads(cPickle.dumps(_xrange(5, 55, 3)))))
    yield assert_equal, _xrange(5).index(4), 4
    yield assert_equal, _xrange(5, 9).index(6), 1
    yield assert_equal, _xrange(8, 24, 3).index(11), 1
    yield assert_equal, _xrange(25, 4, -5).index(25), 0
    yield assert_equal, _xrange(28, 7, -7).index(14), 2
    yield assert_raises, ValueError, _xrange(2, 9, 2).index, 3
    yield assert_raises, ValueError, _xrange(2, 20, 2).index, 9
    yield assert_equal, _xrange(5).count(5), 0
    yield assert_equal, _xrange(5).count(4), 1
    yield assert_equal, _xrange(4, 9).count(4), 1
    yield assert_equal, _xrange(3, 9, 2).count(4), 0
    yield assert_equal, _xrange(3, 9, 2).count(5), 1
    yield assert_equal, _xrange(3, 9, 2).count(20), 0
    yield assert_equal, _xrange(9, 3).count(5), 0
    yield assert_equal, _xrange(3, 10, -1).count(5), 0
    yield assert_equal, _xrange(10, 3, -1).count(5), 1
    yield assert_equal, _xrange(10, 0, -2).count(6), 1
    yield assert_equal, _xrange(10, -1, -3).count(7), 1 
開發者ID:mila-iqia,項目名稱:picklable-itertools,代碼行數:27,代碼來源:__init__.py

示例5: test_pickling

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_pickling(self, backend_config):
        x_data, = self.generate_inputs()

        link = self.create_link(self.generate_params())
        link.to_device(backend_config.device)

        x = chainer.Variable(x_data)
        x.to_device(backend_config.device)

        y = link(x)
        y_data1 = y.data
        del x, y
        pickled = pickle.dumps(link, -1)
        del link
        link = pickle.loads(pickled)
        x = chainer.Variable(x_data)
        x.to_device(backend_config.device)
        y = link(x)
        y_data2 = y.data

        testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0) 
開發者ID:chainer,項目名稱:chainer,代碼行數:23,代碼來源:test_convolution_2d.py

示例6: check_pickling

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def check_pickling(self, x_data):
        x = chainer.Variable(x_data)
        y = self.link(x)
        y_data1 = y.data

        del x, y

        pickled = pickle.dumps(self.link, -1)
        del self.link
        self.link = pickle.loads(pickled)

        x = chainer.Variable(x_data)
        y = self.link(x)
        y_data2 = y.data

        testing.assert_allclose(y_data1, y_data2, atol=0, rtol=0) 
開發者ID:chainer,項目名稱:chainer,代碼行數:18,代碼來源:test_dilated_convolution_2d.py

示例7: test_pickling

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_pickling(self):
        try:
            features = numpy.arange(360, dtype='uint16').reshape((10, 36))
            h5file = h5py.File('file.hdf5', mode='w')
            h5file['features'] = features
            split_dict = {'train': {'features': (0, 10, None, '.')}}
            h5file.attrs['split'] = H5PYDataset.create_split_array(split_dict)
            dataset = cPickle.loads(
                cPickle.dumps(H5PYDataset(h5file, which_sets=('train',))))
            # Make sure _out_of_memory_{open,close} accesses
            # external_file_handle rather than _external_file_handle
            dataset._out_of_memory_open()
            dataset._out_of_memory_close()
            assert dataset.data_sources is None
        finally:
            os.remove('file.hdf5') 
開發者ID:rizar,項目名稱:attention-lvcsr,代碼行數:18,代碼來源:test_hdf5.py

示例8: test_causes_pickle

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_causes_pickle(self):
        f = None
        try:
            self._raise_many(["Still still not working",
                              "Still not working", "Not working"])
        except RuntimeError:
            f = failure.Failure()

        self.assertIsNotNone(f)
        p_f = pickle.dumps(f)
        f = pickle.loads(p_f)

        self.assertEqual(2, len(f.causes))
        self.assertEqual("Still not working", f.causes[0].exception_str)
        self.assertEqual("Not working", f.causes[1].exception_str)

        f = f.causes[0]
        self.assertEqual(1, len(f.causes))
        self.assertEqual("Not working", f.causes[0].exception_str)

        f = f.causes[0]
        self.assertEqual(0, len(f.causes)) 
開發者ID:openstack,項目名稱:taskflow,代碼行數:24,代碼來源:test_failure.py

示例9: test_pickle_protein

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_pickle_protein():
    """Pickle proteins"""
    # Proteins
    rec = next(oddt.toolkit.readfile('pdb', xiap_receptor))
    # generate atom_dict
    assert rec.atom_dict is not None

    assert rec._atom_dict is not None
    pickled_rec = loads(dumps(rec))
    assert pickled_rec.protein is False
    assert pickled_rec._atom_dict is not None

    rec.protein = True
    # setting protein property should clean atom_dict cache
    assert rec._atom_dict is None
    # generate atom_dict
    assert rec.atom_dict is not None

    pickled_rec = loads(dumps(rec))
    assert pickled_rec.protein is True
    assert pickled_rec._atom_dict is not None 
開發者ID:oddt,項目名稱:oddt,代碼行數:23,代碼來源:test_toolkit.py

示例10: pickle_invoke

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def pickle_invoke(func, target=None, protocol=None, *args):
    """pickle_invoke(func, *args, target=None, protocol=None)

    Create a byte sequence which when unpickled calls a callable with given
    arguments.

    Note:
        The function has to be importable using the same name on the system
        that unpickles this invocation.

    Arguments:
        func(callable): The function to call or class to instantiate.
        args(tuple): The arguments to call the callable with.
        target: The internals description of the targeted python
            version. If this is ``None`` the specification of the currently
            running python version will be used.
        protocol: The pickle protocol version to use (use None for default).

    Returns:
        bytes: The data that when unpickled calls ``func(*args)``.

    Example:
        >>> from pwny import *
        >>> import pickle
        >>> def hello(arg):
        ...     print('Hello, %s!' % arg)
        ...
        >>> pickle.loads(pickle_invoke(hello, 'world'))
        Hello, world!
    """

    protocol = get_protocol_version(protocol, target)
    return cPickle.dumps(PickleInvoke(func, *args), protocol) 
開發者ID:edibledinos,項目名稱:pwnypack,代碼行數:35,代碼來源:pickle.py

示例11: store

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def store(self, thing):
        """
        Simple persistence method
        """
        to_store = {'field1': thing.field1,
                    'date_field': thing.date_field,
                    }
        to_store['stuff'] = Binary(cPickle.dumps(thing.stuff))
        # Respect any soft-quota on write - raises if stats().totals.size > quota 
        self._arctic_lib.check_quota()
        self._collection.insert_one(to_store) 
開發者ID:man-group,項目名稱:arctic,代碼行數:13,代碼來源:how_to_custom_arctic_library.py

示例12: write

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def write(self, arctic_lib, version, symbol, item, _previous_version):
        # Currently we try to bson encode if the data is less than a given size and store it in
        # the version collection, but pickling might be preferable if we have characters that don't
        # play well with the bson encoder or if you always want your data in the data collection.
        if not SKIP_BSON_ENCODE_PICKLE_STORE:
            try:
                # If it's encodeable, then ship it
                b = bson.BSON.encode({'data': item})
                if len(b) < min(MAX_BSON_ENCODE, _HARD_MAX_BSON_ENCODE):
                    version['data'] = item
                    return
            except InvalidDocument:
                pass

        # Pickle, chunk and store the data
        collection = arctic_lib.get_top_level_collection()
        # Try to pickle it. This is best effort
        version['blob'] = _MAGIC_CHUNKEDV2
        pickled = cPickle.dumps(item, protocol=cPickle.HIGHEST_PROTOCOL)

        data = compress_array([pickled[i * _CHUNK_SIZE: (i + 1) * _CHUNK_SIZE] for i in xrange(int(len(pickled) / _CHUNK_SIZE + 1))])

        for seg, d in enumerate(data):
            segment = {'data': Binary(d)}
            segment['segment'] = seg
            seg += 1
            sha = checksum(symbol, segment)
            collection.update_one({'symbol': symbol, 'sha': sha},
                                  {'$set': segment, '$addToSet': {'parent': version['_id']}},
                                  upsert=True) 
開發者ID:man-group,項目名稱:arctic,代碼行數:32,代碼來源:_pickle_store.py

示例13: test_write_object

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_write_object():
    arctic_lib = Mock()
    self = create_autospec(PickleStore)
    version = {'_id': ObjectId()}
    PickleStore.write(self, arctic_lib, version, 'sentinel.symbol', sentinel.item, sentinel.previous_version)
    assert 'data' not in version

    assert version['blob'] == '__chunked__V2'
    coll = arctic_lib.get_top_level_collection.return_value
    assert coll.update_one.call_args_list == [call({'sha': checksum('sentinel.symbol', {'segment': 0, 'data': Binary(compress(cPickle.dumps(sentinel.item, cPickle.HIGHEST_PROTOCOL)))}),
                                                    'symbol': 'sentinel.symbol'},
                                                   {'$set': {'segment': 0, 'data': Binary(compress(cPickle.dumps(sentinel.item, cPickle.HIGHEST_PROTOCOL)), 0)},
                                                    '$addToSet': {'parent': version['_id']}}, upsert=True)] 
開發者ID:man-group,項目名稱:arctic,代碼行數:15,代碼來源:test_pickle_store.py

示例14: test_read_object_backwards_compat

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_read_object_backwards_compat():
    self = create_autospec(PickleStore)
    version = {'blob': Binary(compressHC(cPickle.dumps(object)))}
    assert PickleStore.read(self, sentinel.arctic_lib, version, sentinel.symbol) == object 
開發者ID:man-group,項目名稱:arctic,代碼行數:6,代碼來源:test_pickle_store.py

示例15: test_read_object_2

# 需要導入模塊: from six.moves import cPickle [as 別名]
# 或者: from six.moves.cPickle import dumps [as 別名]
def test_read_object_2():
    self = create_autospec(PickleStore)
    version = {'_id': sentinel._id,
               'blob': '__chunked__'}
    coll = Mock()
    arctic_lib = Mock()
    coll.find.return_value = [{'data': Binary(compressHC(cPickle.dumps(object))),
                               'symbol': 'sentinel.symbol',
                               'segment': 1}
                              ]
    arctic_lib.get_top_level_collection.return_value = coll

    assert PickleStore.read(self, arctic_lib, version, sentinel.symbol) == object
    assert coll.find.call_args_list == [call({'symbol': sentinel.symbol, 'parent': sentinel._id})] 
開發者ID:man-group,項目名稱:arctic,代碼行數:16,代碼來源:test_pickle_store.py


注:本文中的six.moves.cPickle.dumps方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。