Esempio n. 1
0
    def send_dict(self, args_dict, field_order=None, **kwargs):
        r"""Send a message with fields specified in the input dictionary.

        Args:
            args_dict (dict): Dictionary with fields specifying output fields.
            field_order (list, optional): List of fields in the order they
                should be passed to send. If not provided, the fields from
                the serializer are used. If the serializer dosn't have
                field names an error will be raised.
            **kwargs: Additiona keyword arguments are passed to send.

        Returns:
            bool: Success/failure of send.

        Raises:
            RuntimeError: If the field order can not be determined.

        """
        if field_order is None:
            if self.serializer.field_names is not None:
                field_order = [
                    backwards.bytes2unicode(n) for n in self.serializer.field_names]
            elif len(args_dict) <= 1:
                field_order = [k for k in args_dict.keys()]
            else:  # pragma: debug
                raise RuntimeError("Could not determine the field order.")
        args = (serialize.dict2pandas(args_dict, order=field_order), )
        return self.send(*args, **kwargs)
 def test_send_recv_dict(self):
     r"""Test send/recv Pandas data frame as dict."""
     msg_send = serialize.pandas2dict(self.msg_short)
     names = [backwards.bytes2unicode(n) for n in self.field_names]
     flag = self.send_instance.send_dict(msg_send)
     assert (flag)
     flag, msg_recv = self.recv_instance.recv_dict()
     assert (flag)
     msg_recv = serialize.dict2pandas(msg_recv, order=names)
     self.assert_msg_equal(msg_recv, self.msg_short)
Esempio n. 3
0
def test_pandas2dict():
    r"""Test conversion of a Pandas data frame to a dictionary and back."""
    nele = 5
    names = ["complex", "name", "number", "value"]
    dtypes = ['c16', 'S5', 'i8', 'f8']
    dtype = np.dtype([(n, f) for n, f in zip(names, dtypes)])
    arr_mix = np.zeros(nele, dtype)
    arr_mix['name'][0] = 'hello'
    arr_obj = np.array([list(), 'hello', 5], dtype='O')
    test_arrs = [arr_mix,
                 np.zeros(nele, 'float'),
                 arr_mix['name'],
                 arr_obj]
    for ans in test_arrs:
        frame = serialize.numpy2pandas(ans)
        # Sorted
        d = serialize.pandas2dict(frame)
        res = serialize.dict2pandas(d)
        np.testing.assert_array_equal(res, frame)
        # Provided
        d = serialize.pandas2dict(frame)
        res = serialize.dict2pandas(d, order=ans.dtype.names)
        np.testing.assert_array_equal(res, frame)