Пример #1
0
def test_guess_serializer():
    r"""Test guess_serializer."""
    nele = 5
    names = ["name", "number", "value", "complex"]
    field_names = [backwards.unicode2bytes(n) for n in names]
    dtypes = ['S5', 'i8', 'f8', 'c16']
    dtype = np.dtype([(n, f) for n, f in zip(names, dtypes)])
    arr_mix = np.zeros(nele, dtype)
    arr_mix['name'][0] = 'hello'
    fmt_arr = serialize._default_delimiter.join(
        serialize.nptype2cformat(arr_mix.dtype, asbytes=True))
    fmt_arr += serialize._default_newline
    if platform._is_win:  # pragma: windows
        x = arr_mix[0].tolist()
        for i in x:
            print(type(i))
        if backwards.PY2:  # pragma: Python 2
            # tolist maps to long on python 2, but int on python 3?!
            fmt = backwards.unicode2bytes('%s\t%l64d\t%g\t%g%+gj\n')
        else:  # pragma: Python 3
            fmt = backwards.unicode2bytes('%s\t%d\t%g\t%g%+gj\n')
    else:
        fmt = backwards.unicode2bytes('%s\t%ld\t%g\t%g%+gj\n')
    test_list = [(arr_mix, dict(field_names=field_names, format_str=fmt_arr,
                                stype=2, as_array=1)),
                 (arr_mix[0].tolist(), dict(format_str=fmt, stype=1)),
                 ('hello', dict(stype=0))]
    for obj, sinfo_ans in test_list:
        sinfo_res = serialize.guess_serializer(obj)
        s = serialize.get_serializer(**sinfo_res)
        nt.assert_equal(s.serializer_info, sinfo_ans)
Пример #2
0
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):
        nt.assert_equal(serialize.nptype2cformat(a), b)
        nt.assert_equal(serialize.nptype2cformat(a, asbytes=True),
                        [backwards.unicode2bytes(ib) for ib in b])
Пример #3
0
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:
            nt.assert_equal(serialize.nptype2cformat(ia), b)
    nt.assert_raises(TypeError, serialize.nptype2cformat, 0)
    for a in unsupported_nptype:
        nt.assert_raises(ValueError, serialize.nptype2cformat, a)
Пример #4
0
 def format_str(self):
     r"""str: Format string describing the table column types."""
     if not hasattr(self, '_format_str'):
         if hasattr(self, '_dtype'):
             fmts = serialize.nptype2cformat(self.dtype, asbytes=True)
             self._format_str = self.column.join(fmts) + self.newline
         else:  # pragma: debug
             raise RuntimeError("Format string not set " +
                                "and cannot be determined.")
     return self._format_str