Classes in Python
Wiebke Pätzold
wiebke.paetzold at mplusr.de
Tue Aug 5 05:25:42 EDT 2003
On Tue, 05 Aug 2003 10:59:56 +0200, Peter Otten <__peter__ at web.de>
wrote:
>Judging from the Getting started section in
>http://www.equi4.com/metakit/python.html,
>vw[r.index] seems redundent, so
>
><not tested>
>import Mk4py
>db = Mk4py.storage("c:\\datafile.mk", 1)
>vw = db.view("people")
>
>for r in vw:
> if r.Nachname.startswith("Ge"):
> print r.Nachname
></not tested>
>
>would be the code to go with.
>Less code == fewer occasions for errors,
>and it might even run faster :-)
>
>Peter
Hi Peter,
it's great that you give yourself so much trouble. But want to create
this program by classes.
The programm that I create with functions is OK.
Here is it. Perhaps it may help you.
import sys
import Mk4py
import re
db = Mk4py.storage("c:\\datafile.mk",1)
vw = db.view("people")
pattern = re.compile("ra.*")
def func(row):
try:
nachname = row.Nachname
except AttributeError:
return 0
return pattern.search(nachname) is not None
vf = vw.filter(func)
for r in vf:
print vw[r.index].Nachname
Now I trid to create this program by classes:
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("Ge.*"))
for r in vf:
print vw[r.index].Nachname
But here I get an error: returned exit code 0.
So I think that something is wrong with line:
vf = vw.filter(PatternFilter("Ge.*"))
My task is:
I create a database that contains a table. For example'Nachname'
This is a special fieldname. This program can search for
a special letter. In my example it is 'G'. and the search takes place
in 'Nachname'.
Now I want to use regular expression. So that I can limit my search.
For example: I can search for Ge and it is not relevant wich letters
follow. And it should be solve under use classes.
Wiebke
More information about the Python-list
mailing list