示例#1
0
 def test_basic_interface(self):
     cols = ['a', 'b', 'c', 'μεαν']
     dict1 = {_k: [_i] for _i, _k in enumerate(cols)}
     st1 = Struct(dict1)
     self.assertEqual(list(st1.keys()), cols)
     self.assertEqual(list(st1.keys()), cols)
     self.assertEqual(list(st1), cols)
     for _idx, (_k, _v) in enumerate(st1.items()):
         self.assertEqual(st1[_k], dict1[_k])
         self.assertEqual(cols[_idx], _k)
         self.assertEqual(st1[_idx], dict1[_k])
         self.assertEqual(getattr(st1, _k), dict1[_k])
     self.assertEqual(list(st1.keys()), cols)
     self.assertEqual(list(st1), cols)
     self.assertEqual(list(reversed(st1)), list(reversed(cols)))
     self.assertEqual(st1.get_nrows(), 0)
     self.assertEqual(st1.shape, (0, st1.get_ncols()))
     self.assertEqual(len(st1), st1.get_ncols())
     st1['a'] = -999
     self.assertEqual(st1.a, -999)
     st1['newcol'] = -999
     self.assertEqual(st1.newcol, -999)
示例#2
0
import os

import pytest

from .vnu_checker import VNUChecker
from riptable import Dataset, Struct, Multiset


@pytest.mark.xfail(reason="Requires the VNU.jar HTML validator")
@pytest.mark.parametrize(
    "rt_obj",
    [
        # Add complex examples that use composition and recursion of other riptable data structures.
        Dataset({"one": 1}),
        Multiset({"ds": Dataset({"one": 1})}),
        Struct({"ds": Dataset({"one": 1})}),
    ],
)
def test_simple_html_repr(rt_obj, tmp_path):
    # dump HTML representation to files
    if not os.path.isdir(tmp_path):
        tmp_path.mkdir()
    checker = VNUChecker(base_path=str(tmp_path),
                         errors_only=True,
                         ascii_quotes=True)
    fn = os.path.join(checker.base_path, rt_obj.__class__.__name__ + ".html")
    with open(fn, "w") as f:
        # Consider using builtin library lxml or third-party BeautifulSoup for better HTML dumps:
        # lxml:
        # doc = html.fromstring(rt_obj._repr_html_())
        # f.write(etree.tostring(doc, encoding="unicode", pretty_print=True))
        }),
        "ds_beta":
        Dataset({
            k: list(range(i * 10, (i + 1) * 10))
            for i, k in enumerate(
                ["eta", "theta", "iota", "kappa", "lambada", "mu"])
        }),
    }),
    Struct({
        "alpha":
        1,
        "beta": [2, 3],
        "gamma": ['2', '3'],
        "delta":
        arange(10),
        "epsilon":
        Struct({
            "theta": Struct({
                "kappa": 3,
                "zeta": 4,
            }),
            "iota": 2,
        }),
    }),
]


@contextmanager
def greedy_completion():
    ip = get_ipython()
    greedy_original = ip.Completer.greedy
    try:
示例#4
0
 def test_tree(self):
     '''sanity check that .tree() at least returns something, even for empty Struct'''
     s = Struct()
     self.assertIsInstance(s.tree(), DisplayString)
     s['foo'] = Dataset({'bar': arange(5)})
     self.assertIsInstance(s.tree(), DisplayString)
示例#5
0
    def _test_output(self):
        if sys.platform != 'win32':
            intt_name = b'int64'
        else:
            intt_name = b'int32'

        class Dataset1(Struct):
            pass  # dummy for testing, mimics behavior of real Dataset

        st = Struct(
            {
                'a': Dataset1({'A': range(10), 'B': range(10, 20)}),
                'b': Struct({'C': 0, 'D': 1, 'E': 2}),
                'c': FastArray(np.arange(5)),
                'd': np.arange(5, 10),
                'e': ['abc', 'def', 'ghi'],
                'f': {'q': 1, 'r': 2},
                'g': 3.14,
                'h': 84,
                'i': None,
                'j': slice(None),
            }
        )
        headers, spec = st.get_table_data()
        self.assertEqual(len(headers), 1)
        self.assertEqual(
            [hd.col_name for hd in headers[0]], ['Name', 'Type', 'Rows', '0', '1', '2']
        )
        self.assertEqual(
            [_r.tolist() for _r in spec],
            [
                [b'a', b'b', b'c', b'd', b'e', b'f', b'g', b'h', b'i', b'j'],
                [
                    b'Dataset1',
                    b'Struct',
                    intt_name,
                    intt_name,
                    b'list',
                    b'dict',
                    b'float',
                    b'int',
                    b'NoneType',
                    b'slice',
                ],
                [b'2', b'3', b'5', b'5', b'3', b'2', b'0', b'0', b'0', b'0'],
                [b'A', b'', b'0', b'5', b'', b'', b'3.14', b'84', b'', b''],
                [b'B', b'', b'1', b'6', b'', b'', b'', b'', b'', b''],
                [b'', b'', b'2', b'7', b'', b'', b'', b'', b'', b''],
            ],
        )
        self.assertEqual(
            str(st),
            f'''#   Name   Type       Rows   0      1   2
-   ----   --------   ----   ----   -   -
0   a      Dataset1   2      A      B
1   b      Struct     3
2   c      {intt_name.decode()}      5      0      1   2
3   d      {intt_name.decode()}      5      5      6   7
4   e      list       3
5   f      dict       2
6   g      float      0      3.14
7   h      int        0      84
8   i      NoneType   0
9   j      slice      0                  ''',
        )
        self.assertEqual(Struct._sizeof_fmt(128), '128.0 B')
        tsize = 1280
        for unit in ['K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']:
            self.assertEqual(Struct._sizeof_fmt(tsize), f'1.2 {unit}B')
            tsize *= 1024
        self.assertEqual(st._last_row_stats(), '[10 columns]')
