regular expression

W

Wiebke Pätzold

Hi all,

I wrote a little program. Here it is:

import sys
import Mk4py

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

def func(row):
try:
if row.Nachname[0:1]=='G':
return 1
else:
return 0
except AttributeError:
return 0


vf = vw.filter(func)

for r in vf:
print vw[r.index].Nachname,vw[r.index].Kongressbereich


I create a database that contains a table. 'Nachname' and
'Kongressbereich' are special fieldnames. This program can search for
a special letter. In my example it is 'G'. and the search takes place
in 'Nachname'.
Mow 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
Could somebody help me with this task?

Wiebke
 
T

Thomas =?ISO-8859-15?Q?G=FCttler?=

Please cut unimportant parts if you reply.
Can you tell me please on wich place in my program the if-statement
will be inserted?

At the same place where you have it (if ...=="G")
And then I have another question too. After the if statement there
must be a print statement. I have no idea how I must write my program
so that it is right and executably.
Can you write it me exactly how this program should look like?

You seem to be running on windows ("C:\..."). I think double-clicking
on it should execute it.

If you want to make it executable without python installed: Have a look
at py2exe.

Since you seem to be a german: Maybe my Python introduction helps you:
http://www.thomas-guettler.de/vortraege/python/einfuehrung.html

thomas
 
W

Wiebke Pätzold

These are all untested, because I don't have Mk4py or your datafile to
try it on.

I might write this:
import re
pattern = re.compile("^Ge")
def func(row):
try:
nachname = row.Nachname
except AttributeError:
return 0
return pattern.search(nachname) is not None

vf = vw.filter(func)

If you're using a Python version with nested scopes, you could use them
in this case:
import re
def make_func(pattern):
pattern = re.compile(pattern)
def func(row):
try:
nachname = row.Nachname
except AttributeError:
return 0
return pattern.search(nachname) is not None
return func

vf = vw.filter(make_func("^Ge"))

or you can make a callable filter object by using classes:
import re
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"))

Jeff

thank you for the part of the program.
But I have one question.
If I insert your first suggestion- there is print nothin. Is it
possible that there is missing something?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top