union in Python

W

Wiebke Pätzold

import sys
Hi all,

At the beginning there is a table (database) with different columns.
So I create a search operator to look for regular expressions. First I
start the search only in one column and the program run. Now I want to
expand the search. The search have to take place in 2 columns. In my
example it is "Themenbereiche" and "Nachname". I tried something. But
the program doesn't run.
Can somebody help with the release of the error?

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 union1(feld_th, feld_na):
result = []
for i in feld_th+feld_na:
if i not in result:
result.append(i)
return result

def union2(feld_th, feld_na):
result = {}
for i in feld_th+feld_na:
tmp = 1
return tmp.keys()

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, union1(feld_th, feld_na)
print feld_th, feld_na, union2(feld_th, feld_na)
 
W

Wiebke Pätzold

Hi all,

about union I found still something else. Perhaps it is better. But
here the program doesn't run too.
Can me somebody help?

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 not in feld_th:
c.append(feld_na)
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, union1(feld_th, feld_na)
print feld_th, feld_na, union2(feld_th, feld_na)

Wiebke
 
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Wiebke said:
Hi all,

about union I found still something else. Perhaps it is better. But
here the program doesn't run too.

What are the errors?
Can me somebody help?
Somebody probably could, but as you haven't described what is wrong it
is very difficult to help you. You could/should copy-paste the
error-traceback that was printed when your program failed.
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 not in feld_th:
c.append(feld_na)
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, union1(feld_th, feld_na)
print feld_th, feld_na, union2(feld_th, feld_na)


Where are the functions union1 and union2 defined and why do you want to
find the union between two strings, it doesn't make sense. On the other
hand, if feld_th and feld_na where two lists it would make sense.

Can you come up with a simpler example, and demonstrate what goes wrong?

I don't have mk4py installed, and don't intend installing it.

regards
Jorgen
 
W

Wiebke Pätzold

This is the error-traceback that is print whenmy program failed:

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, union1(feld_th, feld_na)
NameError: name 'union1' is not defined
 
A

Andy Todd

Wiebke said:
Hi all,

about union I found still something else. Perhaps it is better. But
here the program doesn't run too.
Can me somebody help?

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 not in feld_th:
c.append(feld_na)
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, union1(feld_th, feld_na)
print feld_th, feld_na, union2(feld_th, feld_na)

Wiebke


Err no, can't help you at the moment. You need to supply more information.

I'm not sure I understand what you are doing, or trying to do. Can you
describe what you expect to happen and explain how that differs from
what you actually get.

You also say that the program doesn't run but don't say how - or why. Do
you not get the results you expect? Do you get actual traceback errors
from Python?

Regards,
Andy
 
?

=?ISO-8859-1?Q?J=F8rgen_Cederberg?=

Wiebke Pätzold wrote:

Hello Wiebke

you did what I asked, but you forgot to read to whole message I wrote to
you.
This is the error-traceback that is print whenmy program failed:


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, union1(feld_th, feld_na)
NameError: name 'union1' is not defined

The following is taken from my previous message to you:
Where are the functions union1 and union2 defined and why do you want to find the union between two strings, it doesn't make sense. On the other hand, if feld_th and feld_na where two lists it would make sense.

And this is exactly what caused the error. The last line of your
traceback states the problem: > NameError: name 'union1' is not defined
You should really read the tracebacks carefully, they tell what went
wrong and where it went wrong.

You probably made the functions somewhere else, but I still don't see
the point in taking the union of two strings.

Do you care to explain how the functions work or how you intend to get
them working or do just want a solution to your assigment?

Regards
Jorgen

Ps.. You should really do your self a favour and download the
documentation: http://www.python.org/doc/current/download.html
Read the tutorial carefully and try to do some the examples in it.
 
W

Wiebke Pätzold

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 not in feld_th:
c.append(feld_na)
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:
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

Andy Todd

Wiebke said:
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 not in feld_th:
c.append(feld_na)
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:


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
 
T

Terry Reedy

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

When looking for a particular string, rather than a pattern, you are
probably better off to use the find or index method of that string
rather than re.
3

Terry J. Reedy
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top