How to use the cbor2.dumps function in cbor2

To help you get started, we’ve selected a few cbor2 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github piwheels / piwheels / tests / master / test_seraph.py View on Github external
def test_router_no_workers(zmq_context, master_config):
    seraph = Seraph(master_config)
    seraph.start()
    try:
        client = zmq_context.socket(transport.REQ)
        client.connect(master_config.db_queue)
        client.send(cbor2.dumps(['FOO']))
        with pytest.raises(transport.Error):
            client.recv(flags=transport.NOBLOCK)
        worker = zmq_context.socket(transport.REQ)
        worker.connect(const.ORACLE_QUEUE)
        worker.send(b'READY')
        client_addr, empty, msg = worker.recv_multipart()
        assert cbor2.loads(msg) == ['FOO']
        worker.send_multipart([client_addr, empty, cbor2.dumps(['BAR'])])
        assert cbor2.loads(client.recv()) == ['BAR']
    finally:
        seraph.quit()
        seraph.join()
github oberstet / scratchbox / python / lmdb / test3_3.py View on Github external
user = User()
    user.oid = 23
    user.name = 'Homer Simpson'
    user.authid = 'homer'
    user.email = 'homer.simpson@example.com'
    user.birthday = {
        'year': 1950,
        'month': 12,
        'day': 24
    }
    user.is_friendly = True
    user.tags = [Tag.GEEK, Tag.VIP]

    #data = json.dumps(user.marshal(), ensure_ascii=False)
    data = cbor2.dumps(user.marshal())

    RESULT['objects'] += 1
    RESULT['bytes'] += len(data)
github crossbario / zlmdb / zlmdb / _database.py View on Github external
def _set_slot(self, slot_index, slot):
        """

        :param slot_index:
        :param meta:
        :return:
        """
        assert type(slot_index) == int
        assert slot_index > 0 and slot_index < 65536
        assert slot is None or isinstance(slot, Slot)
        if slot:
            assert slot_index == slot.slot

        key = b'\0\0' + struct.pack('>H', slot_index)
        if slot:
            data = cbor2.dumps(slot.marshal())
            with self.begin(write=True) as txn:
                txn._txn.put(key, data)
                self._slots[slot.oid] = slot
                self._slots_by_index[slot.oid] = slot_index

            self.log.debug('Wrote metadata for table <{oid}> to slot {slot_index:03d}',
                           oid=slot.oid,
                           slot_index=slot_index)
        else:
            with self.begin(write=True) as txn:
                result = txn.get(key)
                if result:
                    txn._txn.delete(key)
                if slot.oid in self._slots:
                    del self._slots[slot.oid]
                if slot.oid in self._slots_by_index:
github Chia-Network / chia-blockchain / src / protocol / protocol.py View on Github external
async def send(self, function, data):
        encoded = dumps({"function": function, "data": transform_to_streamable(data)})
        await self.transport_.write(len(encoded).to_bytes(LENGTH_BYTES, "big") + encoded)
