classes (table)

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

    classes (table)

    Hi all,

    I create a database that contains a table. 'Nachname' is one of 13
    column names. This program can search for
    a special letter. In my example it is 'ra'. and the search takes place
    in 'Nachname'. 'ra' takes place within a word. This is solved with
    regular expression. So that I can limit my search.
    For example: I can search for 'ra' and it is not relevant wich letters
    follow or wich letters are in front of 'ra'.
    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):
    self.pattern = re.compile(patt ern)

    def __call__(self, row):
    try:
    nachname = row.Nachname
    except AttributeError:
    return 0
    return self.pattern.se arch(nachname)i s not None

    vf = vw.filter(Patte rnFilter("ra.*" ))

    for r in vf:
    print vw[r.index].Nachname


    The program should have two possibilities to search for regular
    expressions. The first possibility is in this program. Here I can
    look for a certain expression. In my example I can look for 'ra' and
    it is not relevant wich letters follow or wich letters are in front of
    'ra'. But this search can ONLY takes place in the column 'Nachname'.
    'Nachname is fixed by the programmer.
    The second possibility is that the user and NOT the programmer
    determine the column name in which the user want to look for a regular
    expression. Only the user can determine in which column the search
    takes place.
    These 2 kinds of the search must be combined in the program.

    I hope somebody can help me with my problem!

    Wiebke



  • Alex Martelli

    #2
    Re: classes (table)

    Wiebke Pätzold wrote:
    ...[color=blue]
    > import Mk4py[/color]

    I'm not experienced in this package, so I'm not sure this will
    work and can't test, but:
    [color=blue]
    > def __call__(self, row):
    > try:
    > nachname = row.Nachname[/color]

    Just change this to

    whatever = getattr(row, self.attributeN ame)

    Where self.attributeN ame can be any string, e.g. defaulting
    to 'Nachname' if you wish, as set e.g. in __init__.


    Alex

    Comment

    • Wiebke Pätzold

      #3
      Re: classes (table)

      On Wed, 06 Aug 2003 11:31:39 GMT, Alex Martelli <aleax@aleax.it >
      wrote:
      [color=blue]
      >Wiebke Pätzold wrote:
      > ...[color=green]
      >> import Mk4py[/color]
      >
      >I'm not experienced in this package, so I'm not sure this will
      >work and can't test, but:
      >[color=green]
      >> def __call__(self, row):
      >> try:
      >> nachname = row.Nachname[/color]
      >
      >Just change this to
      >
      > whatever = getattr(row, self.attributeN ame)
      >
      >Where self.attributeN ame can be any string, e.g. defaulting
      >to 'Nachname' if you wish, as set e.g. in __init__.
      >
      >
      >Alex[/color]

      but the user of the program should have the possibility to select in
      wich column the search takes place.

      On the one hand the program must be in the situation to search in all
      columns of the table for a special word. And on the other hand the
      user have to determine in wich column the search after this special
      word takes place. This is the task. And this two things must combined
      in one program.

      Comment

      • Christopher Boomer

        #4
        Re: classes (table)

        > def __call__(self, row):[color=blue]
        > try:
        > nachname = row.Nachname
        > except AttributeError:
        > return 0
        > return self.pattern.se arch(nachname)i s not None[/color]

        You should probably start by rewriting your basic function as

        def search_call (self, row, column):
        if not hasattr(row, column):
        return 0 # No such column
        nachname=getatt r(row, column)
        return self.pattern.se arch(nachname) is not None

        Now you can choose the column at will within the program.
        (nachname is now a poor choice of variable name)

        To restrict access to columns, restrict this function and provide as a
        public function the following:

        def __call__ (self, row):
        return search_call(row , 'Nachname')

        Hope this helps.

        MfG
        Christopher Boomer.


        Comment

        • Heiko Wundram

          #5
          Re: classes (table)

          On Wed, 2003-08-06 at 13:49, Wiebke Pätzold wrote:[color=blue]
          > but the user of the program should have the possibility to select in
          > wich column the search takes place.[/color]

          If you're not willing to start thinking Python and read some
          documentation before asking questions as these (have you ever seriously
          programmed before? If not, go read the tutorials on
          http://www.python.org), I guess there's no reason I'd help you. Unless
          you pay for my programming time, of course...

          Heiko.

          PS: I know Alex Martelli has answered already, but I just thought that
          it might be worth noting that Wiebke is simply asking the group to write
          a program she needs, which is not what I think comp.lang.pytho n is
          for...


          Comment

          Working...