473,581 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Jul 18 '05 #1
10 3207
On Mon, 04 Aug 2003 12:30:11 +0200, Wiebke Pätzold
<wi************ *@mplusr.de> wrote:
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

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

Jul 18 '05 #2
Wiebke Pätzold <wi************ *@mplusr.de> writes:
Hi all!

Could somebody help me with a task?
I don't know what exactly you're trying to do (I don't know Mk4py), but...

import sys
import Mk4py
import re

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

class PatternFilter:
def _init_(self, pattern): __init__
self.pattern = re.compile(patt ern)

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

vf = vw.filter(Patte rnFilter("Ge.*" ))
Before posting this, you should really have tried something like:

class Dummy:
Nachname = 'Some name'
PatternFilter(" Ge.*")(Dummy)

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

I wrote this program, but it returns nothing. I can't find the error.
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.

Can somebody help me?
Can somebody tell me why the part: "class PatternFilter:" doesn't
return the expressions it should return?

'as
Jul 18 '05 #3
On Mon, 04 Aug 2003 12:30:11 +0200, Wiebke Pätzold
<wi************ *@mplusr.de> wrote:
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?
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
Wiebke


Jul 18 '05 #4
Wiebke Pätzold wrote:
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?


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>


Jul 18 '05 #5
On Mon, 2003-08-04 at 12:30, Wiebke Pätzold wrote:
class PatternFilter:
def _init_(self, pattern):
self.pattern = re.compile(patt ern)
Python special functions are always prefixed/postfixed by two
underscores, so let this read __init__
[snip]
vf = vw.filter(Patte rnFilter("Ge.*" ))


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.
Jul 18 '05 #6
Wiebke Pätzold wrote:
Could somebody help me with a task?


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
Jul 18 '05 #7
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
Jul 18 '05 #8
On Tue, 05 Aug 2003 10:59:56 +0200, Peter Otten <__*******@web. de>
wrote:
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


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
Jul 18 '05 #9
Wiebke Pätzold wrote:
So I think that something is wrong with line:
vf = vw.filter(Patte rnFilter("Ge.*" ))
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").
But here I get an error: returned exit code 0.

What's that? Please cut and paste the exact output.

Peter

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

145
6255
by: David MacQuigg | last post by:
Playing with Prothon today, I am fascinated by the idea of eliminating classes in Python. I'm trying to figure out what fundamental benefit there is to having classes. Is all this complexity unecessary? Here is an example of a Python class with all three types of methods (instance, static, and class methods). # Example from Ch.23,...
7
2386
by: Bo Peng | last post by:
Dear Python group: I am planning on an application that involves several complicated C++ classes. Basically, there will be one or two big data objects and some "action" objects that can act on the data. I would like to use a script language to control the interaction between these c++ objects. I become interested in Python since it can...
1
1831
by: Eric Wilhelm | last post by:
Hi, By new-style classes, I'm referring to the changes which came into 2.2 as a result of PEP 252 and 253. I discovered this problem when trying to use the Perl Inline::Python module with a python class that was inheriting from the new builting 'object' class like so: class MyClass(object): The problem is in detecting that this class...
12
3810
by: David MacQuigg | last post by:
I have what looks like a bug trying to generate new style classes with a factory function. class Animal(object): pass class Mammal(Animal): pass def newAnimal(bases=(Animal,), dict={}): class C(object): pass C.__bases__ = bases dict = 0
16
2048
by: Simon Wittber | last post by:
I've noticed that a few ASPN cookbook recipes, which are recent additions, use classic classes. I've also noticed classic classes are used in many places in the standard library. I've been using new-style classes since Python 2.2, and am suprised people are still using the classic classes. Is there a legitimate use for classic classes...
2
1680
by: Xah Lee | last post by:
© # -*- coding: utf-8 -*- © # Python © © # in Python, one can define a boxed set © # of data and functions, which are © # traditionally known as "class". © © # in the following, we define a set of data © # and functions as a class, and name it xxx © class xxx:
11
1522
by: Ryan Krauss | last post by:
I have a set of Python classes that represent elements in a structural model for vibration modeling (sort of like FEA). Some of the parameters of the model are initially unknown and I do some system identification to determine the parameters. After I determine these unknown parameters, I would like to substitute them back into the model and...
5
1646
by: Alex | last post by:
Hi, this is my first mail to the list so please correct me if Ive done anything wrong. What Im trying to figure out is a good way to organise my code. One class per .py file is a system I like, keeps stuff apart. If I do that, I usually name the .py file to the same as the class in it. File: Foo.py *********************** class Foo:
6
1814
by: s99999999s2003 | last post by:
hi i come from a non OO environment. now i am learning about classes. can i ask, in JAva, there are things like interface. eg public interface someinterface { public somemethod (); .... ... } In python , how to implement interface like the above? is it just
11
1429
by: allendowney | last post by:
Hi All, I am working on a revised edition of How To Think Like a Computer Scientist, which is going to be called Think Python. It will be published by Cambridge University Press, but there will still be a free version under the GNU FDL. You can see the latest version at thinkpython.com; I am revising now,
0
7854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7783
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8296
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8157
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6533
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5665
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3790
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3806
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.