From 604e6d7f9c1472c217195793d970b727b87591bf Mon Sep 17 00:00:00 2001 From: Tobias Schmidt <tobias.schmidt@unibas.ch> Date: Fri, 30 Dec 2011 13:12:17 +0100 Subject: [PATCH] fix bug in table class - fix assign column type as string (not str) if pattern doesn't match - allow more complicated headers (with special characters) - add some test cases --- modules/base/pymod/table.py | 5 +++-- modules/base/tests/test_table.py | 13 +++++++++++++ .../tests/testfiles/ost-table-difficult-headers.tab | 8 ++++++++ modules/base/tests/testfiles/ost-table-notype.tab | 4 ++++ .../base/tests/testfiles/ost-table-unknown-type.tab | 4 ++++ 5 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 modules/base/tests/testfiles/ost-table-difficult-headers.tab create mode 100644 modules/base/tests/testfiles/ost-table-notype.tab create mode 100644 modules/base/tests/testfiles/ost-table-unknown-type.tab diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py index 8c5b13c59..3cc032943 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 42276a657..1f1260bf6 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 000000000..792a25971 --- /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 000000000..10f59019b --- /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 000000000..69c79442b --- /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 -- GitLab