union in Python

Andy Todd andy47 at halfcooked.com
Wed Aug 13 11:43:47 CEST 2003


Wiebke Pätzold wrote:
> 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
> 
> 
> 
> 
> 
> 
> 

A couple of points. As the other people in this thread have mentioned, 
you could probably benefit from reading a little about Python.

The exact reason you are seeing this error is because you have defined 
'union' as a method on the class PatternFilter and not as a standalone 
function. If you reduce the indentation of that fragment of code by four 
spaces you may well find that the code runs.

It may not do what you want though, and I suspect that your problem 
could be solved more simply, although I'm still not sure what exactly 
you are trying to do.

Regards,
Andy
-- 
--------------------------------------------------------------------------------
 From the desk of Andrew J Todd esq - http://www.halfcooked.com/







More information about the Python-list mailing list