def test_nptype2cformat(): r"""Test conversion from numpy dtype to C format string.""" for a, b in map_nptype2cformat: if isinstance(a, str): a = [a] for ia in a: assert (serialize.nptype2cformat(ia) == b) with pytest.raises(TypeError): serialize.nptype2cformat(0) for a in unsupported_nptype: with pytest.raises(ValueError): serialize.nptype2cformat(a)
def test_nptype2cformat_structured(): r"""Test conversion from structured numpy dtype to C format string.""" if platform._is_win: # pragma: windows fmts = ["%5s", "%l64d", "%g", "%g%+gj"] else: fmts = ["%5s", "%ld", "%g", "%g%+gj"] names0 = ['f0', 'f1', 'f2', 'f3'] names1 = ["name", "number", "value", "complex"] dtypes = ['S5', 'i8', 'f8', 'c16'] dtype0 = np.dtype([(n, f) for n, f in zip(names0, dtypes)]) dtype1 = np.dtype([(n, f) for n, f in zip(names1, dtypes)]) alist = [dtype0, dtype1] blist = [fmts, fmts] for a, b in zip(alist, blist): assert_equal(serialize.nptype2cformat(a), b) assert_equal(serialize.nptype2cformat(a, asbytes=True), [backwards.as_bytes(ib) for ib in b])
def test_nptype2cformat(): r"""Test conversion from numpy dtype to C format string.""" for a, b in map_nptype2cformat: if isinstance(a, str): a = [a] for ia in a: assert_equal(serialize.nptype2cformat(ia), b) assert_raises(TypeError, serialize.nptype2cformat, 0) for a in unsupported_nptype: assert_raises(ValueError, serialize.nptype2cformat, a)
def update_format_str(self): r"""Update the format string based on the type definition.""" # Get format information from precision etc. if (self.format_str is None) and self.initialized: assert(self.typedef['type'] == 'array') fmts = [] if isinstance(self.typedef['items'], dict): # pragma: debug idtype = definition2dtype(self.typedef['items']) ifmt = serialize.nptype2cformat(idtype, asbytes=True) # fmts = [ifmt for x in msg] raise Exception("Variable number of items not yet supported.") elif isinstance(self.typedef['items'], list): for x in self.typedef['items']: idtype = definition2dtype(x) ifmt = serialize.nptype2cformat(idtype, asbytes=True) fmts.append(ifmt) if fmts: self.format_str = serialize.table2format( fmts=fmts, delimiter=self.delimiter, newline=self.newline, comment=b'')