示例#6
0
 def test_tolist(self):
     data = {'a': 5, 'b': 5.6, 'c': 'fish', 'd': 'μεαν', 'e': True, 'f': {'A': None}}
     st = Struct(data)
     self.assertEqual(st.tolist(), list(data.values()))
示例#7
0
 def test_col_pop(self):
     cols = ['aa', 'b', 'c', 'μεαν']
     dict1 = {_k: [_i] for _i, _k in enumerate(cols)}
     st1 = Struct(dict1)
     val = st1.col_pop('aa')
     self.assertEqual(val, [0])
     self.assertEqual(list(st1.keys()), cols[1:])
     with self.assertRaises(IndexError):
         st1.col_pop('aa')
     with self.assertRaises(IndexError):
         st1.col_pop(['aa'])
     st2 = st1.col_pop(['b', 'c'])
     self.assertEqual(list(st1.keys()), cols[3:])
     self.assertEqual(list(st2.keys()), ['b', 'c'])
     self.assertEqual(st2.b, [1])
     self.assertEqual(st2.c, [2])
     st3 = st1.col_pop(slice(None))
     self.assertEqual(st1.get_ncols(), 0)
     self.assertEqual(st3.get_ncols(), 1)
     self.assertEqual(st3.μεαν, [3])
示例#8
0
 def test_col_remove(self):
     cols = ['aa', 'b', 'c', 'μεαν']
     dict1 = {_k: [_i] for _i, _k in enumerate(cols)}
     st1 = Struct(dict1)
     self.assertEqual(list(st1.keys()), cols)
     st1.col_remove('aa')
     self.assertEqual(list(st1.keys()), cols[1:])
     with self.assertRaises(IndexError):
         st1.col_remove('aa')
     st1.col_remove(['b', 'c'])
     self.assertEqual(list(st1.keys()), cols[3:])
     with self.assertRaises(IndexError):
         st1.col_remove(['μεαν', 'b', 'c'])
     with self.assertRaises(TypeError):
         st1.col_remove(2)
     self.assertEqual(list(st1.keys()), cols[3:])
     st1.col_remove(['μεαν'])
     self.assertEqual(list(st1.keys()), [])
     st1.col_remove([])
     self.assertEqual(list(st1.keys()), [])
示例#9
0
    def test_col_moves(self):
        st = Struct(
            {
                _k: list(range(_i * 10, (_i + 1) * 10))
                for _i, _k in enumerate('abcdefghijklmnop')
            }
        )

        st.col_move_to_front(1)
        self.assertEqual(list(st), list('bacdefghijklmnop'))
        st.col_move_to_front(1)

        st.col_move_to_back(14)
        self.assertEqual(list(st), list('abcdefghijklmnpo'))
        st.col_move_to_back(14)

        with self.assertRaises(ValueError):
            st.col_move_to_front(arange(20))

        st.col_move_to_back(list('dgh'))
        self.assertEqual(list(st), list('abcefijklmnopdgh'))
        st.col_move_to_front(list('gpha'))
        self.assertEqual(list(st), list('gphabcefijklmnod'))
        st.col_move(list('cim'), list('hfo'))
        self.assertEqual(list(st), list('cimgpabejklndhfo'))
        st.col_move_to_front({'g': 1})
        st.col_move_to_front('h')
        with self.assertWarns(UserWarning):
            st.col_move_to_front('q')
        self.assertEqual(list(st), list('hgcimpabejklndfo'))
        st.col_move_to_back({'g': 1})
        st.col_move_to_back('h')
        with self.assertWarns(UserWarning):
            st.col_move_to_back('q')
        self.assertEqual(list(st), list('cimpabejklndfogh'))
