classes (table)

Alex Martelli aleax at aleax.it
Wed Aug 6 11:58:52 EDT 2003


Wiebke Pätzold wrote:

> On Wed, 06 Aug 2003 11:31:39 GMT, Alex Martelli <aleax at aleax.it>
> wrote:
> 
>>Wiebke Pätzold wrote:
>>   ...
>>> import Mk4py
>>
>>I'm not experienced in this package, so I'm not sure this will
>>work and can't test, but:
>>
>>>     def __call__(self, row):
>>>         try:
>>>             nachname = row.Nachname
>>
>>Just change this to
>>
>>              whatever = getattr(row, self.attributeName)
>>
>>Where self.attributeName can be any string, e.g. defaulting
>>to 'Nachname' if you wish, as set e.g. in __init__.
>>
>>
>>Alex
> 
> but the user of the program should have the possibility to select in
> wich column the search takes place.

And he or she will, just as long as you provide said user with
UI means to set the value of self.attributeName.  What's so
hard about it?


> On the one hand the program must be in the situation to search in all
> columns of the table for a special word. And on the other hand the
> user have to determine in wich column the search after this special
> word takes place. This is the task. And this two things must combined
> in one program.

Yes, you've said so a zillion times.  So give your PatternFilter class's
__init__ TWO arguments, already (it needs two, so why not give them
two?), the regex to search for AND the column name to search on.  Just
as I said above.  You might not have known about the getattr built-in
function, but that's exactly what I'm pointing out there.  So what's
missing?  There, let me try to put this into a program you might like,
even though I can't run it because I don't use the Mk4py module on
which all depends (and it's QUITE inconsiderate of you not to have that
fact in better evidence, even in the subjects of your posts!!!):


import sys
import Mk4py
import re

db = Mk4py.storage("c:\\datafile.mk",1)
vw = db.view("people")

class PatternFilter:

    def __init__(self, pattern, colum):
        self.pattern = re.compile(pattern)
        self.attributeName = colum

    def __call__(self, row):
        try:
            whatever = getattr(self.row, self.attributeName)
        except AttributeError:
            return 0
        return self.pattern.search(whatever) is not None

regex = raw_input("Please enter the regex to search for:")
colum = raw_input("Please enter the name of the column to search on:")

vf = vw.filter(PatternFilter(regex, colum))

for r in vf:
    print  getattr(vw[r.index], colum, "*NO COLUMN NAMED %r!!!*" % colum)



Alex





More information about the Python-list mailing list