Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > union in Python

Reply
Thread Tools

union in Python

 
 
Wiebke Pätzold
Guest
Posts: n/a
 
      08-12-2003
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)

 
Reply With Quote
 
 
 
 
Wiebke Pätzold
Guest
Posts: n/a
 
      08-12-2003

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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?J=F8rgen_Cederberg?=
Guest
Posts: n/a
 
      08-12-2003
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




 
Reply With Quote
 
Wiebke Pätzold
Guest
Posts: n/a
 
      08-12-2003
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
>>>

 
Reply With Quote
 
Andy Todd
Guest
Posts: n/a
 
      08-12-2003
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/



 
Reply With Quote
 
=?ISO-8859-1?Q?J=F8rgen_Cederberg?=
Guest
Posts: n/a
 
      08-13-2003
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 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.





 
Reply With Quote
 
Wiebke Pätzold
Guest
Posts: n/a
 
      08-13-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
>>>







 
Reply With Quote
 
Andy Todd
Guest
Posts: n/a
 
      08-13-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/



 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      08-13-2003

"Wiebke Pätzold" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
SQL UNION -like filtering of offline DataSet into DataView for WebControl binding Marco Ippolito ASP .Net 2 05-19-2004 12:35 AM
union in struct without union name Peter Dunker C Programming 2 04-26-2004 07:23 PM
map XML union to C union (and vice-versa) Matt Garman XML 1 04-25-2004 12:40 AM
THE STATE OF THE UNION Jenna Bush MCSE 95 01-30-2004 02:02 PM
Re: union in Python Gustavo Niemeyer Python 4 08-20-2003 01:10 AM



Advertisments