473,487 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

union in Python

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[i] = 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)

Jul 18 '05 #1
8 8206

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

Wiebke

Jul 18 '05 #2
Wiebke Pätzold wrote:
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[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, 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


Jul 18 '05 #3
This is the error-traceback that is print whenmy program failed:

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

Jul 18 '05 #4
Wiebke Pätzold wrote:
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[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, 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
--
--------------------------------------------------------------------------------
From the desk of Andrew J Todd esq - http://www.halfcooked.com/

Jul 18 '05 #5
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:

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, 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 otherhand, 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.

Jul 18 '05 #6
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



Jul 18 '05 #7
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/

Jul 18 '05 #8

"Wiebke Pätzold" <wi*************@mplusr.de> wrote in message
news:u6********************************@4ax.com...
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.
'abcdefg'.find('d')

3

Terry J. Reedy
Jul 18 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
8122
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
3311
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under...
2
4337
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes...
10
5046
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
5033
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the...
73
3939
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
30
3223
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
3813
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big...
5
44433
by: TokiDoki | last post by:
Hi! I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly...
0
6967
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5442
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.