Classes in Python

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

    Classes in Python

    Hi all!

    Could somebody help me with a task?

    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("Ge.*" ))

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



    I wrote this program, but it returns nothing. I can't find the error.
    Can somebody help me?
    Can somebody tell me why the part: "class PatternFilter:" doesn't
    return the expressions it should return?

    Wiebke
  • Wiebke Pätzold

    #2
    Re: Classes in Python

    On Mon, 04 Aug 2003 12:30:11 +0200, Wiebke Pätzold
    <wiebke.paetzol d@mplusr.de> wrote:
    [color=blue]
    >Hi all!
    >
    >Could somebody help me with a task?
    >
    >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("Ge.*" ))
    >
    >for r in vf:
    > print vw[r.index].Nachname
    >
    >
    >
    >I wrote this program, but it returns nothing. I can't find the error.
    >Can somebody help me?
    >Can somebody tell me why the part: "class PatternFilter:" doesn't
    >return the expressions it should return?
    >
    >Wiebke[/color]


    It has to be something wrong with line:
    vf = vw.filter(Patte rnFilter("Ge.*" ))

    Comment

    • Alexander Schmolck

      #3
      Re: Classes in Python

      Wiebke Pätzold <wiebke.paetzol d@mplusr.de> writes:
      [color=blue]
      > Hi all!
      >
      > Could somebody help me with a task?[/color]

      I don't know what exactly you're trying to do (I don't know Mk4py), but...
      [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):[/color]
      __init__
      [color=blue]
      > self.pattern = re.compile(patt ern)
      >
      > def _call_(self, row):[/color]
      __call__
      [color=blue]
      > try:
      > nachname = row.Nachname
      > except AttributeError:
      > return 0
      > return self.pattern.se arch(nachname)i s not None
      >
      > vf = vw.filter(Patte rnFilter("Ge.*" ))[/color]

      Before posting this, you should really have tried something like:

      class Dummy:
      Nachname = 'Some name'
      PatternFilter(" Ge.*")(Dummy)
      [color=blue]
      >
      > for r in vf:
      > print vw[r.index].Nachname
      >
      >
      >
      > I wrote this program, but it returns nothing. I can't find the error.[/color]

      Well, given the two errors I found, it would seem strange to me that it
      vw.filter doesn't moan about PatternFilter(" Ge.*") being not callable.

      [color=blue]
      > Can somebody help me?
      > Can somebody tell me why the part: "class PatternFilter:" doesn't
      > return the expressions it should return?[/color]


      'as

      Comment

      • Wiebke Pätzold

        #4
        Re: Classes in Python

        On Mon, 04 Aug 2003 12:30:11 +0200, Wiebke Pätzold
        <wiebke.paetzol d@mplusr.de> wrote:
        [color=blue]
        >Hi all!
        >
        >Could somebody help me with a task?
        >
        >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("Ge.*" ))
        >
        >for r in vf:
        > print vw[r.index].Nachname
        >
        >
        >
        >I wrote this program, but it returns nothing. I can't find the error.
        >Can somebody help me?
        >Can somebody tell me why the part: "class PatternFilter:" doesn't
        >return the expressions it should return?[/color]

        I create a database that contains a table. 'Nachname' and
        'Kongressbereic h' are special fieldnames. This program can search for
        a special letter. In my example it is 'G'. and the search takes place
        in 'Nachname'.
        Mow I want to use regular expression. So that I can limit my search.
        For example: I can search for Ge and it is not relevant wich letters
        follow[color=blue]
        >
        >Wiebke[/color]

        Comment

        • Fredrik Lundh

          #5
          Re: Classes in Python

          Wiebke Pätzold wrote:
          [color=blue]
          > 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("Ge.*" ))
          >
          > for r in vf:
          > print vw[r.index].Nachname
          >
          > I wrote this program, but it returns nothing. I can't find the error.
          > Can somebody help me?
          > Can somebody tell me why the part: "class PatternFilter:" doesn't
          > return the expressions it should return?[/color]

          note that built-in hooks like __init__ and __call__ uses *two* underscores
          on each side of the keyword.

          not sure why vw.filter doesn't give you an exception, but your Pattern-
          Filter instance is neither initialized nor callable.

          </F>




          Comment

          • Heiko Wundram

            #6
            Re: Classes in Python

            On Mon, 2003-08-04 at 12:30, Wiebke Pätzold wrote:[color=blue]
            > class PatternFilter:
            > def _init_(self, pattern):
            > self.pattern = re.compile(patt ern)[/color]

            Python special functions are always prefixed/postfixed by two
            underscores, so let this read __init__
            [color=blue]
            > [snip]
            > vf = vw.filter(Patte rnFilter("Ge.*" ))[/color]

            Moans here with a "__init__ takes only one argument, got two instead",
            when I try to run it. Next time you post code, read the error message,
            and this should've pointed you at the fact that _init_ isn't called, but
            __init__ is.

            When this was corrected, Metakit would've moaned that the filter wasn't
            callable, this would've made you look up the syntax for callable in the
            Python documentation http://www.python.org/doc/current/, which would've
            led you to the documentation on callable instances having a __call__
            method.

            And so on. Next time you post something, please include the correct
            error message (this program doesn't run at all, of course this means it
            doesn't return anything, but the exception traceback would've been
            helpful), and having a look at the Python documentation before posting
            can't harm too...

            Heiko.


            Comment

            • Peter Otten

              #7
              Re: Classes in Python

              Wiebke Pätzold wrote:
              [color=blue]
              > Could somebody help me with a task?[/color]

              As You seem to know not much about Python, instead of messing around
              with classes I would recommend the simplest aproach I can think of (not
              knowing the Mk4py package), which would be:

              <not tested>
              import Mk4py
              db = Mk4py.storage(" c:\\datafile.mk ", 1)
              vw = db.view("people ")

              for r in vw:
              nachname = vw[r.index].Nachname
              if nachname.starts with("Ge"):
              print nachname
              </not tested>

              If this works, take it for now, and if you can spare some time,
              read the Python tutorial (It's very good).
              You can always refine your code, once it is running :-)

              Peter

              Comment

              • Peter Otten

                #8
                Re: Classes in Python

                Judging from the Getting started section in
                http://www.equi4.com/metakit/python.html,
                vw[r.index] seems redundent, so

                <not tested>
                import Mk4py
                db = Mk4py.storage(" c:\\datafile.mk ", 1)
                vw = db.view("people ")

                for r in vw:
                if r.Nachname.star tswith("Ge"):
                print r.Nachname
                </not tested>

                would be the code to go with.
                Less code == fewer occasions for errors,
                and it might even run faster :-)

                Peter

                Comment

                • Wiebke Pätzold

                  #9
                  Re: Classes in Python

                  On Tue, 05 Aug 2003 10:59:56 +0200, Peter Otten <__peter__@web. de>
                  wrote:
                  [color=blue]
                  >Judging from the Getting started section in
                  >http://www.equi4.com/metakit/python.html,
                  >vw[r.index] seems redundent, so
                  >
                  ><not tested>
                  >import Mk4py
                  >db = Mk4py.storage(" c:\\datafile.mk ", 1)
                  >vw = db.view("people ")
                  >
                  >for r in vw:
                  > if r.Nachname.star tswith("Ge"):
                  > print r.Nachname
                  ></not tested>
                  >
                  >would be the code to go with.
                  >Less code == fewer occasions for errors,
                  >and it might even run faster :-)
                  >
                  >Peter[/color]

                  Hi Peter,

                  it's great that you give yourself so much trouble. But want to create
                  this program by classes.
                  The programm that I create with functions is OK.
                  Here is it. Perhaps it may help you.
                  import sys
                  import Mk4py
                  import re

                  db = Mk4py.storage(" c:\\datafile.mk ",1)
                  vw = db.view("people ")

                  pattern = re.compile("ra. *")

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

                  vf = vw.filter(func)

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



                  Now I trid to create this program by classes:

                  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("Ge.*" ))

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

                  But here I get an error: returned exit code 0.

                  So I think that something is wrong with line:
                  vf = vw.filter(Patte rnFilter("Ge.*" ))

                  My task is:
                  I create a database that contains a table. For example'Nachnam e'
                  This is a special fieldname. This program can search for
                  a special letter. In my example it is 'G'. and the search takes place
                  in 'Nachname'.
                  Now I want to use regular expression. So that I can limit my search.
                  For example: I can search for Ge and it is not relevant wich letters
                  follow. And it should be solve under use classes.

                  Wiebke


                  Comment

                  • Peter Otten

                    #10
                    Re: Classes in Python

                    Wiebke Pätzold wrote:
                    [color=blue]
                    > So I think that something is wrong with line:
                    > vf = vw.filter(Patte rnFilter("Ge.*" ))[/color]

                    The PatternFilter class looks OK now, and so does the line you suspect to be
                    wrong. Are you sure there are any records in the database with Nachname
                    containing Ge (and not GE or ge)?

                    The only difference I could spot:
                    - with the function you are looking for Nachname(n), that contain "ra"
                    - with the class you are looking for Nachname(n) that contain "Ge"

                    Being no regular expressions expert, I think that appending ".*" here has no
                    effect for your purpose (If you want Nachname to start with Ge, the
                    expression should be "^Ge").
                    [color=blue]
                    > But here I get an error: returned exit code 0.[/color]
                    What's that? Please cut and paste the exact output.

                    Peter

                    Comment

                    • Wiebke Pätzold

                      #11
                      Re: Classes in Python

                      On Tue, 05 Aug 2003 13:36:08 +0200, Peter Otten <__peter__@web. de>
                      wrote:
                      [color=blue]
                      >Wiebke Pätzold wrote:
                      >[color=green]
                      >> So I think that something is wrong with line:
                      >> vf = vw.filter(Patte rnFilter("Ge.*" ))[/color]
                      >
                      >The PatternFilter class looks OK now, and so does the line you suspect to be
                      >wrong. Are you sure there are any records in the database with Nachname
                      >containing Ge (and not GE or ge)?[/color]
                      That was it, why the program did not run.
                      There I could have lookedl for a long time for the error.
                      Thank you very very very much that you help me with this task. It was
                      very difficult for me because I am very new in Python.[color=blue]
                      >
                      >The only difference I could spot:
                      >- with the function you are looking for Nachname(n), that contain "ra"
                      >- with the class you are looking for Nachname(n) that contain "Ge"[/color]
                      [color=blue]
                      >
                      >Being no regular expressions expert, I think that appending ".*" here has no
                      >effect for your purpose (If you want Nachname to start with Ge, the
                      >expression should be "^Ge").
                      >[color=green]
                      >> But here I get an error: returned exit code 0.[/color]
                      >What's that? Please cut and paste the exact output.[/color]

                      [color=blue]
                      >
                      >Peter[/color]

                      Comment

                      Working...