예제 #1
0
    def func_deserialize(self, msg):
        r"""Default method for deseserializing a message.

        Args:
            msg (str, bytes): Message to be deserialized.

        Returns:
            tuple(obj, dict): Deserialized message and header information.

        """
        if self._func_deserialize is not None:
            out = self._func_deserialize(msg)
        elif self.format_str is not None:
            if self.as_array:
                if len(msg) == 0:
                    out = self.empty_msg
                    # out = np.empty(0, self.numpy_dtype)
                else:
                    out = serialize.bytes_to_array(msg,
                                                   self.numpy_dtype,
                                                   order='F')
            else:
                if len(msg) == 0:
                    out = self.empty_msg
                else:
                    out = serialize.process_message(msg, self.format_str)
        else:
            out = msg
        return out
예제 #2
0
    def process_line(self, line):
        r"""Extract values from the columns in the line using the table format.

        Args:
            line (str): String to extract arguments from.

        Returns:
            tuple: The arguments extracted from line.

        """
        return serialize.process_message(line, self.format_str)
예제 #3
0
def test_format_message():
    r"""Test formatting message from a list or arguments and back."""
    fmt = backwards.unicode2bytes("%5s\t%ld\t%lf\t%g%+gj\n")
    dtype = serialize.cformat2nptype(fmt)
    x_arr = np.ones(1, dtype)
    x_tup = [x_arr[n][0] for n in x_arr.dtype.names]
    # x_tup[0] = backwards.bytes2unicode(x_tup[0])
    flist = [fmt, "%ld"]
    alist = [tuple(x_tup), 0]
    for a, f in zip(alist, flist):
        msg = serialize.format_message(a, f)
        b = serialize.process_message(msg, f)
        if not isinstance(a, tuple):
            nt.assert_equal(b, (a, ))
        else:
            nt.assert_equal(b, a)
    # Errors
    nt.assert_raises(RuntimeError, serialize.format_message, (0, ), "%d %d")
    nt.assert_raises(TypeError, serialize.process_message, 0, "%d")
    nt.assert_raises(ValueError, serialize.process_message,
                     backwards.unicode2bytes("hello"), "%d")