classes (table)

  • Thread starter Wiebke Pätzold
  • Start date
W

Wiebke Pätzold

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'.
This is the program that I wrote:

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


The program should have two possibilities to search for regular
expressions. The first possibility is in this program. Here I can
look for a certain expression. In my example I can look for 'ra' and
it is not relevant wich letters follow or wich letters are in front of
'ra'. But this search can ONLY takes place in the column 'Nachname'.
'Nachname is fixed by the programmer.
The second possibility is that the user and NOT the programmer
determine the column name in which the user want to look for a regular
expression. Only the user can determine in which column the search
takes place.
These 2 kinds of the search must be combined in the program.

I hope somebody can help me with my problem!

Wiebke
 
A

Alex Martelli

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
 
W

Wiebke Pätzold

Wiebke Pätzold wrote:
...

I'm not experienced in this package, so I'm not sure this will
work and can't test, but:


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.

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.
 
C

Christopher Boomer

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

You should probably start by rewriting your basic function as

def search_call (self, row, column):
if not hasattr(row, column):
return 0 # No such column
nachname=getattr(row, column)
return self.pattern.search(nachname) is not None

Now you can choose the column at will within the program.
(nachname is now a poor choice of variable name)

To restrict access to columns, restrict this function and provide as a
public function the following:

def __call__ (self, row):
return search_call(row, 'Nachname')

Hope this helps.

MfG
Christopher Boomer.
 
H

Heiko Wundram

but the user of the program should have the possibility to select in
wich column the search takes place.

If you're not willing to start thinking Python and read some
documentation before asking questions as these (have you ever seriously
programmed before? If not, go read the tutorials on
http://www.python.org), I guess there's no reason I'd help you. Unless
you pay for my programming time, of course...

Heiko.

PS: I know Alex Martelli has answered already, but I just thought that
it might be worth noting that Wiebke is simply asking the group to write
a program she needs, which is not what I think comp.lang.python is
for...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top