By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

write a recognizer

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
Thanks to all who participated in this thread.
Jul 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.