union in Python

Discussion in 'Python' started by Wiebke Pätzold, Aug 12, 2003.

  1. 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)
    Wiebke Pätzold, Aug 12, 2003
    #1
    1. Advertising

  2. 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
    Wiebke Pätzold, Aug 12, 2003
    #2
    1. Advertising

  3. 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 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
    =?ISO-8859-1?Q?J=F8rgen_Cederberg?=, Aug 12, 2003
    #3
  4. 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
    >>>
    Wiebke Pätzold, Aug 12, 2003
    #4
  5. Wiebke Pätzold

    Andy Todd Guest

    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 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
    --
    --------------------------------------------------------------------------------
    From the desk of Andrew J Todd esq - http://www.halfcooked.com/
    Andy Todd, Aug 12, 2003
    #5
  6. 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.
    =?ISO-8859-1?Q?J=F8rgen_Cederberg?=, Aug 13, 2003
    #6
  7. 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:

    >>> 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
    >>>
    Wiebke Pätzold, Aug 13, 2003
    #7
  8. Wiebke Pätzold

    Andy Todd Guest

    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 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:
    >
    >
    >>>>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/
    Andy Todd, Aug 13, 2003
    #8
  9. Wiebke Pätzold

    Terry Reedy Guest

    "Wiebke Pätzold" <> wrote in message
    news:...
    > 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
    Terry Reedy, Aug 13, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marco Ippolito
    Replies:
    2
    Views:
    3,188
    Marco Ippolito
    May 19, 2004
  2. goli

    SQL UNION query in Java

    goli, Apr 4, 2005, in forum: Java
    Replies:
    4
    Views:
    2,565
  3. Matt Garman
    Replies:
    1
    Views:
    654
    Matt Garman
    Apr 25, 2004
  4. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    854
    Chris Torek
    Apr 26, 2004
  5. Gustavo Niemeyer

    Re: union in Python

    Gustavo Niemeyer, Aug 18, 2003, in forum: Python
    Replies:
    4
    Views:
    399
    Bengt Richter
    Aug 20, 2003
Loading...

Share This Page