def test_col_map_03(self): # same as test_col_map_02 but w/ transitions in scrambled order st = Struct( { _k: list(range(_i * 10, (_i + 1) * 10)) for _i, _k in enumerate('abcdefghijklmnopqrst') } ) cmap = { 'a': 'x', # 1-transition 'b': 'c', 'c': 'b', # 2-cycle 'f': 'f', # 1-cycle 'g': 'h', 'k': 'g', 'h': 'j', 'j': 'k', # 4-cycle 'n': 'o', 'm': 'n', 'o': 'y', # 3-transition 'q': 'z', 'p': 'q', 'z': 'r', } # false 3-cycle, really a 2-transition st.col_map(cmap) for _k in 'abcdefghijklmnopqrst': self.assertEqual(st[cmap.get(_k, _k)][1], (ord(_k) - ord('a')) * 10 + 1)
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)
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])