write a recognizer

K

Klaus Neuner

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
 
P

Peter Otten

Klaus said:
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
 
C

Chunming Luo

Peter said:
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
 
M

max khesin

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 said:
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
 
P

Peter Otten

max said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,178
Messages
2,570,955
Members
47,509
Latest member
Jack116

Latest Threads

Top