github satori-com / satori-rtm-sdk-python / satori / rtm / connection.py View on Github external
def action_with_preserialized_body(self, name, body, callback=None):
        if callback:
            if len(self.ack_callbacks_by_id) >= high_ack_count_watermark:
                self.logger.debug('Throttling %s request', name)
                time.sleep(0.001)

            action_id = next(self.action_id_iterator)
            if self.protocol == 'cbor':
                payload =\
                    b''.join([
                        b'\xa3',
                        cbor2.dumps(u'action'),
                        cbor2.dumps(name),
                        cbor2.dumps(u'id'),
                        cbor2.dumps(action_id),
                        cbor2.dumps(u'body'),
                        body])
            else:
                payload =\
                    u''.join([
                        u'{"action":"',
                        name,
                        u'","id":',
                        str(action_id),
                        u',"body":',
                        body,
                        u'}']).encode('utf8')
            self.ack_callbacks_by_id[action_id] = callback
        else:
            if self.protocol == 'cbor':
                payload =\
                    b''.join([
github satori-com / satori-rtm-sdk-python / satori / rtm / connection.py View on Github external
def publish_preserialized_message(self, channel, message, callback=None):
        if self.protocol == 'json':
            body = u'{{"channel":"{0}","message": {1}}}'.format(
                channel, message)
        elif self.protocol == 'cbor':
            body =\
                b''.join([
                    b'\xa2',
                    cbor2.dumps(u'channel'),
                    cbor2.dumps(channel),
                    cbor2.dumps(u'message'),
                    message])
        self.action_with_preserialized_body(u'rtm/publish', body, callback)
github satori-com / satori-rtm-sdk-python / satori / rtm / connection.py View on Github external
self.delegate = delegate
        self.ack_callbacks_by_id = {}
        self.action_id_iterator = itertools.count()
        self.https_proxy = https_proxy
        self._auth_lock = threading.RLock()
        self._next_auth_action = None
        self.ws = None
        self._last_ping_time = None
        self._last_ponged_time = None
        self._time_to_stop_pinging = False
        self._auth_callback = None
        self._ping_thread = None
        self._ws_thread = None
        self.protocol = protocol
        if self.protocol == 'cbor':
            self._dumps = cbor2.dumps
        else:
            self._dumps = json.dumps
github rokups / Launchpad / src / common / transport / ws.py View on Github external
async def _native_send(self):
        while True:
            if not self._outgoing.empty():
                msg = self._outgoing.get()
                logging.debug('<< ' + str(msg))

                if isinstance(msg, bytes):
                    await self._socket.send_bytes(msg)
                elif isinstance(msg, dict):
                    await self._socket.send_bytes(cbor2.dumps(msg, default=encoder.cbor2_encoder))
                elif isinstance(msg, str):
                    await self._socket.send_str(msg)
                else:
                    raise ValueError('Payload must be bytes|dict|str')
                self._outgoing.task_done()
            else:
                await asyncio.sleep(0.01)
github piwheels / piwheels / piwheels / transport.py View on Github external
schema = self._protocol.send[msg]
        except KeyError:
            raise IOError('unknown message: %s' % msg)
        if data is NoData:
            if schema is not NoData:
                raise IOError('data must be specified for %s' % msg)
            return cbor2.dumps(msg, default=default_encoder)
        else:
            if schema is NoData:
                raise IOError('no data expected for %s' % msg)
            try:
                data = schema(data)
            except Invalid as e:
                raise IOError('invalid data for %s: %s' % (msg, e))
            try:
                return cbor2.dumps((msg, data), default=default_encoder)
            except cbor2.CBOREncodeError as e:
                raise IOError('unable to serialize data')
github crossbario / crossbar / crossbar / node / controller.py View on Github external
def _start_native_worker(self, worker_type, worker_id, worker_options=None, details=None):

        # prohibit starting a worker twice
        #
        if worker_id in self._workers:
            emsg = "Could not start worker: a worker with ID '{}' is already running (or starting)".format(worker_id)
            self.log.error(emsg)
            raise ApplicationError('crossbar.error.worker_already_running', emsg)

        # check worker options
        #
        options = worker_options or {}
        if 'extra' in options:
            worker_options_extra = binascii.b2a_hex(cbor2.dumps(dict(options['extra'])))
        else:
            worker_options_extra = None

        try:
            if worker_type in self._node._native_workers:
                if self._node._native_workers[worker_type]['checkconfig_options']:
                    self._node._native_workers[worker_type]['checkconfig_options'](self.personality, options)
                else:
                    raise Exception('No checkconfig_options for worker type "{worker_type}" implemented!'.format(worker_type=worker_type))
            else:
                raise Exception('invalid worker type "{}"'.format(worker_type))
        except Exception as e:
            emsg = "Could not start native worker: invalid configuration ({})".format(e)
            self.log.error(emsg)
            raise ApplicationError('crossbar.error.invalid_configuration', emsg)