union in Python

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wiebke Pätzold

    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(patt ern)

    def __call__(self, row):
    try:
    exec(self.feld+ " = row."+self.feld )
    except AttributeError:
    return 0
    return self.pattern.se arch(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(Patte rnFilter("do.*" , feld_th))

    feld_na = "Nachname"
    vf = vw.filter(Patte rnFilter("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

    #2
    Re: union in Python


    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(patt ern)

    def __call__(self, row):
    try:
    exec(self.feld+ " = row."+self.feld )
    except AttributeError:
    return 0
    return self.pattern.se arch(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_n a[i])
    return c

    feld_th = "Themenbereiche "
    vf = vw.filter(Patte rnFilter("do.*" , feld_th))

    feld_na = "Nachname"
    vf = vw.filter(Patte rnFilter("im.*" , feld_na))

    print feld_th, feld_na, union1(feld_th, feld_na)
    print feld_th, feld_na, union2(feld_th, feld_na)

    Wiebke

    Comment

    • Jørgen Cederberg

      #3
      Re: union in Python

      Wiebke Pätzold wrote:[color=blue]
      > Hi all,
      >
      > about union I found still something else. Perhaps it is better. But
      > here the program doesn't run too.[/color]

      What are the errors?
      [color=blue]
      > Can me somebody help?[/color]
      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.
      [color=blue]
      >
      > 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(patt ern)
      >
      > def __call__(self, row):
      > try:
      > exec(self.feld+ " = row."+self.feld )
      > except AttributeError:
      > return 0
      > return self.pattern.se arch(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_n a[i])
      > return c
      >
      > feld_th = "Themenbereiche "
      > vf = vw.filter(Patte rnFilter("do.*" , feld_th))
      >
      > feld_na = "Nachname"
      > vf = vw.filter(Patte rnFilter("im.*" , feld_na))
      >
      > print feld_th, feld_na, union1(feld_th, feld_na)
      > print feld_th, feld_na, union2(feld_th, feld_na)[/color]

      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




      Comment

      • Wiebke Pätzold

        #4
        Re: union in Python

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

        [color=blue][color=green][color=darkred]
        >>> Themenbereiche Nachname[/color][/color][/color]
        Traceback (most recent call last):
        File
        "C:\PROGRA~1\Py thon22\lib\site-packages\Python win\pywin\frame work\scriptutil s.py",
        line 310, in RunScript
        exec codeObject in __main__.__dict __
        File
        "C:\Programme\P ython22\Lib\sit e-packages\Python win\pywin\Demos \Uebung11.py",
        line 46, in ?
        print feld_th, feld_na, union1(feld_th, feld_na)
        NameError: name 'union1' is not defined[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Comment

        • Andy Todd

          #5
          Re: union in Python

          Wiebke Pätzold wrote:[color=blue]
          > 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(patt ern)
          >
          > def __call__(self, row):
          > try:
          > exec(self.feld+ " = row."+self.feld )
          > except AttributeError:
          > return 0
          > return self.pattern.se arch(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_n a[i])
          > return c
          >
          > feld_th = "Themenbereiche "
          > vf = vw.filter(Patte rnFilter("do.*" , feld_th))
          >
          > feld_na = "Nachname"
          > vf = vw.filter(Patte rnFilter("im.*" , feld_na))
          >
          > print feld_th, feld_na, union1(feld_th, feld_na)
          > print feld_th, feld_na, union2(feld_th, feld_na)
          >
          > Wiebke
          >[/color]

          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/



          Comment

          • Jørgen Cederberg

            #6
            Re: union in Python

            Wiebke Pätzold wrote:

            Hello Wiebke

            you did what I asked, but you forgot to read to whole message I wrote to
            you.
            [color=blue]
            > This is the error-traceback that is print whenmy program failed:
            >
            > [color=green][color=darkred]
            >>>> Themenbereiche Nachname[/color][/color]
            > Traceback (most recent call last):
            > File
            > "C:\PROGRA~1\Py thon22\lib\site-packages\Python win\pywin\frame work\scriptutil s.py",
            > line 310, in RunScript
            > exec codeObject in __main__.__dict __
            > File
            > "C:\Programme\P ython22\Lib\sit e-packages\Python win\pywin\Demos \Uebung11.py",
            > line 46, in ?
            > print feld_th, feld_na, union1(feld_th, feld_na)
            > NameError: name 'union1' is not defined[color=green][color=darkred]
            >>>> [/color][/color][/color]

            The following is taken from my previous message to you:[color=blue]
            > 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.
            > [/color]

            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.





            Comment

            • Wiebke Pätzold

              #7
              Re: union in Python

              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(patt ern)

              def __call__(self, row):
              try:
              exec(self.feld+ " = row."+self.feld )
              except AttributeError:
              return 0
              return self.pattern.se arch(eval(self. feld))is not None

              feld = "Themenbereiche "
              vf = vw.filter(Patte rnFilter("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 ('Themenbereich e', 'Nachname').
              I tied something
              [color=blue][color=green]
              >> 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(patt ern)
              >>
              >> def __call__(self, row):
              >> try:
              >> exec(self.feld+ " = row."+self.feld )
              >> except AttributeError:
              >> return 0
              >> return self.pattern.se arch(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_n a[i])
              >> return c
              >>
              >> feld_th = "Themenbereiche "
              >> vf = vw.filter(Patte rnFilter("do.*" , feld_th))
              >>
              >> feld_na = "Nachname"
              >> vf = vw.filter(Patte rnFilter("im.*" , feld_na))
              >>
              >> print feld_th, feld_na, union(feld_th, feld_na)[/color][/color]

              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:
              [color=blue][color=green][color=darkred]
              >>> Themenbereiche Nachname[/color][/color][/color]
              Traceback (most recent call last):
              File
              "C:\PROGRA~1\Py thon22\lib\site-packages\Python win\pywin\frame work\scriptutil s.py",
              line 310, in RunScript
              exec codeObject in __main__.__dict __
              File
              "C:\Programme\P ython22\Lib\sit e-packages\Python win\pywin\Demos \Uebung11.py",
              line 46, in ?
              print feld_th, feld_na, union(feld_th, feld_na)
              NameError: name 'union' is not defined[color=blue][color=green][color=darkred]
              >>>[/color][/color][/color]






              Comment

              • Andy Todd

                #8
                Re: union in Python

                Wiebke Pätzold wrote:[color=blue]
                > 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(patt ern)
                >
                > def __call__(self, row):
                > try:
                > exec(self.feld+ " = row."+self.feld )
                > except AttributeError:
                > return 0
                > return self.pattern.se arch(eval(self. feld))is not None
                >
                > feld = "Themenbereiche "
                > vf = vw.filter(Patte rnFilter("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 ('Themenbereich e', 'Nachname').
                > I tied something
                >
                >[color=green][color=darkred]
                >>>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(patt ern)
                >>>
                >>> def __call__(self, row):
                >>> try:
                >>> exec(self.feld+ " = row."+self.feld )
                >>> except AttributeError:
                >>> return 0
                >>> return self.pattern.se arch(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_n a[i])
                >>> return c
                >>>
                >>>feld_th = "Themenbereiche "
                >>>vf = vw.filter(Patte rnFilter("do.*" , feld_th))
                >>>
                >>>feld_na = "Nachname"
                >>>vf = vw.filter(Patte rnFilter("im.*" , feld_na))
                >>>
                >>>print feld_th, feld_na, union(feld_th, feld_na)[/color][/color]
                >
                >
                > 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:
                >
                >[color=green][color=darkred]
                >>>>Themenberei che Nachname[/color][/color]
                >
                > Traceback (most recent call last):
                > File
                > "C:\PROGRA~1\Py thon22\lib\site-packages\Python win\pywin\frame work\scriptutil s.py",
                > line 310, in RunScript
                > exec codeObject in __main__.__dict __
                > File
                > "C:\Programme\P ython22\Lib\sit e-packages\Python win\pywin\Demos \Uebung11.py",
                > line 46, in ?
                > print feld_th, feld_na, union(feld_th, feld_na)
                > NameError: name 'union' is not defined
                >
                >
                >
                >
                >
                >
                >[/color]

                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/



                Comment

                • Terry Reedy

                  #9
                  Re: union in Python


                  "Wiebke Pätzold" <wiebke.paetzol [email protected]> wrote in message
                  news:u6qjjv81q7 vqq5mahauii8fll [email protected] om...[color=blue]
                  > I create a database that contains a table. 'Themenbereiche ' is one[/color]
                  of[color=blue]
                  > 13column names. This program can search for
                  > a special letter. In my example it is 'do'. and the search takes[/color]
                  place[color=blue]
                  > in 'Themenbereiche '. 'do' takes place within a word. This is solved
                  > with regular expression.[/color]

                  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.
                  [color=blue][color=green][color=darkred]
                  >>> 'abcdefg'.find( 'd')[/color][/color][/color]
                  3

                  Terry J. Reedy


                  Comment

                  Working...