Пример #1
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)
Пример #2
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'))