def test_read_and_save_attributes(self): samplefile = """\ Feature 1\tFeature 2\tClass 1\tClass 42 d \tM F \td \td \ta=1 b=2 \tclass x=a\\ longer\\ string \tclass 1.0 \tM \t5 \trich """ file = io.StringIO(samplefile) table = read_tab_file(file) f1, f2, c1, c2 = table.domain.variables self.assertIsInstance(f2, DiscreteVariable) self.assertEqual(f2.name, "Feature 2") self.assertEqual(f2.attributes, {'a': 1, 'b': 2}) self.assertIn(c1, table.domain.class_vars) self.assertIsInstance(c1, DiscreteVariable) self.assertEqual(c1.name, "Class 1") self.assertEqual(c1.attributes, {'x': 'a longer string'}) outf = io.StringIO() outf.close = lambda: None TabReader.write_file(outf, table) saved = outf.getvalue() file = io.StringIO(saved) table = read_tab_file(file) f1, f2, c1, c2 = table.domain.variables self.assertIsInstance(f2, DiscreteVariable) self.assertEqual(f2.name, "Feature 2") self.assertEqual(f2.attributes, {'a': 1, 'b': 2}) self.assertIn(c1, table.domain.class_vars) self.assertIsInstance(c1, DiscreteVariable) self.assertEqual(c1.name, "Class 1") self.assertEqual(c1.attributes, {'x': 'a longer string'})
def test_read_and_save_attributes(self): samplefile = """\ Feature 1\tFeature 2\tClass 1\tClass 42 d \tM F \td \td \ta=1 b=2 \tclass x=a\\ longer\\ string \tclass 1.0 \tM \t5 \trich """ file = io.StringIO(samplefile) table = read_tab_file(file) f1, f2, c1, c2 = table.domain.variables self.assertIsInstance(f2, DiscreteVariable) self.assertEqual(f2.name, "Feature 2") self.assertEqual(f2.attributes, {"a": 1, "b": 2}) self.assertIn(c1, table.domain.class_vars) self.assertIsInstance(c1, DiscreteVariable) self.assertEqual(c1.name, "Class 1") self.assertEqual(c1.attributes, {"x": "a longer string"}) outf = io.StringIO() outf.close = lambda: None TabReader.write_file(outf, table) saved = outf.getvalue() file = io.StringIO(saved) table = read_tab_file(file) f1, f2, c1, c2 = table.domain.variables self.assertIsInstance(f2, DiscreteVariable) self.assertEqual(f2.name, "Feature 2") self.assertEqual(f2.attributes, {"a": 1, "b": 2}) self.assertIn(c1, table.domain.class_vars) self.assertIsInstance(c1, DiscreteVariable) self.assertEqual(c1.name, "Class 1") self.assertEqual(c1.attributes, {"x": "a longer string"}) path = "/path/to/somewhere" c1.attributes["path"] = path outf = io.StringIO() outf.close = lambda: None TabReader.write_file(outf, table) outf.seek(0) table = read_tab_file(outf) f1, f2, c1, c2 = table.domain.variables self.assertEqual(c1.attributes["path"], path)
def test_read_save_quoted(self): quoted = '''\ S\tA s\td m\t """a"""\ti """b"""\tj """c\td"""\tk ''' expected = ['"a"', '"b"', '"c\td"'] f = io.StringIO(quoted) table = read_tab_file(f) self.assertSequenceEqual(table.metas[:, 0].tolist(), expected) f = io.StringIO() f.close = lambda: None TabReader.write_file(f, table) saved = f.getvalue() table1 = read_tab_file(io.StringIO(saved)) self.assertSequenceEqual(table1.metas[:, 0].tolist(), expected)