diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 8c5b13c59903353299d81939070e00b07e72a5fb..3cc032943220ce346b79f2fb1b88b983e979a17f 100644 --- a/modules/base/pymod/table.py +++ b/modules/base/pymod/table.py @@ -487,7 +487,7 @@ class Table(object): @staticmethod def _LoadOST(stream_or_filename): - fieldname_pattern=re.compile(r'(?P<name>[A-Za-z0-9_]+)(\[(?P<type>\w+)\])?') + fieldname_pattern=re.compile(r'(?P<name>[^[]+)(\[(?P<type>\w+)\])?') if not hasattr(stream_or_filename, 'read'): stream=open(stream_or_filename, 'r') else: @@ -510,7 +510,7 @@ class Table(object): if match.group('type'): fieldtypes.append(match.group('type')) else: - fieldtypes.append('str') + fieldtypes.append('string') fieldnames.append(match.group('name')) tab=Table(fieldnames, fieldtypes) header=True @@ -526,6 +526,7 @@ class Table(object): for row in self.rows: for idx in range(len(row)): row[idx]=self._Coerce(row[idx], self.col_types[idx]) + @staticmethod def _LoadCSV(stream_or_filename, sep): if not hasattr(stream_or_filename, 'read'): diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py index 42276a6577b3f70b839baa64368129835f421831..1f1260bf6b801e45738df9f36d3f6406f78ba5a4 100644 --- a/modules/base/tests/test_table.py +++ b/modules/base/tests/test_table.py @@ -713,6 +713,17 @@ class TestTable(unittest.TestCase): tab.Sort('third', '+') self.CompareDataFromDict(tab, {'first': [None,'foo','x'], 'second': [9,None,3], 'third': [3.3,2.2,None]}) + def testLoadTableOSTUnknownType(self): + self.assertRaises(ValueError, Table.Load, os.path.join('testfiles','ost-table-unknown-type.tab')) + + def testLoadTableOSTNoType(self): + tab = Table.Load(os.path.join('testfiles','ost-table-notype.tab')) + self.CompareDataFromDict(tab, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) + + def testLoadOSTDifficultHeaders(self): + tab = Table.Load(os.path.join('testfiles','ost-table-difficult-headers.tab')) + self.assertEquals(tab.col_types, ['float','float','float','float','float']) + def testSaveLoadTableOST(self): tab = self.CreateTestTable() self.CompareDataFromDict(tab, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) @@ -738,6 +749,7 @@ class TestTable(unittest.TestCase): self.assertRaises(IOError, Table.Load, os.path.join('testfiles','emptytable.tab')) in_stream = open(os.path.join('testfiles','emptytable.csv'), 'r') self.assertRaises(IOError, Table.Load, in_stream) + def testSaveLoadTableCSV(self): tab = self.CreateTestTable() self.CompareDataFromDict(tab, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) @@ -757,6 +769,7 @@ class TestTable(unittest.TestCase): # check content self.CompareDataFromDict(tab_loaded_stream, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) self.CompareDataFromDict(tab_loaded_fname, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) + def testSaveLoadTablePickle(self): tab = self.CreateTestTable() self.CompareDataFromDict(tab, {'first': ['x','foo',None], 'second': [3,None,9], 'third': [None,2.2,3.3]}) diff --git a/modules/base/tests/testfiles/ost-table-difficult-headers.tab b/modules/base/tests/testfiles/ost-table-difficult-headers.tab new file mode 100644 index 0000000000000000000000000000000000000000..792a25971450fa7150964dc540539b8a24cfaf49 --- /dev/null +++ b/modules/base/tests/testfiles/ost-table-difficult-headers.tab @@ -0,0 +1,8 @@ +cut-off[float] a_sangle[float] smoo:thing[float] 3O89.pdb.gz[float] 3P0K;pdbgz[float] +1.0 20.0 0.0 0.5 0.5 +1.0 20.0 2.0 0.600685362782 0.774702862809 +1.0 20.0 4.0 0.749697277923 0.872928615954 +1.0 20.0 6.0 0.775949336433 0.864866900109 +1.0 20.0 8.0 0.673823016565 0.818450326527 +1.0 20.0 10.0 0.528419548581 0.779631093875 +1.0 20.0 12.0 0.463080015499 0.761846789747 diff --git a/modules/base/tests/testfiles/ost-table-notype.tab b/modules/base/tests/testfiles/ost-table-notype.tab new file mode 100644 index 0000000000000000000000000000000000000000..10f59019bc567304bf57520c2f6c0e8f99d308f4 --- /dev/null +++ b/modules/base/tests/testfiles/ost-table-notype.tab @@ -0,0 +1,4 @@ +first second[int] third[float] +x 3 NA +foo NA 2.2 +NA 9 3.3 diff --git a/modules/base/tests/testfiles/ost-table-unknown-type.tab b/modules/base/tests/testfiles/ost-table-unknown-type.tab new file mode 100644 index 0000000000000000000000000000000000000000..69c79442b33df285507c7f42845f73aa09663ed0 --- /dev/null +++ b/modules/base/tests/testfiles/ost-table-unknown-type.tab @@ -0,0 +1,4 @@ +first[unknown] second[int] third[float] +x 3 NA +foo NA 2.2 +NA 9 3.3