示例#10
0
 def test_col_swap(self):
     orig_cols = ['a', 'b', 'c', 'd']
     dict1 = {_k: [_i] for _i, _k in enumerate(orig_cols)}
     st = Struct(dict1)
     for _i, _k in enumerate(orig_cols):
         self.assertEqual(st[_k][0], _i)
     st.a, st.b = st.b, st.a
     self.assertEqual(st['a'][0], 1)
     self.assertEqual(st['b'][0], 0)
     st.col_swap(['a', 'b'], ['b', 'a'])
     for _i, _k in enumerate(orig_cols):
         self.assertEqual(st[_k][0], _i)
     st.col_swap(['a', 'b', 'c'], ['c', 'b', 'a'])
     self.assertEqual(st['a'][0], 2)
     self.assertEqual(st['b'][0], 1)
     self.assertEqual(st['c'][0], 0)
     self.assertEqual(list(st.keys()), orig_cols)
     st = Struct(dict1)
     st.col_swap(['a', 'b', 'c', 'd'], ['c', 'b', 'a', 'd'])
     self.assertEqual(st['a'][0], 2)
     self.assertEqual(st['b'][0], 1)
     self.assertEqual(st['c'][0], 0)
     self.assertEqual(st['d'][0], 3)
     self.assertEqual(list(st.keys()), orig_cols)
示例#11
0
 def test_col_map_01(self):
     orig_cols = ['a', 'b', 'c']
     dict1 = {_k: [_i] for _i, _k in enumerate(orig_cols)}
     st = Struct(dict1)
     with self.assertRaises(ValueError):
         st.col_map({'e': 'f'})  # cannot rename non-existent column
     self.assertEqual(list(st.keys()), orig_cols)
     st.col_map({_k: '{}_{}'.format(_k, _i) for _i, _k in enumerate(st)})
     dict2 = {'{}_{}'.format(_k, _i): [_i] for _i, _k in enumerate(orig_cols)}
     st2 = Struct(dict2)
     self.assertEqual(list(st.keys()), list(st2.keys()))
     # dicts no longer equal
     # for _k in st:
     #    self.assertEqual(st.__dict__[_k], st2.__dict__[_k])
     st = Struct(dict1)
     self.assertEqual(list(st.keys()), orig_cols)
     with self.assertRaises(ValueError):
         st.col_map({'a': 'e', 'b': 'd', 'c': 'd'})
         st.col_map({'q': 'z'})  # cannot rename non-existent column
         st.col_map({'q': 'a'})  # cannot rename non-existent column
     with self.assertRaises(TypeError):
         st.col_map({'a': 'e', 'b': 'd', 'c': 1})
     with self.assertRaises(TypeError):
         st.col_map({1: 'e', 'b': 'd', 'c': 1})
     with self.assertRaises(TypeError):
         st.col_map('a')
     self.assertEqual(list(st.keys()), orig_cols)
     st.col_map({'a': 'f', 'b': 'e', 'c': 'd'})
     self.assertEqual(list(st.keys()), ['f', 'e', 'd'])
     self.assertEqual(st.f, [0])
     self.assertEqual(st.e, [1])
     self.assertEqual(st.d, [2])
示例#12
0
 def test_col_rename(self):
     tempsave = Struct.AllowAnyName
     Struct.AllowAnyName = False
     dict1 = {_k: [_i] for _i, _k in enumerate('abc')}
     st = Struct(dict1)
     with self.assertRaises(ValueError):
         st.col_rename('b', 'no-longer-b')
     orig_cols = list(st.keys())
     st.col_rename('a', 'a')  # no-op
     with self.assertRaises(ValueError):
         st.col_rename('d', 'c')  # cannot rename non-existent column
     self.assertEqual(list(st.keys()), orig_cols)
     with self.assertRaises(ValueError):
         st.col_rename('a', 'c')
     for kwd in keyword.kwlist:
         with self.assertRaises(ValueError):
             st.col_rename('a', kwd)
     for memb in dir(Struct):
         with self.assertRaises(ValueError):
             st.col_rename('a', memb)
     st.col_rename('b', 'no_longer_b')
     self.assertEqual(list(st.keys()), ['a', 'no_longer_b', 'c'])
     with self.assertRaises(TypeError):
         st.col_rename('a')
     Struct.AllowAnyName = tempsave
示例#13
0
 def test_lock(self):
     st1 = Struct({_k: [_i] for _i, _k in enumerate(['a', 'b', 'c'])})
     st1._lock()
     with self.assertRaises(AttributeError):
         st1.a = 1
     with self.assertRaises(AttributeError):
         st1.d = 1
     with self.assertRaises(AttributeError):
         st1['a'] = 1
     with self.assertRaises(AttributeError):
         st1['d'] = 1
     with self.assertRaises(AttributeError):
         del st1.b
     with self.assertRaises(AttributeError):
         st1.col_remove('c')
     with self.assertRaises(AttributeError):
         _ = st1.col_pop('c')
     with self.assertRaises(AttributeError):
         _ = st1.col_rename('c', 'C')
     with self.assertRaises(AttributeError):
         _ = st1.col_map({})
     with self.assertRaises(AttributeError):
         _ = st1.col_move_to_back('c')
     with self.assertRaises(AttributeError):
         _ = st1.col_move_to_front('c')
     st1._unlock()
     st1.a = 1
     st1.d = 1
     del st1.b
     self.assertEqual(list(st1.keys()), ['a', 'c', 'd'])
     self.assertEqual(st1.a, 1)
     self.assertEqual(st1.d, 1)