Пример #1
0
def test_format_message():
    r"""Test formatting message from a list or arguments and back."""
    fmt = b'%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]
    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):
            assert (b == (a, ))
        else:
            assert (b == a)
    # Formats with mixed types
    assert (serialize.format_message(b'hello', '%s') == 'hello')
    assert (serialize.format_message('hello', b'%s') == b'hello')
    # Errors
    with pytest.raises(RuntimeError):
        serialize.format_message((0, ), "%d %d")
    with pytest.raises(TypeError):
        serialize.process_message(0, "%d")
    with pytest.raises(ValueError):
        serialize.process_message(b'hello', "%d")
Пример #2
0
    def func_serialize(self, args):
        r"""Serialize a message.

        Args:
            args: List of arguments to be formatted or numpy array to be
                serialized.

        Returns:
            bytes, str: Serialized message.

        """
        if (((self.extra_kwargs.get('format_str', None) is not None)
             and isinstance(args, list))):
            args = format_message(args, self.extra_kwargs['format_str'])
        return backwards.as_bytes(args)
Пример #3
0
def test_format_message():
    r"""Test formatting message from a list or arguments and back."""
    fmt = b'%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.as_str(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):
            assert_equal(b, (a, ))
        else:
            assert_equal(b, a)
    # Errors
    assert_raises(RuntimeError, serialize.format_message, (0, ), "%d %d")
    assert_raises(TypeError, serialize.process_message, 0, "%d")
    assert_raises(ValueError, serialize.process_message, b'hello', "%d")
Пример #4
0
    def func_serialize(self, args):
        r"""Serialize a message.

        Args:
            args: List of arguments to be formatted or numpy array to be
                serialized.

        Returns:
            bytes, str: Serialized message.

        """
        if self.format_str is None:
            raise RuntimeError("Format string is not defined.")
        args = self.datatype.coerce_type(args,
                                         key_order=self.get_field_names())
        if self.as_array:
            out = serialize.array_to_table(args, self.format_str,
                                           use_astropy=self.use_astropy)
        else:
            out = serialize.format_message(args, self.format_str)
        return backwards.as_bytes(out)