[PEAK] Another RuleDispatch error with python 2.5
paul
paul at subsignal.org
Sun Sep 17 14:03:24 EDT 2006
Hi,
Running this code with Python 2.5rc1 (RuleDispatch-0.5a0.dev-r2100,
win32, VS2003.NET, XP) fails. The commented convert_date() function
however works. I tested with other types (decimal.Decimal) and it seems
the error occurs as soon as .when() contains multiple tests and 'or'.
thanks
Paul
"""
import datetime
import dispatch
def convert(obj):
raise NotImplementedError()
convert = dispatch.generic()(convert)
#def convert_date(obj):
# return str(obj)
#convert_date = convert.when('isinstance(obj,
datetime.datetime)')(convert_date)
def convert_dateobj(obj):
return str(obj)
convert_dateobj = convert.when(
'isinstance(obj, datetime.datetime) or '
'isinstance(obj, datetime.date)')(convert_dateobj)
if __name__ == '__main__':
d = convert(datetime.datetime.now())
print "converted <datetime>: %s" % d
"""
traceback below, info between ==== is 'builder' and 'nodelist' from
ast_builder.build():
==================================================
<dispatch.predicates.CriteriaBuilder instance at 0x00C93990>
==================================================
(326, (303, (304, (305, (306, (307, (309, (310, (311, (312, (313, (314,
(315, (3
16, (317, (1, 'isinstance', 1)), (321, (7, '(', 1), (329, (330, (303,
(304, (305
, (306, (307, (309, (310, (311, (312, (313, (314, (315, (316, (317, (1,
'obj', 1
)))))))))))))))), (12, ',', 1), (330, (303, (304, (305, (306, (307,
(309, (310,
(311, (312, (313, (314, (315, (316, (317, (1, 'datetime', 1)), (321,
(23, '.', 1
), (1, 'datetime', 1))))))))))))))))), (8, ')', 1))))))))))))), (1,
'or', 1), (3
05, (306, (307, (309, (310, (311, (312, (313, (314, (315, (316, (317,
(1, 'isins
tance', 1)), (321, (7, '(', 1), (329, (330, (303, (304, (305, (306,
(307, (309,
(310, (311, (312, (313, (314, (315, (316, (317, (1, 'obj',
1)))))))))))))))), (1
2, ',', 1), (330, (303, (304, (305, (306, (307, (309, (310, (311, (312,
(313, (3
14, (315, (316, (317, (1, 'datetime', 1)), (321, (23, '.', 1), (1,
'date', 1))))
))))))))))))), (8, ')', 1))))))))))))))))
==================================================
Traceback (most recent call last):
File "D:\home2\programming\dispatch_test.py", line 18, in <module>
'isinstance(obj, datetime.datetime) or '
File
"C:\programme\python25\lib\site-packages\ruledispatch-0.5a0.dev_r2100-py2
.5-win32.egg\dispatch\functions.py", line 690, in when
return self._decorate(cond)
File
"C:\programme\python25\lib\site-packages\ruledispatch-0.5a0.dev_r2100-py2
.5-win32.egg\dispatch\functions.py", line 577, in _decorate
cond = self.parseRule(cond,frame=frame) or cond
File "<string>", line 10, in parseRule
File
"C:\programme\python25\lib\site-packages\ruledispatch-0.5a0.dev_r2100-py2
.5-win32.egg\dispatch\functions.py", line 440, in parseRule
return self.parse(rule, frame.f_locals, frame.f_globals)
File
"C:\programme\python25\lib\site-packages\ruledispatch-0.5a0.dev_r2100-py2
.5-win32.egg\dispatch\functions.py", line 326, in parse
return parse_expr(expr_string,builder)
File
"C:\Programme\python25\lib\site-packages\ruledispatch-0.5a0.dev_r2100-py2
.5-win32.egg\dispatch\ast_builder.py", line 388, in parse_expr
return build(builder, parser.expr(expr).totuple(1)[1])
File
"C:\Programme\python25\lib\site-packages\ruledispatch-0.5a0.dev_r2100-py2
.5-win32.egg\dispatch\ast_builder.py", line 383, in build
return production[nodelist[0]](builder,nodelist)
KeyError: 304
More information about the PEAK
mailing list