abstraction of the column names (classes)

Peter Abel p-abel at t-online.de
Tue Aug 5 23:00:23 CEST 2003


Wiebke Pätzold <wiebke.paetzold at mplusr.de> wrote in message news:<h29viv0b74ib3tc1g7eem3eo26fsjv93bj at 4ax.com>...
> Hi all!
> 
> I create a database that contains a table. 'Nachname'  is one of 13
> column names. This program can search for
> a special letter. In my example it is 'ra'. and the search takes place
> in 'Nachname'. 'ra' takes place within a word. This is solved with
> regular expression. So that I can limit my search.
> For example: I can search for 'ra' and it is not relevant wich letters
> follow or wich letters are in front of 'ra'.
> Now I want to abstract the column name 'Nachname'. So I search in an
> unknown column. Only the user of the program should determine in wich
> class the search takes place. This input should happen in the view
> lines of testing the class.
> My task is it to change the program that a abstraction of the column
> names takes place.
> Please give me a detailed answer. If it is possible the changed
> program because I am very new in Python and I am orientationless.
> 
> This is the program
> 
> import sys
> import Mk4py
> import re
> 
> db = Mk4py.storage("c:\\datafile.mk",1)
> vw = db.view("people")
> 
> class PatternFilter:
>     def __init__(self, pattern):
>         self.pattern = re.compile(pattern)
> 
>     def __call__(self, row):
>         try:
>             nachname = row.Nachname
>         except AttributeError:
>             return 0
>         return self.pattern.search(nachname)is not None
> 
> vf = vw.filter(PatternFilter("ra.*"))
> 
> for r in vf:
>     print  vw[r.index].Nachname

RegEx are really great and there are a lot of problems you can 
only solve with them. But if I understand you right your problem
is to find a string where a substring is to be in, the simple
string operation does the work:
if String.find(substring) > -1:

**find** will give you the position in String where substring
will occure the 1rst time and -1 if substring doesn't occur.

e.g.
>>> nameList=['Meyer', 'Maxon', 'Johnson', 'Mueller']
>>> for name in nameList:
... 	if name.find('er')>-1:
... 		print name
... 		
Meyer
Mueller
>>> 

So if you can get your columns contents in **ColumnList**
and your searchpattern in **SearchPattern** so the following
function could do the work:

def getPatternMatches(ColumnList,SearchPattern):
  foundList=[]
  for n in ColumnList:
    if n.find(SearchPattern) > -1:
      foundList.append(n)
  return foundList

Regards
Peter




More information about the Python-list mailing list