473,657 Members | 2,316 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(obje ct):

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 1482
Klaus Neuner wrote:
Hello,

I want to write a class Recognizer, like so:

class Recognizer(obje ct):

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(obje ct):
def __init__(self):
r = self.recognizer s = []
for n in dir(self.__clas s__):
if n.startswith("i s_"):
r.append(getatt r(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_cate gory_1, self.is_of_cate gory_2]:

for fun in self.recognizer s:
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(obje ct):

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(obje ct):
def __init__(self):
r = self.recognizer s = []
for n in dir(self.__clas s__):
if n.startswith("i s_"):
r.append(getatt r(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_cate gory_1, self.is_of_cate gory_2]:

for fun in self.recognizer s:
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(obje ct):

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(obje ct):
def __init__(self):
r = self.recognizer s = []
for n in dir(self.__clas s__):
if n.startswith("i s_"):
r.append(getatt r(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_cate gory_1, self.is_of_cate gory_2]:

for fun in self.recognizer s:
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.__b ases__)
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
2868
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();'>"); document.write("<OPTION VALUE='0.345066110642241'>Argentina Peso </OPTION>"); document.write("<OPTION VALUE='0.790200069503053'>Australia Dollar
2
2379
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 copying/pasting everytime I change it, I figure this will be better, as I only change it once. The problem is, document.write doesn't handle multiple lines very well, so I was wondering what is the best way to do this? Maybe there is even a better...
0
1750
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 correct, but giving error says " file is accessed by other process cannot access" I have some logic inbetween and write the info using response.write in to html format Ex: Response.Write("<td>" & "client_no" & "</td>"). I am getting the error at :...
6
3568
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
8411
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8323
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8739
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6176
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
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
2
1732
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.