473,503 Members | 544 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

write a recognizer

Hello,

I want to write a class Recognizer, like so:

class Recognizer(object):

def is_of_category_1(self, token):
if token == 1:
return "1"
else:
return False

def is_of_category_2(self, token):
if token == 2:
return "2"
else:
return False

def recognize(self, token):
for fun in <?>:
result = apply(fun, token)
if result:
return result
return False

What do I have to write instead of <?>?
Or: How should I design the recognizer, if the above design is not good?

Klaus
Jul 18 '05 #1
5 1477
Klaus Neuner wrote:
Hello,

I want to write a class Recognizer, like so:

class Recognizer(object):

def is_of_category_1(self, token):
if token == 1:
return "1"
else:
return False

def is_of_category_2(self, token):
if token == 2:
return "2"
else:
return False

def recognize(self, token):
for fun in <?>:
result = apply(fun, token)
if result:
return result
return False

What do I have to write instead of <?>?
Or: How should I design the recognizer, if the above design is not good?

Klaus


The following assumes that all category checker method names start with a
common prefix. Those are automatically extracted in the __init__() method.
If you are interested in this technique, I stole it from cmd.py in the
library. IIRC, the implementation is more complete as it also inspects the
base classes.

class Recognizer(object):
def __init__(self):
r = self.recognizers = []
for n in dir(self.__class__):
if n.startswith("is_"):
r.append(getattr(self, n))

def is_of_category_1(self, token):
if token == 1:
return "1"

def is_of_category_2(self, token):
if token == 2:
return "2"

def recognize(self, token):
# would also work:
#for fun in [self.is_of_category_1, self.is_of_category_2]:

for fun in self.recognizers:
result = fun(token)
if result:
return result
return False

if __name__ == "__main__":
r = Recognizer()
for t in "12341":
print r.recognize(int(t)),
print

Peter
Jul 18 '05 #2
Peter Otten wrote:
Klaus Neuner wrote:
Hello,

I want to write a class Recognizer, like so:

class Recognizer(object):

def is_of_category_1(self, token):
if token == 1:
return "1"
else:
return False

def is_of_category_2(self, token):
if token == 2:
return "2"
else:
return False

def recognize(self, token):
for fun in <?>:
result = apply(fun, token)
if result:
return result
return False

What do I have to write instead of <?>?
Or: How should I design the recognizer, if the above design is not good?

Klaus


The following assumes that all category checker method names start with a
common prefix. Those are automatically extracted in the __init__() method.
If you are interested in this technique, I stole it from cmd.py in the
library. IIRC, the implementation is more complete as it also inspects the
base classes.

class Recognizer(object):
def __init__(self):
r = self.recognizers = []
for n in dir(self.__class__):
if n.startswith("is_"):
r.append(getattr(self, n))

def is_of_category_1(self, token):
if token == 1:
return "1"

def is_of_category_2(self, token):
if token == 2:
return "2"

def recognize(self, token):
# would also work:
#for fun in [self.is_of_category_1, self.is_of_category_2]:

for fun in self.recognizers:
result = fun(token)
if result:
return result
return False

if __name__ == "__main__":
r = Recognizer()
for t in "12341":
print r.recognize(int(t)),
print

Peter


Here is another solution:

def recongnize(self, token):
for item in self.__class__.__dict__.keys():
method = self.__class__.__dict__[item]
if callable(method) and method !=
self.__class__.__dict__["recongnize"]:
result = method(self, token)
if result:
return result
return false

-Chunming
Jul 18 '05 #3
This is cool. Curious (my py object recall is a bit stale): would this
solution work for a class that derives from Recognizer (and implements
an 'is_' method)?

thanks,
max
Peter Otten wrote:
Klaus Neuner wrote:

Hello,

I want to write a class Recognizer, like so:

class Recognizer(object):

def is_of_category_1(self, token):
if token == 1:
return "1"
else:
return False

def is_of_category_2(self, token):
if token == 2:
return "2"
else:
return False

def recognize(self, token):
for fun in <?>:
result = apply(fun, token)
if result:
return result
return False

What do I have to write instead of <?>?
Or: How should I design the recognizer, if the above design is not good?

Klaus

The following assumes that all category checker method names start with a
common prefix. Those are automatically extracted in the __init__() method.
If you are interested in this technique, I stole it from cmd.py in the
library. IIRC, the implementation is more complete as it also inspects the
base classes.

class Recognizer(object):
def __init__(self):
r = self.recognizers = []
for n in dir(self.__class__):
if n.startswith("is_"):
r.append(getattr(self, n))

def is_of_category_1(self, token):
if token == 1:
return "1"

def is_of_category_2(self, token):
if token == 2:
return "2"

def recognize(self, token):
# would also work:
#for fun in [self.is_of_category_1, self.is_of_category_2]:

for fun in self.recognizers:
result = fun(token)
if result:
return result
return False

if __name__ == "__main__":
r = Recognizer()
for t in "12341":
print r.recognize(int(t)),
print

Peter

Jul 18 '05 #4
max khesin wrote:
This is cool. Curious (my py object recall is a bit stale): would this
solution work for a class that derives from Recognizer (and implements
an 'is_' method)?


No, but you can use the following instead of dir(...) in the for loop of
__init__():

(copied from cmd.py in the libarary)

def get_names(self):
# Inheritance says we have to look in class and
# base classes; order is not important.
names = []
classes = [self.__class__]
while classes:
aclass = classes.pop(0)
if aclass.__bases__:
classes = classes + list(aclass.__bases__)
names = names + dir(aclass)
return names

Peter
Jul 18 '05 #5
Thanks to all who participated in this thread.
Jul 18 '05 #6

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

Similar topics

1
2856
by: techy techno | last post by:
Hii Just wanted to know how can I decorate my texboxes and Listmenu which is called from a JS file using the following code below: document.write("<SELECT NAME='cur2' ONCHANGE='cconv1();'>");...
2
2364
by: Brett Baisley | last post by:
Hello I have a block of html code that I want to run by calling a javascript function to print it. Its basically a table with menu items in it that is the same for many pages, and instead of...
0
1736
by: hari krishna | last post by:
hi all, My requirement is to generate xl reports throu Asp.Net without installing xl on web server computer. i am using Response object and wrtifile method as below. i dont know whether it is...
6
3557
by: yusufjammy | last post by:
Hi i am newbie in visual basic ? How to automatically write in text box without keyboard with visual basic.net .. And what is .net(dotnet) ?
0
7087
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...
0
7334
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...
1
6993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
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...
0
4675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3168
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...
0
3156
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1514
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 ...
1
737
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.