From 4e42cdd9a8e42aad20c8ccaa9543d37f08ce0951 Mon Sep 17 00:00:00 2001
From: Tobias Schmidt <tobias.schmidt@unibas.ch>
Date: Mon, 30 Jul 2012 11:15:42 +0200
Subject: [PATCH] TableClass: add method to search column names

---
 modules/base/pymod/table.py      | 16 ++++++++++++++++
 modules/base/tests/test_table.py |  4 ++++
 modules/doc/table.rst            |  1 +
 3 files changed, 21 insertions(+)

diff --git a/modules/base/pymod/table.py b/modules/base/pymod/table.py
index 7d0b53268..4eb8bdeb1 100644
--- a/modules/base/pymod/table.py
+++ b/modules/base/pymod/table.py
@@ -314,6 +314,22 @@ class Table(object):
     '''
     return self.col_names
   
+  def SearchColNames(self, regex):
+    '''
+    Returns a list of column names matching the regex
+
+    :param regex: regex pattern
+    :type regex: :class:`str`
+
+    :returns: :class:`list` of column names (:class:`str`)
+    '''
+    matching_names = []
+    for name in self.col_names:
+      matches = re.search(regex, name)
+      if matches:
+        matching_names.append(name)
+    return matching_names
+
   def HasCol(self, col):
     '''
     Checks if the column with a given name is present in the table.
diff --git a/modules/base/tests/test_table.py b/modules/base/tests/test_table.py
index 21fd58a67..5e2e82824 100644
--- a/modules/base/tests/test_table.py
+++ b/modules/base/tests/test_table.py
@@ -163,6 +163,10 @@ class TestTable(unittest.TestCase):
     diff = ImageChops.difference(img1, img2)
     self.assertEqual(diff.getbbox(),None)
 
+  def testSearchColNames(self):
+    tab = self.CreateTestTable()
+    self.assertEquals(tab.SearchColNames('d$'), ['second', 'third'])
+    self.assertEquals(tab.SearchColNames('(first|third)'), ['first','third'])
 
   def testZip(self):
     tab=Table(['col1', 'col2', 'col3', 'col4'], 'sssi')
diff --git a/modules/doc/table.rst b/modules/doc/table.rst
index 19d9c3c27..73bc6cc79 100644
--- a/modules/doc/table.rst
+++ b/modules/doc/table.rst
@@ -64,6 +64,7 @@ Functions You Might be Interested In
 :meth:`~ost.table.Table.Sort`                 sort table by column
 :meth:`~ost.table.Table.Filter`               filter table by values
 :meth:`~ost.table.Table.Zip`                  extract multiple columns at once
+:meth:`~ost.table.Table.SearchColNames`       search for matching column names
 
 **Input/Output**
 :meth:`~ost.table.Table.Save`                 save a table to a file
-- 
GitLab