union in Python
Wiebke Pätzold
wiebke.paetzold at mplusr.de
Wed Aug 13 03:51:12 EDT 2003
Hi Andy,
I'm very new in Python and this is why I ask this stupid question.
I create a database that contains a table. 'Themenbereiche' is one of
13column names. This program can search for
a special letter. In my example it is 'do'. and the search takes place
in 'Themenbereiche'. 'do' takes place within a word. This is solved
with regular expression. So that I can limit my search.
For example: I can search for 'do' and it is not relevant wich letters
follow or wich letters are in front of 'do'.
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, feld):
self.feld = feld
self.pattern = re.compile(pattern)
def __call__(self, row):
try:
exec(self.feld+" = row."+self.feld)
except AttributeError:
return 0
return self.pattern.search(eval(self.feld))is not None
feld = "Themenbereiche"
vf = vw.filter(PatternFilter("do.*", feld))
for r in vf:
exec("print vw[r.index]." +feld)
Now I want to change my program that I can look for the same regular
expression in two columns ('Themenbereiche', 'Nachname').
I tied something
>> import sys
>> import Mk4py
>> import re
>>
>> db = Mk4py.storage("c:\\datafile.mk",1)
>> vw = db.view("people")
>>
>> class PatternFilter:
>> def __init__(self, pattern, feld):
>> self.feld = feld
>> self.pattern = re.compile(pattern)
>>
>> def __call__(self, row):
>> try:
>> exec(self.feld+" = row."+self.feld)
>> except AttributeError:
>> return 0
>> return self.pattern.search(eval(self.feld))is not None
>>
>> def union(feld_th, feld_na):
>> c = feld_th[:]
>> for i in range(len(feld_na)):
>> if feld_na[i] not in feld_th:
>> c.append(feld_na[i])
>> return c
>>
>> feld_th = "Themenbereiche"
>> vf = vw.filter(PatternFilter("do.*", feld_th))
>>
>> feld_na = "Nachname"
>> vf = vw.filter(PatternFilter("im.*", feld_na))
>>
>> print feld_th, feld_na, union(feld_th, feld_na)
But it seems to be worng.
I didn't find verx much material in order to solve the task.
But I tried it.
I don't get the results I except. I get actual traceback error:
>>> Themenbereiche Nachname
Traceback (most recent call last):
File
"C:\PROGRA~1\Python22\lib\site-packages\Pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
exec codeObject in __main__.__dict__
File
"C:\Programme\Python22\Lib\site-packages\Pythonwin\pywin\Demos\Uebung11.py",
line 46, in ?
print feld_th, feld_na, union(feld_th, feld_na)
NameError: name 'union' is not defined
>>>
More information about the Python-list
mailing list