[PEAK] New PEAK-Rules Milestone in SVN
Phillip J. Eby
pje at telecommunity.com
Sat Jan 12 21:31:33 EST 2008
I just checked in the final piece needed to give PEAK-Rules roughly
the same predicate dispatch generic function capabilities as
RuleDispatch. (Minus PyProtocols integration, that is.)
Actually, PEAK-Rules has slightly more power than RuleDispatch, in
that it supports multiple dispatch on types through a fast and simple
engine (based on a prototype by GvR), as well as full Python
expression parsing, and can automatically upgrade a function from one
engine to the other at runtime. You can start out with something like:
@abstract
def f(a,b):
"""A function"""
@when(f, (int, str))
def f_int_str(a, b):
...
@when(f, (str, int))
def f_str_int(a, b):
...
And then add some strings, like so:
@when(f, "isinstance(a,str) and isinstance(b, int) and b==42"))
def f_str_42(a, b):
...
And the GF machinery will automatically import the extra modules
needed to parse and dispatch the more complex kinds of
expressions. Thereafter, you can use both type tuples and strings
for the same function, and they're both translated to the same thing
internally.
(One minor issue: exact-type comparisons using 'istype(x)' in type
tuples are not correctly translated or indexed yet, when a function
has rules registered with both type-tuples and expressions.)
Anyway, there is still a fairly long list of tasks (mostly better
docs, tests, and performance) before the 0.5a1 release. I originally
planned to have it out by February 1st, but due to some OSAF-related
travel, I'll have about a week less of nights and weekends available
this month to work on it than I originally planned.
Some of the more exciting features I would like to get in 0.5a1 (or
at least before 0.5b1) will include variable bindings, "meta"
functions (i.e. functions that can affect their compile-time
representation in conditions), and pattern matching support.
On a less exciting but more practical note, I hope to have a
"backward compatibility" shim API to let you make PEAK-Rules an
almost drop-in replacement for RuleDispatch, and maybe eventually
PyProtocols as well.
The idea is that you should be able to do something like 'from
peak.rules import dispatch' and get a module that fakes up all your
favorite RuleDispatch APIs, by wrapping the new PEAK-Rules APIs. It
won't be a 100% mapping, since some things are just too different,
but for single and multiple/predicate dispatch generic functions that
don't have custom combiners or method types, and don't use
PyProtocols interfaces to specify argument types, the compatibility
API should be a near-perfect match.
Of course, that API doesn't exist yet, and neither do docs for either
the old or new APIs. But I'd love to hear from anybody who's willing
to tinker with using the SVN version in code that's currently using
RuleDispatch.
More information about the PEAK
mailing list