Python msgpack_numpy.encode() Examples

The following are 11 code examples of msgpack_numpy.encode(). 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 msgpack_numpy , or try the search function .
Example #1
Source File: banyan_base_multi.py    From python_banyan with GNU Affero General Public License v3.0 6 votes vote down vote up
def set_subscriber_topic(self, topic, subscriber_socket):
        """
        This method sets a subscriber topic.

        You can subscribe to multiple topics by calling this method for
        each topic.

        :param topic: A topic string

        :param subscriber_socket: subscriber socket

        :return:
        """

        # make sure topic is a string
        if not type(topic) is str:
            raise TypeError('Subscriber topic must be python_banyan string')

        # does the subscriber socket exist?
        if subscriber_socket:
            subscriber_socket.setsockopt(zmq.SUBSCRIBE, topic.encode())
        else:
            raise ValueError('set_subscriber_topic: socket is None') 
Example #2
Source File: banyan_base_multi.py    From python_banyan with GNU Affero General Public License v3.0 6 votes vote down vote up
def unsubscribe_topic(self, topic, subscriber_socket):
        """
        This method un-subscribes from a topic.

        :param topic: A topic string

        :param subscriber_socket: subscriber socket

        :return:
        """

        # make sure topic is a string
        if not type(topic) is str:
            raise TypeError('Subscriber topic must be python_banyan string')

        # make sure that a socket reference has been passed in
        if subscriber_socket:
            subscriber_socket.unsubscribe(topic.encode())
        else:
            raise ValueError('set_subscriber_topic: socket is None') 
Example #3
Source File: banyan_base_aio.py    From python_banyan with GNU Affero General Public License v3.0 6 votes vote down vote up
def publish_payload(self, payload, topic=''):
        """
        This method will publish a python_banyan payload and its associated topic

        :param payload: Protocol message to be published

        :param topic: A string value
        """

        # make sure the topic is a string
        if not type(topic) is str:
            raise TypeError('Publish topic must be python_banyan string', 'topic')

        if self.numpy:
            message = await self.numpy_pack(payload)
        else:
            message = await self.pack(payload)

        pub_envelope = topic.encode()
        await self.publisher.send_multipart([pub_envelope, message])
        # await asyncio.sleep(1) 
Example #4
Source File: banyan_base.py    From python_banyan with GNU Affero General Public License v3.0 6 votes vote down vote up
def publish_payload(self, payload, topic=''):
        """
        This method will publish a python_banyan payload and its associated topic

        :param payload: Protocol message to be published

        :param topic: A string value
        """

        # make sure the topic is a string
        if not type(topic) is str:
            raise TypeError('Publish topic must be python_banyan string', 'topic')

        # create python_banyan message pack payload
        if self.numpy:
            message = msgpack.packb(payload, default=m.encode)
        else:
            message = msgpack.packb(payload, use_bin_type=True)

        pub_envelope = topic.encode()
        self.publisher.send_multipart([pub_envelope, message]) 
Example #5
Source File: client.py    From osim-rl with MIT License 5 votes vote down vote up
def _generate_response_channel(self):
        random_hash = hashlib.md5("{}".format(random.randint(0, 10**10)).encode('utf-8')).hexdigest()
        response_channel = "{}::{}::response::{}".format(   self.namespace,
                                                            self.service_id,
                                                            random_hash)
        return response_channel 
Example #6
Source File: client.py    From osim-rl with MIT License 5 votes vote down vote up
def _blocking_request(self, _request):
        """
            request:
                -command_type
                -payload
                -response_channel
            response: (on response_channel)
                - RESULT
            * Send the payload on command_channel (self.namespace+"::command")
                ** redis-left-push (LPUSH)
            * Keep listening on response_channel (BLPOP)
        """
        assert type(_request) ==type({})
        _request['response_channel'] = self._generate_response_channel()

        _redis = self.get_redis_connection()
        """
            The client always pushes in the left
            and the service always pushes in the right
        """
        if self.verbose: print("Request : ", _response)
        # Push request in command_channels
        payload = msgpack.packb(_request, default=m.encode, use_bin_type=True)
        _redis.lpush(self.command_channel, payload)
        ## TODO: Check if we can use `repr` for json.dumps string serialization
        # Wait with a blocking pop for the response
        _response = _redis.blpop(_request['response_channel'])[1]
        if self.verbose: print("Response : ", _response)
        _response = msgpack.unpackb(_response, object_hook=m.decode, encoding="utf8")
        if _response['type'] == messages.OSIM_RL.ERROR:
            raise Exception(str(_response))
        else:
            return _response 
Example #7
Source File: banyan_base_multi.py    From python_banyan with GNU Affero General Public License v3.0 5 votes vote down vote up
def publish_payload(self, payload, publisher_socket, topic=''):
        """
        This method will publish a python_banyan payload and its associated topic

        :param payload:  Protocol message to be published

        :param publisher_socket: Publisher socket - handle to socket or "BROADCAST" to send to
                                 all connected publisher sockets

        :param topic: A string value for message topic

        :return:
        """

        # make sure topic is a string
        if not type(topic) is str:
            raise TypeError('Publish topic must be python_banyan string', 'topic')

        # create python_banyan message pack payload
        if self.numpy:
            message = msgpack.packb(payload, default=m.encode)
        else:
            message = umsgpack.packb(payload)

        pub_envelope = topic.encode()
        if publisher_socket == "BROADCAST":
            for element in self.backplane_table:
                if element['publisher']:
                    element['publisher'].send_multipart([pub_envelope, message])
        else:

            if publisher_socket:
                publisher_socket.send_multipart([pub_envelope, message])
            else:
                raise ValueError('Invalid publisher socket') 
Example #8
Source File: banyan_base_aio.py    From python_banyan with GNU Affero General Public License v3.0 5 votes vote down vote up
def numpy_pack(self, data):
        return msgpack.packb(data, default=m.encode) 
Example #9
Source File: banyan_base.py    From python_banyan with GNU Affero General Public License v3.0 5 votes vote down vote up
def set_subscriber_topic(self, topic):
        """
        This method sets a subscriber topic.

        You can subscribe to multiple topics by calling this method for
        each topic.

        :param topic: A topic string
        """

        if not type(topic) is str:
            raise TypeError('Subscriber topic must be python_banyan string')

        self.subscriber.setsockopt(zmq.SUBSCRIBE, topic.encode()) 
Example #10
Source File: util.py    From multisensory with Apache License 2.0 5 votes vote down vote up
def np_serialize(x):
  import msgpack
  import msgpack_numpy as m
  return msgpack.packb(np.array(x, 'float32'), default=m.encode)
  #return msgpack.packb(x, default=m.encode)
  # f = StringIO.StringIO()
  # np.save(f, x)
  # return f.getvalue() 
Example #11
Source File: serializers.py    From lmdb-embeddings with GNU General Public License v3.0 5 votes vote down vote up
def serialize(vector):
        """ Serializer a vector using msgpack.

        :param np.array vector:
        :return bytes:
        """
        return msgpack.packb(vector, default = msgpack_numpy.encode)