classes (table)

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

  1. 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(pattern)

    def __call__(self, row):
    try:
    nachname = row.Nachname
    except AttributeError:
    return 0
    return self.pattern.search(nachname)is not None

    vf = vw.filter(PatternFilter("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
     
    Wiebke Pätzold, Aug 6, 2003
    #1
    1. Advertising

  2. Wiebke Pätzold wrote:
    ...
    > import Mk4py


    I'm not experienced in this package, so I'm not sure this will
    work and can't test, but:

    > def __call__(self, row):
    > try:
    > nachname = row.Nachname


    Just change this to

    whatever = getattr(row, self.attributeName)

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


    Alex
     
    Alex Martelli, Aug 6, 2003
    #2
    1. Advertising

  3. On Wed, 06 Aug 2003 11:31:39 GMT, Alex Martelli <>
    wrote:

    >Wiebke Pätzold wrote:
    > ...
    >> import Mk4py

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

    >
    >Just change this to
    >
    > whatever = getattr(row, self.attributeName)
    >
    >Where self.attributeName can be any string, e.g. defaulting
    >to 'Nachname' if you wish, as set e.g. in __init__.
    >
    >
    >Alex


    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.
     
    Wiebke Pätzold, Aug 6, 2003
    #3
  4. > def __call__(self, row):
    > try:
    > nachname = row.Nachname
    > except AttributeError:
    > return 0
    > return self.pattern.search(nachname)is not None


    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=getattr(row, column)
    return self.pattern.search(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.
     
    Christopher Boomer, Aug 6, 2003
    #4
  5. On Wed, 2003-08-06 at 13:49, Wiebke Pätzold wrote:
    > but the user of the program should have the possibility to select in
    > wich column the search takes place.


    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.python is
    for...
     
    Heiko Wundram, Aug 7, 2003
    #5
    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. David Williams
    Replies:
    2
    Views:
    1,140
    Jacob Yang [MSFT]
    Aug 12, 2003
  2. David

    Classes within classes

    David, Jul 21, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    4,959
    David
    Jul 22, 2005
  3. lonelyplanet999
    Replies:
    1
    Views:
    2,232
    VisionSet
    Nov 13, 2003
  4. Carfield Yim
    Replies:
    1
    Views:
    1,451
    Andrew Thompson
    May 31, 2004
  5. Rio
    Replies:
    4
    Views:
    1,203
Loading...

Share This Page